我的程序单字节读写都没问题,连续的就不行,代码如下,求帮忙看看
void start_iic(void)
{
TWI_SCL_HIGH();
delay_us(DELAYTIME);
TWI_SDA_HIGH();
delay_us(DELAYTIME);
TWI_SDA_LOW();
delay_us(DELAYTIME);
TWI_SCL_LOW();
delay_us(DELAYTIME);
}
void stop_iic(void)
{
TWI_SDA_LOW();
delay_us(DELAYTIME);
TWI_SCL_HIGH();
delay_us(DELAYTIME);
TWI_SDA_HIGH();
delay_us(DELAYTIME);
TWI_SCL_LOW();
delay_us(DELAYTIME);
}
uint8_t readchar(void)
{
uint8_t i,val=0;
TWI_SDA_INPUT();
TWI_DELAY();
for(i=0;i<8;i++)
{
val <<= 1;
TWI_SCL_HIGH();
delay_us(DELAYTIME);
if (TWI_SDA_READ() == 1)
val |=1;
TWI_SCL_LOW();
delay_us(DELAYTIME);
}
TWI_SDA_OUTPUT();
delay_us(DELAYTIME);
TWI_SDA_HIGH();
delay_us(DELAYTIME);
TWI_SCL_HIGH();
delay_us(DELAYTIME);
TWI_SCL_LOW();
delay_us(DELAYTIME);
return val;
}
void writedata(uint8_t data)
{
uint8_t i;
for(i=0;i<8;i++)
{
if(data & 0x80) TWI_SDA_HIGH();
else TWI_SDA_LOW();
data=(data<<1);
TWI_SCL_HIGH();
delay_us(DELAYTIME);
TWI_SCL_LOW();
delay_us(DELAYTIME);
}
TWI_SDA_INPUT();
TWI_DELAY();
TWI_SDA_HIGH();
delay_us(DELAYTIME);
TWI_SCL_HIGH();
delay_us(DELAYTIME);
TWI_SCL_LOW();
delay_us(DELAYTIME);
TWI_SDA_OUTPUT();
delay_us(DELAYTIME);
}
uint8_t readiic (uint8_t addrr)
{
uint8_t val;
start_iic();
writedata(0xa0);
writedata(addrr);
start_iic();
writedata(0xa1);
val = readchar();
stop_iic();
return val;
}
void writeiic(uint8_t addrr,uint8_t data)
{
start_iic();
writedata(0xa0);
writedata(addrr);
writedata(data);
stop_iic();
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
我的读写偶数位没问题,读写基数位就出错,跟楼主的差不多吧
一周热门 更多>