论坛元老
 
- 积分
- 4938
- 金钱
- 4938
- 注册时间
- 2015-1-10
- 在线时间
- 619 小时
|
发表于 2021-11-14 21:58:55
|
显示全部楼层
出错后重新配置I2C寄存器就可以重新通讯了,给你贴个读取的吧
//Function: AT24CXX_ReadBytes(driver)
//Description: use I2C to read bytes from AT24CXX
//Input: uint16_t addr ---- register address in AT24CXX
// uint16_t count ---- data length
//Output: uint8_t *pdata
//Return: SUCCESS ---- 0
// FAIL ---- 1
uint8_t AT24CXX_ReadBytes(uint16_t addr, uint8_t *pdata, uint16_t count)
{
uint32_t errcnt = 0;
do {
errcnt++;
if (errcnt > SystemCoreClock/1000)
goto error;
} while (I2C_GetFlagStatus(AT24CXX_I2C, I2C_FLAG_BUSY));
/*!< Send START condition */
I2C_GenerateSTART(AT24CXX_I2C, ENABLE);
/*!< Test on EV5 and clear it (cleared by reading SR1 then writing to DR) */
/* Test on EV5 and clear it */
errcnt = 0;
do {
errcnt++;
if (errcnt > SystemCoreClock/1000)
goto error;
} while (!I2C_CheckEvent(AT24CXX_I2C, I2C_EVENT_MASTER_MODE_SELECT));
/*!< Send EEPROM address for write */
I2C_Send7bitAddress(AT24CXX_I2C, AT24CXX_SLAVEADDR, I2C_Direction_Transmitter);
if (EE_TYPE <= AT24C16) {
/*!< Test on EV6 and clear it */
errcnt = 0;
do {
errcnt++;
if (errcnt > SystemCoreClock/1000)
goto error;
} while (!I2C_CheckEvent(AT24CXX_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); /* EV7 */
I2C_SendData(AT24CXX_I2C, addr);
} else {
/*!< Test on EV6 and clear it */
errcnt = 0;
do {
errcnt++;
if (errcnt > SystemCoreClock/1000)
goto error;
} while (!I2C_CheckEvent(AT24CXX_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); /* EV7 */
I2C_SendData(AT24CXX_I2C, addr>>8);
/*!< Test on EV6 and clear it */
errcnt = 0;
do {
errcnt++;
if (errcnt > SystemCoreClock/1000)
goto error;
} while (!I2C_CheckEvent(AT24CXX_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); /* EV7 */
I2C_SendData(AT24CXX_I2C, addr);
}
errcnt = 0;
do {
errcnt++;
if (errcnt > SystemCoreClock/1000)
goto error;
} while (!I2C_CheckEvent(AT24CXX_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(AT24CXX_I2C, ENABLE);
errcnt = 0;
do {
errcnt++;
if (errcnt > SystemCoreClock/1000)
goto error;
} while (!I2C_CheckEvent(AT24CXX_I2C, I2C_EVENT_MASTER_MODE_SELECT));
/* Send address for read */
I2C_Send7bitAddress(AT24CXX_I2C, AT24CXX_SLAVEADDR, I2C_Direction_Receiver);
errcnt = 0;
do {
errcnt++;
if (errcnt > SystemCoreClock/1000)
goto error;
} while (!I2C_CheckEvent(AT24CXX_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); /* EV7 */
for (int i = 0; i < count - 1; i++) {
I2C_AcknowledgeConfig(AT24CXX_I2C, ENABLE);
errcnt = 0;
do {
errcnt++;
if (errcnt > SystemCoreClock/1000)
goto error;
} while (I2C_GetFlagStatus(AT24CXX_I2C, I2C_FLAG_RXNE) == RESET);
*pdata++ = I2C_ReceiveData(AT24CXX_I2C);
}
I2C_AcknowledgeConfig(AT24CXX_I2C, DISABLE);
errcnt = 0;
do {
errcnt++;
if (errcnt > SystemCoreClock/1000)
goto error;
} while (I2C_GetFlagStatus(AT24CXX_I2C, I2C_FLAG_RXNE) == RESET);
*pdata = I2C_ReceiveData(AT24CXX_I2C);
/* Send STOP condition */
I2C_GenerateSTOP(AT24CXX_I2C, ENABLE);
return STATUS_OK;
error:
return STATUS_FAIL;
} |
|