STM32F103—有关DHT11温湿度传感器模块的代码

dht11.c

#include "stm32f10x.h"
#include "dht11.h"
#include "SysTick.h"
#include "sys.h"

uint8_t  dat[5]={0x00,0x00,0x00,0x00,0x00};    //存储读取的温湿度信息
uint32_t sum=0;         //存放校验时的求和

void DHT_GPIO_Config_Output( void )
{		
	GPIO_InitTypeDef GPIO_InitStruct;	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE); 
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOE, &GPIO_InitStruct);
}

void DHT_GPIO_Config_Input( void )
{
	GPIO_InitTypeDef GPIO_InitStruct;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE); 
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOE, &GPIO_InitStruct);
}


uint8_t DHT_Read_Byte(void)
{
	uint8_t temp;  
	uint8_t ReadDat=0; 
	uint8_t t = 0;
	uint8_t i; 

	for(i=0;i<8;i++)
	{
		while(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_13)==0&&t<100)  
		{		
			delay_us(1);
			t++;  //防止卡死
		}
		t=0;
		//由于‘0’代码高电平时间26~28us,1代码高电平时间70us,延时30us,可判断高低电平,数字0读取到的是低电平,高电平则反之
		delay_us(30);
		temp=0;
		if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_13)==1) temp=1;		
			
		while(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_13)==1&&t<100)
		{		
			delay_us(1);
			t++;
		}
		t=0;
		ReadDat<<=1; 
		ReadDat|=temp;
	}	
	return ReadDat;
}

uint8_t DHT_Read(void)
{
	uint8_t i;
	uint8_t t = 0;
	DHT_GPIO_Config_Output();
	DHT11_LOW ;  //拉低
	delay_ms(18);
	DHT11_HIGH;  //拉高
	delay_us(40);
	DHT_GPIO_Config_Input();
	delay_us(20);
	//延时20us,低电平80us,还剩60us,检查是否是低电平以确定是否有响应信号
	if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_13)==0)  //如果读取到低电平,证明DHT11响应
	{
		while(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_13)==0&&t<100)//接收响应信号低电平剩余60us,等待变高电平
		{
			delay_us(1);
			t++;			
		}
		t=0;//超过100us自动向下运行,以免卡死
		while(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_13)==1&&t<100)//接收响应信号高电平80us,等待变低电平
			{
				delay_us(1);
				t++;			
			}
		t=0;
		for(i=0;i<5;i++)  //接收40位数据
			{
				dat[i]=DHT_Read_Byte();//读出1个字节
			}
		delay_us(50);//结束信号
	}
	sum=dat[0]+dat[1]+dat[2]+dat[3];
	if(dat[4]==(u8)sum)  //校验  
	{
		return 1;    
	}
	else
		return 0;    
}

dht11.h

#ifndef  __DHT11_H__
#define  __DHT11_H__

#include "sys.h"
#include "stm32f10x.h"

#define DHT11_PORT      GPIOE
#define DHT11_CLK       RCC_APB2Periph_GPIOE
#define DHT11_TRIG      GPIO_Pin_13
#define DHT11           GPIO_ReadInputDataBit(DHT11_PORT,DHT11_TRIG)

#define DHT11_HIGH      PEout(13)
#define DHT11_LOW       PEin(13)

void DHT11_GPIO_OUT(void);
void DHT11_GPIO_IN(void);
uint8_t DHT_Read_Byte(void);
uint8_t DHT_Read(void);

#endif

main.c

#include "stm32f10x.h"
#include "SysTick.h"
#include "dht11.h"
#include "usart.h"

extern uint8_t dat[5];//存储读取的温湿度信息

int main(void)
{	 
	SysTick_Init (7200);
	USART1_Init(9600);	 	//串口初始化为9600
	printf("湿温度传感器初始化成功!\r\n");
	delay_ms(100);  	

	while(1)
	{ 	
		if(DHT_Read())
		{
			printf("湿度:%d.%d%温度:%d.%d℃\r\n",dat[0],dat[1],dat[2],dat[3]);
			delay_ms(1000);
		}
		
	} 
}

(1条消息) STM32F103C8T6基本功能代码_墨云景阳的博客-CSDN博客

  • 7
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值