论坛元老
- 积分
- 4584
- 金钱
- 4584
- 注册时间
- 2015-1-10
- 在线时间
- 573 小时
|
发表于 2024-11-9 10:06:38
|
显示全部楼层
写过一个TM1730的,你看着参考吧
uint8_t TM1730_ReadBytes(uint8_t addr, uint8_t *pdata, uint16_t count)
{
uint32_t i = 0, errcnt = 0;
do {
errcnt++;
if (errcnt > SystemCoreClock/1000)
goto error;
} while (I2C_GetFlagStatus(TM1730_I2C, I2C_FLAG_BUSY));
/* Send START condition */
I2C_GenerateSTART(TM1730_I2C, ENABLE);
errcnt = 0;
do {
errcnt++;
if (errcnt > SystemCoreClock/1000)
goto error;
} while (!I2C_CheckEvent(TM1730_I2C, I2C_EVENT_MASTER_MODE_SELECT));
/* Send address for write */
I2C_Send7bitAddress(TM1730_I2C, TM1730_SLAVEADDR, I2C_Direction_Transmitter);
errcnt = 0;
do {
errcnt++;
if (errcnt > SystemCoreClock/1000)
goto error;
} while (!I2C_CheckEvent(TM1730_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); /* EV7 */
I2C_SendData(TM1730_I2C, addr);
errcnt = 0;
do {
errcnt++;
if (errcnt > SystemCoreClock/1000)
goto error;
} while (!I2C_CheckEvent(TM1730_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(TM1730_I2C, ENABLE);
errcnt = 0;
do {
errcnt++;
if (errcnt > SystemCoreClock/1000)
goto error;
} while (!I2C_CheckEvent(TM1730_I2C, I2C_EVENT_MASTER_MODE_SELECT));
/* Send address for read */
I2C_Send7bitAddress(TM1730_I2C, TM1730_SLAVEADDR, I2C_Direction_Receiver);
errcnt = 0;
do {
errcnt++;
if (errcnt > SystemCoreClock/1000)
goto error;
} while (!I2C_CheckEvent(TM1730_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); /* EV7 */
for (i = 0; i < count - 1; i++) {
I2C_AcknowledgeConfig(TM1730_I2C, ENABLE);
errcnt = 0;
do {
errcnt++;
if (errcnt > SystemCoreClock/1000)
goto error;
} while (I2C_GetFlagStatus(TM1730_I2C, I2C_FLAG_RXNE) == RESET);
*pdata++ = I2C_ReceiveData(TM1730_I2C);
}
I2C_AcknowledgeConfig(TM1730_I2C, DISABLE);
errcnt = 0;
do {
errcnt++;
if (errcnt > SystemCoreClock/1000)
goto error;
} while (I2C_GetFlagStatus(TM1730_I2C, I2C_FLAG_RXNE) == RESET);
*pdata = I2C_ReceiveData(TM1730_I2C);
/* Send STOP condition */
I2C_GenerateSTOP(TM1730_I2C, ENABLE);
return STATUS_OK;
error:
I2C_SoftwareResetCmd(TM1730_I2C, ENABLE);
return STATUS_FAIL;
} |
|