STM32+SHT11测温湿度,输出显示0,SHT11在ARDUINO上测试正常,附上程序,求指点:
PC9接date,PC8接SCK;
- #include "delay.h"
- #include "timer.h"
- #include "usart.h"
- #include "sys.h"
- #include "adc.h"
- #include "sht10.h"
- #include "lcd12864.h"
- #include "usart2.h"
- u8 t,TL,cnt=0;
- u16 TH,Speed;;
- u8 usart2buf[60];
- u16 dis_temp = 0;
- u16 dis_humi = 0;
- u16 humi_val, temp_val;
- u8 err = 0, checksum = 0;
- float humi_val_real = 0.0;
- float temp_val_real = 0.0;
- u8 temp[2],humi[2];
- int main(void)
- {
- delay_init();
- LCD12864_InitPort();
- LCD12864_Init();
- NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- uart_init(9600); //串口初始化为9600
- USART2_Init(9600); //串口2初始化为9600
- SHT10_GPIO_Config();
- Adc_Init();
-
- LCD_ShowString(3,0,"环境污染检测系统");
- USART2_Send_Data(r_dat1,6);
- delay_ms(5000);
- LCD12864_Clr();
- TIM3_Int_Init(1,2880);//25000 hz的计数频率,0.08ms 1 2880
- while(1)
- {
- err += SHT10_Measure(&temp_val, &checksum, TEMP); //获取温度测量值
- err += SHT10_Measure(&humi_val, &checksum, HUMI); //获取湿度测量值
- if(err != 0)
- SHT10_ConReset();
- else
- {
- SHT10_Calculate(temp_val, humi_val, &temp_val_real, &humi_val_real); //计算实际的温湿度值
- dis_temp = (u16)(temp_val_real*100);
- dis_humi = (u16)(humi_val_real*100);
- }
-
-
- LCD_ShowString(3,0,"Temp");
- temp[0] = dis_temp/1000+48;
- temp[1] = dis_temp%1000/100+48;
- LCD12684_Wcmd(0x8a);
- LCD12684_Wdat(':');
- LCD12684_Wdat(temp[0]);
- LCD12684_Wdat(temp[1]);
-
- LCD_ShowString(3,4,"Humi");
- humi[0] = dis_humi/1000+48;
- humi[1] = dis_humi%1000/100+48;
- LCD12684_Wcmd(0x8e);
- LCD12684_Wdat(':');
- LCD12684_Wdat(humi[0]);
- LCD12684_Wdat(humi[1]);
-
- USART2_Send_Data(r_dat,8);
- delay_ms(1500);
-
- USART2_Receive_Data(usart2buf,&cnt);
- TH = usart2buf[3];
- TL = usart2buf[4];
- TH = (TH<<8)+TL;
-
- for(t=0;t<cnt;t++)
- {
- USART_SendData(USART1, usart2buf[t]);//向串口1发送数据
- while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
- }
- printf("温度:%2.2f℃,湿度:%2.2f%%
",temp_val_real,humi_val_real);
- }
- }
复制代码
第一次接手STM32,程序看着没问题,可就是显示0,求指点
一周热门 更多>