1.目标用华大HC32F460的 I2C通讯连接一个温度感应模块,读取存储温度模块的数据
已知: 温度模块I2C引脚接芯片端口 SCL->PE2 SDA->PE3 地址引脚接芯片 A0->PE6 A1->PH2 温度模块通讯地址0x49
I2C通讯使用的华大官方例程,但是现在发现我状态寄存器SR读不到变化,卡死在信号状态等待那里了,导致数据读取不出来int32_t main(void)
{
uint8_t u8TxBuf[E2_PAGE_LEN];
uint8_t u8RxBuf[E2_PAGE_LEN];
uint32_t i;
for(i=0ul; i<E2_PAGE_LEN; i++)
{
u8TxBuf[i] = (uint8_t)(i+1u);
}
memset(u8RxBuf, 0x00, E2_PAGE_LEN);
/* 初始化*/
BSP_CLK_Init();
BSP_LED_Init();
I2C_A0A1_Init();
/* 初始化I2C端口 */
PORT_SetFunc(I2C_SCL_PORT, I2C_SCL_PIN, I2C_GPIO_SCL_FUNC, Disable);
PORT_SetFunc(I2C_SDA_PORT, I2C_SDA_PIN, I2C_GPIO_SDA_FUNC, Disable);
/* 启用I2C外设时钟 */
PWC_Fcg1PeriphClockCmd(I2C_FCG_USE, Enable);
/* 初始化I2C外设并启动功能 */
E2_Initialize();
/* I2C内存写入*/
I2C_Mem_Write(E2_ADDRESS, DATA_TEST_ADDR, u8TxBuf, 1u, TIMEOUT);
/* 5mS delay m*/
Ddl_Delay1ms(5ul);
/* I2C存储器读取*/
I2C_Mem_Read(E2_ADDRESS, DATA_TEST_ADDR, u8RxBuf, 1u, TIMEOUT);
/* 比较数据 */
if(0x01u != u8RxBuf[0])
{
/* e2prom byte write error*/
while(1)
{
BSP_LED_Toggle(LED_RED);
Ddl_Delay1ms(500ul);
}
}
/* 5mS delay */
Ddl_Delay1ms(5ul);
/* 写入数据 */
I2C_Mem_Write(E2_ADDRESS, DATA_TEST_ADDR, u8TxBuf, E2_PAGE_LEN, TIMEOUT);
/* 5mS delay*/
Ddl_Delay1ms(5ul);
/* 读取数据*/
I2C_Mem_Read(E2_ADDRESS, DATA_TEST_ADDR, u8RxBuf, E2_PAGE_LEN, TIMEOUT);
/* Compare the data */
for(i=0ul; i<E2_PAGE_LEN; i++)
{
if(u8TxBuf[i] != u8RxBuf[i])
{
/* e2prom page write error*/
while(1)
{
BSP_LED_Toggle(LED_RED);
Ddl_Delay1ms(500ul);
}
}
}
/* e2prom sample success*/
while(1)
{
BSP_LED_Toggle(LED_GREEN);
Ddl_Delay1ms(500ul);
}
}
|