我这是用单片机普通I/O模拟IIC,向E2PROM中写1个字节的,然后读出来校验,可以就是出错,我不知道是读错误,还是根本没有写进去,下面是我的程序,帮忙看看,谢谢了
#define CSI24WC02 0xA0 /* 定义从要地址,忽略通用地址 */
#define SCL (1<<2)
#define SDA (1<<3)
#define LED1 (1<<22)
void i2c_start(void)//发送开始信号
{
IO0SET = SDA;
IO0SET = SCL;
IO0CLR = SDA;
IO0CLR = SCL;
return;
}
//****************************************************************************
void i2c_stop(void)//发送停止信号
{
IO0CLR = SDA;
IO0SET = SCL;
IO0SET = SDA;
return;
}
//****************************************************************************
uint8 i2c_ack(void)//确认信号
{
uint8 ack;
IO0SET = SDA;
IO0SET = SCL;
if(SDA==1)
ack = 1;
else
ack = 0;
IO0CLR = SCL;
return (ack);
}
//****************************************************************************
void i2c_send8bit(uint8 b)
{
uint8 a;
for(a=0;a<8;a++)
{
if (b&0x80==1)
IO0SET = SDA;
else
IO0CLR = SDA;
IO0SET = SCL;
IO0CLR = SCL;
}
return;
}
//****************************************************************************
uint8 i2c_receive8bit(void)
{
uint8 a;
uint8 b=0;
for(a=0;a<8;a++)
{
IO0SET = SCL;
b=b<<1;
if(SDA==1)
b=b|0x01; //按位或
IO0CLR = SCL;
}
return(b);
}
//****************************************************************************
//i2c_write(地址,数据),写一个字节
void i2c_write(uint8 Address,uint8 Data)
{
i2c_start();
i2c_send8bit(CSI24WC02);
do{
i2c_send8bit(Address);
}while(i2c_ack());
do{
i2c_send8bit(Data);
}while(i2c_ack());
i2c_stop();
return;
}
//****************************************************************************
//i2c_read(地址,数据),写一个字节
uint8 i2c_read(uint8 Address)
{
uint8 c;
i2c_start();
i2c_send8bit(CSI24WC02);
do{
i2c_send8bit(Address);
}while(i2c_ack());
do{
i2c_start();
i2c_send8bit(0xA1);
}while(i2c_ack());
do{
c=i2c_receive8bit();
}while(i2c_ack());
i2c_stop();
return(c);
int main(void)
{
uint8 dd;
PINSEL0 = 0x00000000; // 设置I2C口连接
PINSEL1 = 0x00000000;
i2c_write(0x02,0x06);
DelayNS(5);
dd=i2c_read(0x02);
for(;;)
{}
return(0);
}