下面是程序,从电容触摸屏中读取坐标,问题是如果我每次只读一个地址的数据就没问题,连续读取的话就会都是第一个地址的数据,
void i2c_master_send_ACK( void)
{
SDA = 1;
i2c_delay();
SCL = 1;
i2c_delay();
SCL = 0;
i2c_delay();
}
这里发送应答应该是将SDA=0才感觉是对的,要拉低SDA给对方应答信号,但是如果这里发送SDA=0的话,
后面就连读一次的数据都会出现这个错误printf("msg i2c read error: %d
",ret);
现在的程序至少执行ft5x06_i2c_rxdata(u8 *rxdata,u8 length)的length长度=1的不会出现任何错误
请帮忙看一下.
- void i2c_delay(void)
- {
- u8 i=5;
- while(i--);
- }
- void i2c_start(void)
- {
- SDA = 1;
- SCL = 1;
- i2c_delay();
- SDA = 0;
- i2c_delay();
- SCL = 0;
- i2c_delay();
- }
- void i2c_stop(void)
- {
- SDA = 0;
- SCL = 1;
- i2c_delay();
- SDA = 1;
- i2c_delay();
- }
- void i2c_master_send_ACK( void)
- {
- SDA = 1;
- i2c_delay();
- SCL = 1;
- i2c_delay();
- SCL = 0;
- i2c_delay();
- }
- u8 i2c_master_recv_ACK(void)
- {
- u8 ACK=0;
- SCL = 1;
- i2c_delay();
-
- ACK |= SDA;
- i2c_delay();
-
- SCL = 0;
- i2c_delay();
- return ACK;
- }
- u8 i2c_master_send_byte(u8 Byte)
- {
- u8 i,ACK;
- for(i=0;i<8;i++)
- {
- SDA = Byte>>(7-i)&0x01;
- i2c_delay();
- SCL = 1;
- i2c_delay();
- SCL = 0;
- i2c_delay();
- }
- ACK = i2c_master_recv_ACK();
- return ACK; //ACK=1=失败,ACK=0=成功
- }
- u8 i2c_master_recv_byte( void )
- {
- u8 i,temp;
- for(i=0;i<8;i++)
- {
- temp=temp<<1;
- SCL = 1;
- i2c_delay();
- temp |= SDA;
- SCL = 0;
- i2c_delay();
- }
- return temp;
- }
- //i2c数据传输,每次传输一个数据包
- int i2c_transfer_msg(u8 SlaveAddr_and_RorW , u8 *msg , u8 length)
- {
- int ret;
- u8 i;
- i2c_start();
- ret = i2c_master_send_byte(SlaveAddr_and_RorW);
- if(SlaveAddr_and_RorW&0x01) //RorW=1=Read
- {
- for(i=0;i<length;i++)
- {
- msg[i]=i2c_master_recv_byte();
- i2c_master_send_ACK();
- }
- }
- else //RorW=0=Write
- {
- for(i=0;i<length;i++)
- {
- ret += i2c_master_send_byte(msg[i]);
- }
- }
- i2c_stop();
- return ret; //=0表示没任何错误
- }
- //master从ft5x06中读数据,数据缓冲区第一个字节为起始读地址
- static int ft5x06_i2c_rxdata(u8 *rxdata,u8 length)
- {
- int ret;
- u8 SlaveAddr_and_RorW;
- SlaveAddr_and_RorW = (ft5x06_I2C_SLAVE_ADDRESS<<1) + 0; //Write
- ret = i2c_transfer_msg(SlaveAddr_and_RorW,rxdata,1);
-
- SlaveAddr_and_RorW = (ft5x06_I2C_SLAVE_ADDRESS<<1) + 1; //Read
- ret += i2c_transfer_msg(SlaveAddr_and_RorW,rxdata,length);
- if(ret)
- printf("msg i2c read error: %d
",ret);
- return ret; //=0表示没任何错误
- }
复制代码
弄个超时,重发。
一周热门 更多>