|
现自己写了一份基于STM8s库函数的I2C代码,遇到一个问题,在读取数据的时候,在时序的重发起始信号后,SDA线就始终为低了,不知道如何解决 ,还请大神指点!暂时不要叫我用软件模拟
现附上代码:
#define IIC_Frequency 100000
#define IIC_InFrequency 4
#define IIC_salveaddr 0xDE
void SingleGSM_IIC_init(void)
{
CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C,ENABLE);
GPIO_Init(GPIOE,GPIO_PIN_1,GPIO_MODE_OUT_OD_HIZ_SLOW);
GPIO_Init(GPIOE,GPIO_PIN_2,GPIO_MODE_OUT_OD_HIZ_SLOW);
I2C_DeInit();
I2C_Init(IIC_Frequency,0x0001,I2C_DUTYCYCLE_2,I2C_ACK_CURR,I2C_ADDMODE_7BIT,IIC_InFrequency);
I2C_Cmd(ENABLE);
}
void SingleGSM_IIC_write(uint8_t addr,uint8_t data)
{
while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
I2C_GenerateSTART(ENABLE);
while(!(I2C->SR1 & 0x01));
// while(!(I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT)));
I2C_Send7bitAddress(0xDE,I2C_DIRECTION_TX);
while(!(I2C->SR1 & 0x02));
I2C->SR1;
I2C->SR3;
// while(!(I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)));
I2C_SendData(addr);
while(!(I2C->SR1 & 0x80));
// I2C->SR1;
// I2C->SR3;
// while(!(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(data);
while(!(I2C->SR1 & 0x84));
// while(!(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(ENABLE);
}
uint8_t SingleGSM_IIC_read(uint8_t addr)
{
uint8_t data;
while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
I2C_GenerateSTART(ENABLE);
while(!(I2C->SR1 & 0x01));
// while(!(I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT)));
I2C_Send7bitAddress(IIC_salveaddr,I2C_DIRECTION_TX);
while(!(I2C->SR1 & 0x02));
I2C->SR1;
I2C->SR3;
// while(!(I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)));
// while(!(I2C->SR1 & 0x80))
I2C_SendData(addr);
while(!(I2C->SR1 & 0x80));
// I2C->SR1;
// I2C->SR3;
// while(!(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED)));
I2C_GenerateSTART(ENABLE);/*********SDA信号执行完这一步后再也不能变高************/
while(!(I2C->SR1 & 0x01));
// while(!(I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT)));
I2C_Send7bitAddress(IIC_salveaddr,I2C_DIRECTION_RX);
I2C->SR1;
I2C->SR3;
// while(!(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED)));
while(!(I2C->SR1 & 0x40));
I2C_AcknowledgeConfig(I2C_ACK_NONE);
I2C_GenerateSTOP(ENABLE);
data=I2C_ReceiveData();
I2C_AcknowledgeConfig(I2C_ACK_CURR);
I2C_GenerateSTOP(ENABLE);
return data;
}
|
|