#ifndef _IIC_H_
#define _IIC_H_
//
#include "DrvGPIO.h"
#define AT24C01 127
#define AT24C02 255
#define AT24C04 511
#define AT24C08 1023
#define AT24C16 2047
#define AT24C32 4095
#define AT24C64 8191
#define AT24C128 16383
#define AT24C256 32767
//
#define EE_TYPE AT24C64
//
#define I2C_SCL_0() GPIOA->DOUT&=~(1<<9)
#define I2C_SCL_1() GPIOA->DOUT|=1<<9
#define I2C_SDA_0() GPIOA->DOUT&=~(1<<8)
#define I2C_SDA_1() GPIOA->DOUT|=1<<8
//
#define I2C_SDA_READ() DrvGPIO_GetBit(E_GPA, 8)
//
void IIC_Start();
void IIC_Stop();
uint8_t IIC_Write_Byte(uint8_t _ucByte);
uint8_t IIC_Read_Byte();
uint8_t IIC_Get_Ack(void);
void IIC_Ack(void);
void IIC_NAck(void);
static void IIC_Init(void);
//
static void i2c_Delay(void)
{
uint8_t i;
for (i = 0; i < 10; i++)
;
}
//
void IIC_Start()
{
I2C_SDA_1();
I2C_SCL_1();
i2c_Delay();
I2C_SDA_0();
i2c_Delay();
I2C_SCL_0();
i2c_Delay();
}
//
void IIC_Stop(void)
{
I2C_SDA_0();
I2C_SCL_1();
i2c_Delay();
I2C_SDA_1();
}
//
uint8_t IIC_Write_Byte(uint8_t _ucByte)
{
uint8_t i;
for (i = 0; i < 8; i++)
{
if (_ucByte & 0x80)
{
I2C_SDA_1();
}
else
{
I2C_SDA_0();
}
i2c_Delay();
I2C_SCL_1();
i2c_Delay();
I2C_SCL_0();
if (i == 7)
{
I2C_SDA_1();
}
_ucByte <<= 1;
i2c_Delay();
}
return IIC_Get_Ack();
}
//
uint8_t IIC_Read_Byte()
{
uint8_t i;
uint8_t value;
value = 0;
for (i = 0; i < 8; i++)
{
value <<= 1;
I2C_SCL_1();
i2c_Delay();
if (I2C_SDA_READ())
{
value++;
}
I2C_SCL_0();
i2c_Delay();
}
return value;
}
//
//
uint8_t IIC_Get_Ack(void)
{
uint8_t re;
I2C_SDA_1();
i2c_Delay();
I2C_SCL_1();
i2c_Delay();
if (I2C_SDA_READ())
{
re = 1;
}
else
{
re = 0;
}
I2C_SCL_0();
i2c_Delay();
return re;
}
//
void IIC_Ack(void)
{
I2C_SDA_0();
i2c_Delay();
I2C_SCL_1();
i2c_Delay();
I2C_SCL_0();
i2c_Delay();
I2C_SDA_1();
}
//
void IIC_NAck(void)
{
I2C_SDA_1();
i2c_Delay();
I2C_SCL_1();
i2c_Delay();
I2C_SCL_0();
i2c_Delay();
}
//
static void IIC_Init(void)
{
DrvGPIO_Open(E_GPA, 8, E_IO_OPENDRAIN);
DrvGPIO_Open(E_GPA, 9, E_IO_OPENDRAIN);
i2c_Stop();
}
#endif
|