GPIO 发表于 2023-12-17 21:05:33

STC-Y3系列单片机实现驱动DHT11数字温湿度传感器模块 程序源码 并通过串口发送显示

单片机型号:STC12C5A60S2 晶振频率:12Mhz 串口波特率:3125 DHT11接P3.2

#include "stc12c5a60s2.h"
#include "intrins.h"
#include "stdlib.h"
#include "stdio.h"
#define u8 unsigned char
#define u16 unsigned int
#define u32 unsigned long
#define f(i) (float)(i)
sbit DHT11 = P3 ^ 2;

void InitUART(){    //初始化串口(12Mhz@3125bps)
    TMOD = 0x20;
    SCON = 0x50;
    TH1 = 0xF6;
    TL1 = TH1;
    PCON = 0x00;
    TR1 = 1;
}

void txd_byte(u8 c){      //串口发送1字节
    SBUF = c;
    while(!TI);
    TI = 0;
}

void txd_text(u8* text) {      //串口发送字符串
    for (; *text != 0; text++) {
      txd_byte(*text);
    }
}

void Delay18ms(void)        //@12MHz
{
        unsigned char data i, j;

        _nop_();
        _nop_();
        i = 211;
        j = 25;
        do
        {
                while (--j);
        } while (--i);
}

void Delay2000ms(void)        //@12MHz
{
        unsigned char data i, j, k;

        i = 92;
        j = 50;
        k = 238;
        do
        {
                do
                {
                        while (--k);
                } while (--j);
        } while (--i);
}

u8 dht11_us(bit signal){                //DHT11电平时间计数 超时255微秒
        TL0 = 0;
        TF0 = 0;
        while(signal == DHT11 && !TF0);
        return TF0 ? 255 : TL0;
}

void dht11_init(){        //初始化与DHT11有关的寄存器
        AUXR &= 0x7F;                        //定时器时钟12T模式
        TMOD &= 0xF0;                        //设置定时器模式
        TMOD |= 0x02;                        //设置定时器模式
        TH0 = 0;
        TL0 = 0;
        TR0 = 1;
}

/*
DHT11读取温度与湿度 (成功返回0,失败返回非0)
参数1:温度指针(摄氏度) 参数2:湿度指针
*/
u8 dht11(float *temperature,float *humidity){
        u8 i;
        u8 c;
        *temperature = 0;
        *humidity = 0;
        for(i=0;i<5;i++){
                c = 0;
        }
        DHT11 = 0;                                //主动拉低DHT11 18ms(18000us)
        Delay18ms();
        DHT11 = 1;                                //主动拉高
        if(dht11_us(1) == 255){                //等待被拉低超时
                return 1;
        }
        dht11_us(0);        //被拉低83us
        dht11_us(1);        //被拉高87us
        for(i=0;i<40;i++){
                dht11_us(0);                //1bit开始 (被拉低54us)
                        if(dht11_us(1) > 60) {        //超过60us
                                c |= 1 << (7^(i&7));
                        }
                }
        dht11_us(0);
        i = c + c + c + c;
        if(i != c){        //校验和错误
                return 2;
        }
        *humidity = f(c) + f(c)/10;                        //湿度
        *temperature = f(c) + f(c&127)/10;                //温度
        if(c&128){                //零下温度
                *temperature = 0 - *temperature;
        }
        return 0;
}

u8 text;
void main() {
        float temperature;
        float humidity;
        u8 ecode;
        dht11_init();
        InitUART();
        while (1) {
                Delay2000ms();
                ecode = dht11(&temperature,&humidity);
                if(ecode){
                        sprintf(text,"DHT11 错误! 错误代码:0x%02X\r\n",(u16)ecode);
                } else {
                        sprintf(text,"温度:%3.3f℃ 湿度:%3.3f%%\r\n",temperature,humidity);

                }
                txd_text(text);
        }
}
页: [1]
查看完整版本: STC-Y3系列单片机实现驱动DHT11数字温湿度传感器模块 程序源码 并通过串口发送显示