最近在做STM8S105K4跟ES8323S的I2C通信遇到了一个问题。当我发送的数据在6个以内时,接收的数据全都正确,但是当6<发送的数据<24的时候,接收的数据总是第七个错误,而当发送的数据>24个的时候,接收的数据除了第七个错误的,连第一个也错误了。实在是不知道哪里出了问题,求各位帮帮忙,给点指导。下面是相关的代码
//发送单个字节
void SendByte(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin_SDA,GPIO_Pin_TypeDef GPIO_Pin_SCL,uchar c)
{
uchar BitCnt;
for(BitCnt=0;BitCnt<8;BitCnt++) /*要传送的数据长度为8位*/
{
GPIOx->ODR &= (uint8_t)(~GPIO_Pin_SCL); //scl 0
Delay(delay_count2);
if( c&0x80 )
GPIOx->ODR |= (uint8_t)GPIO_Pin_SDA; //sda 1
else
GPIOx->ODR &= (uint8_t)(~GPIO_Pin_SDA); //sda 0
c=c<<1;
GPIOx->ODR |= (uint8_t)GPIO_Pin_SCL; //scl 1
/*置时钟线为高,通知被控器开始接收数据位*/
__no_operation(); /*保证时钟高电平周期大于4μs*/
Delay(delay_count);
}
//从机应答,可以用应答和非应答信号代替
__no_operation(); //maybe cancel this line
GPIOx->ODR &= (uint8_t)(~GPIO_Pin_SCL); //first scl 0
Delay(delay_count);
GPIOx->ODR &= (uint8_t)(~GPIO_Pin_SDA); //sda 0
// Set Input mode
GPIOx->CR1 &= (uint8_t)(~(GPIO_Pin_SDA)); /* no pull_up Register 1 */
GPIOx->DDR &= (uint8_t)(~(GPIO_Pin_SDA));
Delay(delay_count);
GPIOx->ODR |= (uint8_t)GPIO_Pin_SCL; //scl 1 ;the receive side will send out the acknowledge signal
__no_operation();
Delay(delay_count);
if((GPIOx->IDR & (uint8_t)GPIO_Pin_SDA)) //normal low
ack=FALSE; /*判断是否接收到应答信号*/
else
ack=TRUE;
GPIOx->ODR &= (uint8_t)(~GPIO_Pin_SCL); //scl 0
GPIOx->DDR |= (uint8_t)GPIO_Pin_SDA; // must be set output mode
Delay(delay_count);
}
//发送多个字节
bool ISendStr(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin_SDA,GPIO_Pin_TypeDef GPIO_Pin_SCL,uchar *s,uchar no)
{
uchar i;
Start_I2c(GPIOx,GPIO_Pin_SDA,GPIO_Pin_SCL); /*启动总线*/
for(i=0;i<no;i++)
{
SendByte(GPIOx,GPIO_Pin_SDA,GPIO_Pin_SCL,*s); /*发送数据*/
s++;
}
Stop_I2c(GPIOx,GPIO_Pin_SDA,GPIO_Pin_SCL); /*结束总线*/
return(TRUE);
}
//I2C发送数据
void Write_BQREC(void)
{
Array_Write_I2C_TI[1] = 0x02;
Array_Write_I2C_TI[2] = BQreq[0];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Array_Write_I2C_TI[1] = 0x2d;
Array_Write_I2C_TI[2] = BQreq[1];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Array_Write_I2C_TI[1] = 0x00;
Array_Write_I2C_TI[2] = BQreq[2];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Array_Write_I2C_TI[1] = 0x07;
Array_Write_I2C_TI[2] = BQreq[3];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Array_Write_I2C_TI[1] = 0x2b;
Array_Write_I2C_TI[2] = BQreq[4];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Array_Write_I2C_TI[1] = 0x08;
Array_Write_I2C_TI[2] = BQreq[5];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x01;
Array_Write_I2C_TI[2] = BQreq[6];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x03;
Array_Write_I2C_TI[2] = BQreq[7];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x04;
Array_Write_I2C_TI[2] = BQreq[8];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x05;
Array_Write_I2C_TI[2] = BQreq[9];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x06;
Array_Write_I2C_TI[2] = BQreq[10];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x19;
Array_Write_I2C_TI[2] = BQreq[11];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x0a;
Array_Write_I2C_TI[2] = BQreq[12];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x0b;
Array_Write_I2C_TI[2] = BQreq[13];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x0c;
Array_Write_I2C_TI[2] = BQreq[14];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x0d;
Array_Write_I2C_TI[2] = BQreq[15];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x10;
Array_Write_I2C_TI[2] = BQreq[16];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x17;
Array_Write_I2C_TI[2] = BQreq[17];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x18;
Array_Write_I2C_TI[2] = BQreq[18];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x1A;
Array_Write_I2C_TI[2] = BQreq[19];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x1B;
Array_Write_I2C_TI[2] = BQreq[20];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x27;
Array_Write_I2C_TI[2] = BQreq[21];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x2A;
Array_Write_I2C_TI[2] = BQreq[22];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x02;
Array_Write_I2C_TI[2] = BQreq[23];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x02;
Array_Write_I2C_TI[2] = BQreq[24];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x30;
Array_Write_I2C_TI[2] = BQreq[25];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x31;
Array_Write_I2C_TI[2] = BQreq[26];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x30;
Array_Write_I2C_TI[2] = BQreq[27];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x31;
Array_Write_I2C_TI[2] = BQreq[28];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x1A;
Array_Write_I2C_TI[2] = BQreq[29];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
Array_Write_I2C_TI[1] = 0x1B;
Array_Write_I2C_TI[2] = BQreq[30];
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = ISendStr(GPIOC, POW_SDA, POW_SCL, Array_Write_I2C_TI, 3);
}
Rx = 0;
Delayms(5);
}
=============================================================================
//接收单字节
uchar RcvByte(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin_SDA,GPIO_Pin_TypeDef GPIO_Pin_SCL)
{
uchar retc;
uchar BitCnt;
GPIOx->CR1 &= (uint8_t)(~(GPIO_Pin_SDA)); /* no pull_up Register 1 */
GPIOx->DDR &= (uint8_t)(~(GPIO_Pin_SDA)); // it 's need to set input mode ,very important
Delay(delay_count);
retc=0;
for(BitCnt=0;BitCnt<8;BitCnt++)
{
GPIOx->ODR |= (uint8_t)GPIO_Pin_SCL; //scl 1
__no_operation();
retc<<=1;
if((GPIOx->IDR & (uint8_t)GPIO_Pin_SDA)) retc+=1; /*读数据位,接收的数据位放入retc中 */
GPIOx->ODR &= (uint8_t)(~GPIO_Pin_SCL); //scl 0 the last status of the scl must be "0"
}
return(retc);
}
//接收多字节
bool IRcvStr(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin_SDA,GPIO_Pin_TypeDef GPIO_Pin_SCL,uchar sla,uchar suba,uchar *s,uchar no)
{
Start_I2c(GPIOx,GPIO_Pin_SDA,GPIO_Pin_SCL); /*启动总线*/
SendByte(GPIOx,GPIO_Pin_SDA,GPIO_Pin_SCL,sla); /*发送器件地址*/
if(ack==FALSE)return(FALSE);
SendByte(GPIOx,GPIO_Pin_SDA,GPIO_Pin_SCL,suba); /*发送器件子地址*/
if(ack==FALSE)return(FALSE);
Start_I2c(GPIOx,GPIO_Pin_SDA,GPIO_Pin_SCL); //start again
SendByte(GPIOx,GPIO_Pin_SDA,GPIO_Pin_SCL,sla+1); //read mode
if(ack==FALSE)return(FALSE);
while(no!=1)
{
*s=RcvByte(GPIOx,GPIO_Pin_SDA,GPIO_Pin_SCL); /*receive数据*/
Ack_I2c(GPIOx,GPIO_Pin_SDA,GPIO_Pin_SCL); /*发送就答位*/
s++;
no--;
}
*s=RcvByte(GPIOx,GPIO_Pin_SDA,GPIO_Pin_SCL); //read last byte
NoAck_I2c(GPIOx,GPIO_Pin_SDA,GPIO_Pin_SCL); /*发送非应位*/
Stop_I2c(GPIOx,GPIO_Pin_SDA,GPIO_Pin_SCL); /*结束总线*/
return(TRUE);
}
//读取BQ寄存器数据
void Read_BQREC(void)
{
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x02, BQREC, 3);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x2d, BQREC, 3);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x00, BQREC, 3);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x07, BQREC, 3);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x2b, BQREC, 3);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x08, BQREC, 3);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x01, BQREC, 3);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x03, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x04, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x05, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x06, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x19, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x0a, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x0b, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x0c, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x0d, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x10, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x17, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x18, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x1A, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x1B, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x27, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x2A, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x02, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x02, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x30, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x31, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x30, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x31, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x1A, BQREC, 1);
}
Rx = 0;
while (!Rx)
{
IWDG->KR = 0xAA;
Rx = IRcvStr(BQ24168_IIC_PORT, POW_SDA, POW_SCL, 0x20, 0x1B, BQREC, 1);
}
Rx = 0;
} |