首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >DHT11仿真(proteus8.15)代码,纯享版(好移植)

DHT11仿真(proteus8.15)代码,纯享版(好移植)

作者头像
紫昭
发布2025-11-29 08:11:38
发布2025-11-29 08:11:38
120
举报

仿真软件:proteus8.15(特地强调一下,因为这个仿真软件感觉有点版本Bug)

仿真效果如下

https://live.csdn.net/v/embed/433469

DHT11驱动代码

DHT11.h

代码语言:javascript
复制
#ifndef __DHT11_H
#define __DHT11_H

//修改下面三个参数来进行引脚的修改(更加方便进行移植)
#define DHT11_PIN              GPIO_Pin_7
#define DHT11_RCC              RCC_APB2Periph_GPIOB
#define DHT11_GPIO_PORT        GPIOB

#define DHT11_LOGIC_HIGH       1
#define DHT11_LOGIC_LOW        0

#define DHT11_DATA_OUTPUT(a)  if (a) \
                                GPIO_SetBits(DHT11_GPIO_PORT, DHT11_PIN);\
                                else \
                                GPIO_ResetBits(DHT11_GPIO_PORT, DHT11_PIN)

#define DHT11_DATA_INPUT()     GPIO_ReadInputDataBit(DHT11_GPIO_PORT, DHT11_PIN)

typedef struct
{
    uint8_t humidity_integer;    //湿度的整数部分
    uint8_t humidity_decimal;    //湿度的小数部分
    uint8_t temperature_integer; //温度的整数部分
    uint8_t temperature_decimal; //温度的小数部分
    uint8_t checksum;            
} DHT11_Sensor_Data_TypeDef;

void DHT11_Sensor_GPIO_Configuration(void);
static void DHT11_Sensor_SetInputPullUpMode(void);
static void DHT11_Sensor_SetOutputPushPullMode(void);
uint8_t DHT11_ReadSensorData(DHT11_Sensor_Data_TypeDef *sensorData);
static uint8_t DHT11_ReadDataByte(void);

#endif

DHT11.c

代码语言:javascript
复制
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "DHT11.h"

void DHT11_Sensor_GPIO_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(DHT11_RCC, ENABLE); 
    GPIO_InitStructure.GPIO_Pin = DHT11_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(DHT11_GPIO_PORT, &GPIO_InitStructure);

    GPIO_SetBits(DHT11_GPIO_PORT, DHT11_PIN);
}

static void DHT11_Sensor_SetInputPullUpMode(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = DHT11_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(DHT11_GPIO_PORT, &GPIO_InitStructure);
}

static void DHT11_Sensor_SetOutputPushPullMode(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    GPIO_InitStructure.GPIO_Pin = DHT11_PIN;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(DHT11_GPIO_PORT, &GPIO_InitStructure);
}

static uint8_t DHT11_ReadDataByte(void)
{
    uint8_t i, temp = 0;

    for (i = 0; i < 8; i++)
    {
        while (DHT11_DATA_INPUT() == Bit_RESET);

        Delay_us(40);

        if (DHT11_DATA_INPUT() == Bit_SET)
        {
            while (DHT11_DATA_INPUT() == Bit_SET);

            temp |= (uint8_t)(0x01 << (7 - i));
        }
        else
        {
            temp &= (uint8_t) ~(0x01 << (7 - i));
        }
    }
    return temp;
}

uint8_t DHT11_ReadSensorData(DHT11_Sensor_Data_TypeDef *sensorData)
{
    DHT11_Sensor_SetOutputPushPullMode();
    DHT11_DATA_OUTPUT(DHT11_LOGIC_LOW);
    Delay_ms(18);

    DHT11_DATA_OUTPUT(DHT11_LOGIC_HIGH);

    Delay_us(30);

    DHT11_Sensor_SetInputPullUpMode();

    if (DHT11_DATA_INPUT() == Bit_RESET)
    {
        while (DHT11_DATA_INPUT() == Bit_RESET);

        while (DHT11_DATA_INPUT() == Bit_SET);

        sensorData->humidity_integer = DHT11_ReadDataByte();
        sensorData->humidity_decimal = DHT11_ReadDataByte();
        sensorData->temperature_integer = DHT11_ReadDataByte();
        sensorData->temperature_decimal = DHT11_ReadDataByte();
        sensorData->checksum = DHT11_ReadDataByte();

        DHT11_Sensor_SetOutputPushPullMode();
        DHT11_DATA_OUTPUT(DHT11_LOGIC_HIGH);

        if (sensorData->checksum == sensorData->humidity_integer + sensorData->humidity_decimal + sensorData->temperature_integer + sensorData->temperature_decimal)
            return SUCCESS;
        else
            return ERROR;
    }
    else
    {
        return ERROR;
    }
}

main.c

给出main.c函数进行运行的参考,单独移植DHT11代码的话,就只用参考与其相关的代码即可

代码语言:javascript
复制
#include "stm32f10x.h"
#include  "OLED.h"  //OLED显示屏 
#include "PWM.h"    //PWM输出
#include "motor.h"  //直流电机
#include "Delay.h"
#include "DHT11.h"
#include "stdio.h"
			  
char dis1[16];	
DHT11_Sensor_Data_TypeDef DHT11_Data;

int8_t Speed = 0;

int main(void)
{ 
	uint8_t tem1;
	uint8_t hum1;
	OLED_Init();
	Motor_Init();
	DHT11_Sensor_GPIO_Configuration();
	
	OLED_ShowString(1, 1, "Speed:");
	OLED_ShowString(2, 1, "temp:");
	OLED_ShowString(3, 1, "humidity:");
	while(1){ 
		Motor_SetSpeed(Speed);
		OLED_ShowSignedNum(1, 7, Speed, 3);
		if(DHT11_ReadSensorData(&DHT11_Data) == SUCCESS)
		{
			OLED_ShowNum(2, 10, DHT11_Data.humidity_integer, 2);
			OLED_ShowString(2, 12, ".");
			OLED_ShowNum(2, 13, DHT11_Data.humidity_decimal, 2);
			
			OLED_ShowNum(3, 10, DHT11_Data.temperature_integer, 2);
			OLED_ShowString(3, 12, ".");
			OLED_ShowNum(3, 13, DHT11_Data.temperature_decimal, 1);
			
		}
		
		if((DHT11_Data.temperature_integer>30)&&(DHT11_Data.temperature_integer<35))
		{
			Speed=25;
		}else if((DHT11_Data.temperature_integer>=35)&&(DHT11_Data.temperature_integer<40))
		{
			Speed=50;
		}
		else if((DHT11_Data.temperature_integer>40)&&(DHT11_Data.temperature_integer<50))
		{
			Speed=100;
		}else if(DHT11_Data.temperature_integer<=30)
		{
			Speed=0;
		}
		 }
}

后续将对传感器专栏进行更新,致力于打造优质的传感器移植专栏,使传感器驱动更为便捷。

觉得不错的话,希望能够一键三连,多多关注!!!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-11-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 仿真效果如下
    • DHT11驱动代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档