我的程序单字节读写都没问题,连续的就不行,代码如下,求帮忙看看
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();
}
|