用STM32做的温湿度简单尝试,将数据通过串口在上位机上显示
单片机源程序如下:
- #include "dht11.h"
- //DHT11初始化
- //返回0:初始化成功,1:失败
- u8 DHT11_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStucture;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE);
-
- GPIO_InitStucture.GPIO_Pin=GPIO_Pin_11;
- GPIO_InitStucture.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStucture.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOG,&GPIO_InitStucture);
- GPIO_SetBits(GPIOG,GPIO_Pin_11);
-
- DHT11_Rst();
- return DHT11_Check();
-
- }
- //DHT11复位
- void DHT11_Rst()
- {
- DHT11_IO_OUT(); //设置OUTPUT
- DHT11_DQ_OUT=0; //拉低DQ
- delay_ms(20); //拉低至少18ms
- DHT11_DQ_OUT=1; //DQ=1
- delay_us(20); //主机拉高20~40us
- }
- //等待DHT11的回应
- //返回1:未检测到DHT11的存在
- //返回0:存在
- u8 DHT11_Check()
- {
- u8 retry=0;
- DHT11_IO_IN(); //设置INPUT
- while(DHT11_DQ_IN&&retry<100)//DHT11拉低40~50us
- {
- retry++;
- delay_us(1);
- };
- if(retry>=100)return 1;
- else retry=0;
- while(DHT11_DQ_IN&&retry<100)//DHT11拉低再拉高40~50us
- {
- retry++;
- delay_ms(1);
- };
- if(retry>=100)return 1;
- return 0;
-
- }
- //从DHT11读取一个位
- //返回值:1/0
- u8 DHT11_Read_Bit()
- {
- u8 retry;
- while(DHT11_DQ_IN&&retry<100)//等待变为低电平12~14us开始
- {
- retry++;
- delay_us(1);
-
- }
- retry=0;
- while(!DHT11_DQ_IN&&retry<100)//等待变为高电平 26~28us表示0,116~118us表示1
- {
- retry++;
- delay_us(1);
- }
- delay_us(40);//等待40us
- if(DHT11_DQ_IN)return 1;
- return 0;
- }
- //从DHT11读取一个字节
- //返回值:读到的数据
- u8 DHT11_Read_Byte(void)
- {
- u8 i,dat;
- dat=0;
- for(i=0;i<8;i++)
- {
- dat<<=1;
- dat|=DHT11_Read_Bit();
- }
- return dat;
- }
- //从DHT11读取一次数据
- //temp:温度值(范围:0-50)
- //hum1:湿度值(范围:20%~90%)
- //返回值:0正常,1读取失败
- u8 DHT11_Read_Data(u8 *temp,u8 *hum1)
- {
- u8 buf[5];
- u8 i;
- DHT11_Rst();
- if(DHT11_Check()==0)
- {
- for(i=0;i<5;i++)//读取40位数据
- {
- buf[i]=DHT11_Read_Byte();
- }
- if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])//接收到的40位数据buf[0],buf[1],buf[2],buf[3],分别表示湿度高8位,低8位,温度高8位,低8位,buf[4]表示校验位
- {
- *hum1=buf[0];
- *temp=buf[2];
-
-
-
- }
-
- }else return 1;
- return 0;
- }
- //DHT11输出模式配置
- void DHT11_IO_OUT()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOG,&GPIO_InitStructure);
-
- }
- //DHT11输入模式配置
- void DHT11_IO_IN()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
- GPIO_Init(GPIOG,&GPIO_InitStructure);
- }
复制代码- #include "delay.h"
- #include "sys.h"
- #include "oled.h"
- #include "dht11.h"
- void LED_GPIO_Init(void)//GPIO配置
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- GPIO_SetBits(GPIOB,GPIO_Pin_0);
- }
- int main(void)
- {
- u16 miao;
- u8 temperature;
- u8 humidity;
-
- delay_init();//延时函数初始化
- NVIC_Configuration();//设置NVIC中断分组为2:2位抢占优先级,2位相应优先级,
- OLED_Init();
-
- DHT11_Init();
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- OLED_Clear();
- OLED_ShowString(0,0,"DHT11",8);
- OLED_ShowCHinese(8,0,0);
- OLED_ShowCHinese(16,0,1);
- OLED_ShowString(0,2,"Temp: C",16);
- OLED_ShowString(0,4,"Humi: %",16);
- while(1)
- {
-
-
- printf("温度为:%d\n",temperature);
-
- // if(miao>=4)
- // {
- // PBout(0)=!PBout(0);//LED闪烁
- // DHT11_Read_Data(&temperature,&humidity);//读取温湿度
- // OLED_ShowNum(0,0,temperature,8,8);//显示温度
- // OLED_ShowNum(0,2,humidity,16,16);//显示湿度
- // miao=0;
- //
- // }
- }
-
- }
复制代码
Keil代码下载:
DHT11的Keil代码.7z
(191.88 KB, 下载次数: 69)
|