初中生
最后登录1970-1-1
在线时间 小时
注册时间2016-9-19
|
几天前曾在坛子里询问淘宝上LM75A模块该怎么接线。想不到一下子就得到了火哥的回复并解决了问题。
但是火哥分享的软件模拟I2C通讯例程在我STM32F429的板子上不知道为什么跑不起来。
所以我参照<零死角玩转STM32-F429>里I2C相关章节,通过I2C固件库的方法写了一个简单的获取温度程序供大家参考。
我的程序还存在以下问题,希望能到高手指教:
通过STM32 I2C库写的I2C通讯程序,板子与LM75A的连接和通讯必须正确,否则板子会没有任何反应。
应该如何处理STM32 I2C出错的情况??
同时希望火哥能够在新版本的《零死角玩转STM32》里添加I2C出错时的解决方案。
引脚配置部分:
- void IIC_LIM75A_config()
- {
- GPIO_InitTypeDef I2C_GPIO;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2,ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH,ENABLE);
- GPIO_PinAFConfig(GPIOH,GPIO_PinSource4,GPIO_AF_I2C2);
- GPIO_PinAFConfig(GPIOH,GPIO_PinSource5,GPIO_AF_I2C2);
-
- I2C_GPIO.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5;
- I2C_GPIO.GPIO_Mode=GPIO_Mode_AF;
- I2C_GPIO.GPIO_Speed = GPIO_Speed_50MHz;
- I2C_GPIO.GPIO_OType = GPIO_OType_OD;
- I2C_GPIO.GPIO_PuPd = GPIO_PuPd_NOPULL;
-
- GPIO_Init(GPIOH,&I2C_GPIO);
-
- I2C_InitTypeDef I2C_InitStruct;
- I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
- I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
- I2C_InitStruct.I2C_OwnAddress1 =0x00;
- I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
- I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
- I2C_InitStruct.I2C_ClockSpeed = 100000;
-
- I2C_Init(I2C2, &I2C_InitStruct);
- I2C_Cmd(I2C2, ENABLE);
- }
复制代码 温度获取部分:
- float I2C_LM75_Read(void)
- {
- uint8_t TempH,TempL;
- uint16_t data;
- uint32_t TimeOut;
-
- while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY));
- I2C_GenerateSTART(I2C2,ENABLE);
- TimeOut=0x1000;
- while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT))
- {
- if ((TimeOut--) == 0) printf("I2C GenerateSTART 0 Error\r\n");
- }
- I2C_Send7bitAddress(I2C2, 0x90,I2C_Direction_Transmitter);
- TimeOut=0x1000;
- while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
- {
- if ((TimeOut--) == 0) printf("I2C Send7bitAddress 0 Error\r\n");
- }
-
- I2C_SendData(I2C2,0x00);
-
- TimeOut=0x1000;
- while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
- {
- if ((TimeOut--) == 0) printf("I2C SendData 0 Error\r\n");
- }
-
- I2C_GenerateSTART(I2C2,ENABLE);
- TimeOut=0x1000;
- while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT))
- {
- if ((TimeOut--) == 0) printf("I2C GenerateSTART 1 Error\r\n");
- }
-
- I2C_Send7bitAddress(I2C2, 0x90,I2C_Direction_Receiver);
-
- TimeOut=0x1000;
- while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
- {
- if ((TimeOut--) == 0) printf("I2C I2C_Send7bitAddress 1 Error\r\n");
- }
- I2C_AcknowledgeConfig(I2C2,ENABLE);
- TimeOut=0x1000;
- while(!I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_BYTE_RECEIVED))
- {
- if ((TimeOut--) == 0) printf("I2C I2C_ReceiveData 0 Error\r\n");
- }
- TempH = I2C_ReceiveData(I2C2);
-
- printf("TempH:%x\r\n",TempH);
- TimeOut=0x1000;
-
- I2C_AcknowledgeConfig(I2C2,DISABLE);
- I2C_GenerateSTOP(I2C2,ENABLE);
- while(!I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_BYTE_RECEIVED))
- {
- if ((TimeOut--) == 0) printf("I2C I2C_ReceiveData 1 Error\r\n");
- }
- TempL = I2C_ReceiveData(I2C2);
-
-
-
- data=((uint16_t)TempH << 8) | TempL ;
-
-
- return data_convert(data);
- }
复制代码 温度结果数据转换部分:
- float data_convert(uint16_t data)
- {
- uint8_t is_negative=data&(uint16_t)(0x8000);
- uint8_t vaild_data_part=(((data<<1)&(uint16_t)(0xFF00))>>8);
-
- return is_negative==1 ? -(((int)vaild_data_part)*0.5):((int)vaild_data_part)*0.5;
- }
复制代码
调用部分:
- int main()
- {
- float LM75_Temp=0;
- IIC_LIM75A_config();
-
- while(1)
- {
- LM75_Temp=I2C_LM75_Read();
- printf("LM75_Temp %.2f\r\n",LM75_Temp);
- delay_ms(300);
- }
- }
复制代码
|
|