最近在做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_opera
tion(); /*保证时钟高电平周期大于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);
}
=================================================
一周热门 更多>