/**
* @brief 从I2C1总线上的某一器件的某一起始地址中读取一定字节的数据到数组中
* @param driver_Addr:I2C器件地址
* @param start_Addr:起始字节地址
* @param number_Bytes:要读取的字节数量(小于一页)
* @param read_Buffer:存放读取数据的数组指针
* @retval 是否读取成功
*/
u8 I2C1_Read_NBytes(u8 driver_Addr, u8 start_Addr, u8 number_Bytes, u8 *read_Buffer)
{
uint8_t read_Num;
u8 I2C_Timeout;
I2C_Timeout = 200;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) != RESET)
{
if((I2C_Timeout--) == 0)
{
return 1;
}
}
I2C_TransferHandling(I2C1, driver_Addr, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); //I2C_No_StartStop I2C_Generate_Start_Write
I2C_Timeout = 200;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) == RESET)
{
if((I2C_Timeout--) == 0)
{
return 1;
}
}
I2C_SendData(I2C1, start_Addr);
I2C_Timeout = 200;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TC) == RESET)
{
if((I2C_Timeout--) == 0)
{
return 1;
}
}
I2C_TransferHandling(I2C1, driver_Addr, number_Bytes, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
for(read_Num = 0; read_Num { I2C_Timeout = 200; while(I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == RESET) { if((I2C_Timeout--) == 0) { return 1; } } read_Buffer[read_Num] = I2C_ReceiveData(I2C1); } I2C_Timeout = 200; while(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF) == RESET); { if((I2C_Timeout--) == 0) { return 1; } } return 0; } u8 I2C1_Write_NBytes(u8 driver_Addr, u8 start_Addr, u8 number_Bytes, u8 *write_Buffer) { u8 write_Num; u8 I2C_Timeout; I2C_Timeout = 200; while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) != RESET) { if((I2C_Timeout--) == 0) { //I2C_ClearFlag(I2C1, I2C_FLAG_BUSY); return 1; } } I2C_TransferHandling(I2C1, driver_Addr, number_Bytes+1, I2C_AutoEnd_Mode, I2C_Generate_Start_Write); I2C_Timeout = 200; while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) == RESET) { if((I2C_Timeout--) == 0) { return 1; } } I2C_SendData(I2C1, start_Addr); //while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) == RESET); //I2C_TransferHandling(I2C1, driver_Addr, number_Bytes, I2C_AutoEnd_Mode, I2C_No_StartStop); for(write_Num = 0; write_Num { I2C_Timeout = 200; while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) == RESET) { if((I2C_Timeout--) == 0) { return 1; } } I2C_SendData(I2C1, write_Buffer[write_Num]); } I2C_Timeout = 200; while(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF) == RESET) { if((I2C_Timeout--) == 0) { return 1; } } return 0; }
上一篇:STM32学习笔记之EXTI(外部中断)
下一篇:STM32F030控制蜂鸣器定时响和控制LED亮
推荐阅读最新更新时间:2024-03-16 16:13