基于STM32F103C8T6的温湿度监测程序,测试通过!
单片机源程序如下:
- //实验平台:ALIENTEK Mini STM32开发板
- //实验名称:SHT10温湿度传感器显示实验
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "lcd.h"
- #include "SHT10.h"
- SHT1x sht1x; //定义传感器结构体
- int main(void)
- {
- u8 t=0,error=0,checksum,value=0;
- volatile u8 T_Display=0,H_Display=0;
- u16 T_Value=0,H_Value=0;
- delay_init(); //延时函数初始化
- uart_init(9600); //USART1初始化
- LED_Init(); //LED初始化
- LCD_Init();
- SHT1x_Init();
-
- POINT_COLOR=RED;//设置字体为红色
- LCD_ShowString(60,120,200,16,16,"SHT1X Test");
- POINT_COLOR=BLUE;//设置字体为蓝色
- LCD_ShowString(60,150,200,16,16,"Temp: . C");
- LCD_ShowString(60,170,200,16,16,"Humi: . %RH");
-
- if(!SHT1X_read_statusreg(&value))
- {
- if(value&0x07)
- {
- for(value=5;value!=0;value--)
- {
- if(!SHT1X_write_statusreg(0))break;
- else SHT1X_softreset();
- }
- }
- }
- while(1)
- {
- //------------------------读取SHT1X传感器数据--------------------------------------
- error+=SHT1x_Measure( &T_Value,&checksum,0); //measure temperature
- error+=SHT1x_Measure( &H_Value,&checksum,1); //measure humidity
-
- if(error!=0)SHT1x_Reset(); //通讯故障,复位传感器
- else
- {
- sht1x.T_Result = T_Value;
- sht1x.H_Result = H_Value;
- SHT1X_Caculation1((float*)&sht1x.T_Result, (float*)&sht1x.H_Result );
-
- sht1x.Temperature=sht1x.T_Result;
- sht1x.Humidity=(u16)sht1x.H_Result;
- sht1x.DEW=SHT1X_dewpoint1(sht1x.T_Result, sht1x.H_Result);
- }
- //------------------------------------------------------------------------------
-
- //显示函数处理
- //-------------------温度显示-------------------------------------------------
-
- if(sht1x.Temperature<0)
- {
- LCD_ShowChar(60+40,150,'-',16,0); //显示负号
- sht1x.Temperature=-sht1x.Temperature; //转为正数
- }
- else LCD_ShowChar(60+40,150,' ',16,0); //去掉负号
-
- LCD_ShowNum(60+40+8,150,sht1x.Temperature/100,2,16); //显示正数部分
- if((sht1x.Temperature%100)>=0 && (sht1x.Temperature%100)<=9 )
- {
- LCD_ShowNum(60+40+32,150,sht1x.Temperature%100,1,16); //显示小数部分
- }
- else
- {
- LCD_ShowNum(60+40+32,150,sht1x.Temperature%100,2,16); //显示小数部分
- }
-
- //-------------------------湿度显示-----------------------------------------------
- LCD_ShowNum(60+40+8,170,sht1x.Humidity/100,2,16); //显示正数部分
- if((sht1x.Humidity%100)>=0 && (sht1x.Humidity%100)<=9 )
- {
- LCD_ShowNum(60+40+32,170,sht1x.Humidity%100,1,16); //显示小数部分
- }
- else
- {
- LCD_ShowNum(60+40+32,170,sht1x.Humidity%100,2,16); //显示小数部分
- }
- //--------------------------------------------------------------------------------
- t++;
- if(t==20)
- {
- t=0;
- LED0=!LED0;
- }
- }
- }
复制代码
下载:
STM32-SHT1X程序.zip
(386.53 KB, 下载次数: 92)
|