综合模块------温湿度检测系统(温湿度模块,LCD602,继电器,电机,风扇,蓝牙模块)

开发板:51开发板  显示: lcd1602 温湿度模块: DHT11 通信模块: HC-08 蓝牙
代码实现:

#include "reg52.h"
#include <intrins.h>
#include <string.h>

sfr AUXR = 0x8E;
#define SIZE 12
sbit DHT = P1^7;
sbit LED1 = P2^0;
sbit LED2 = P2^1;

char data_dht[5];
char buffer[SIZE];

void Delay30ms(void)    //@11.0592MHz
{
    unsigned char data i, j;

    i = 54;
    j = 199;
    do
    {
        while (--j);
    } while (--i);
}

/* 区分数据是0,还是1 */
void Delay40us(void)    //@11.0592MHz
{
    unsigned char data i;

    _nop_();
    i = 15;
    while (--i);
}

void Delay1000ms(void)    //@11.0592MHz
{
    unsigned char data i, j, k;

    _nop_();
    i = 8;
    j = 1;
    k = 243;
    do
    {
        do
        {
            while (--k);
        } while (--j);
    } while (--i);
}


void Uart1_Init(void)    //9600bps@11.0592MHz
{
    PCON &= 0x7F;        //波特率不倍速
    SCON = 0x40;        //8位数据,可变波特率
    AUXR &= 0xBF;        //定时器时钟12T模式
    AUXR &= 0xFE;        //串口1选择定时器1为波特率发生器
    TMOD &= 0xF0;        //设置定时器模式
    TMOD |= 0x20;        //设置定时器模式
    TL1 = 0xFD;            //设置定时初始值
    TH1 = 0xFD;            //设置定时重载值
    ET1 = 0;            //禁止定时器中断
    TR1 = 1;            //定时器1开始计时
    ES = 1;                //使能串口1中断
    EA = 1;
}

void Send_Byte(char cmd)
{
    SBUF = cmd;
    while(!TI);
    TI = 0;
}

void Send_String(char* string)
{
    while(*string != '\0')
    {
        Send_Byte(*string);
        string++;
    }
}


/* 模块初始化,单片机是否识别到该模块 */
void Check_DHT11(void)
{
    /* 主机发送信号 */
    DHT = 1;
    DHT = 0;
    Delay30ms();
    DHT = 1;
    /* DHT11模块开始响应 */
    while(DHT);        /* 卡d点 */
    while(!DHT);       /* 卡e点 */
    while(DHT);        /* 卡f点 */
    //模块已经连上,准备开始发数据
}

void Read_Data_From_DHT11(void)
{
    char i;
    char j;
    char temp;
    char flag;
    
    Check_DHT11();              /* 每次进来要确认模块是否与主机连接好 */
    for(i = 0; i < 5; i++)
    {
        for(j = 0; j < 8; j++)
        {
            while(!DHT);        /* 卡g点,准备传输数据 */
            Delay40us();        /* 区分数据是0,还是1 */
            if(DHT == 1)
            {
                flag = 1;       /* 数据1 */
                while(DHT);     /* 等待数据1的高电平时间结束,直到下一个数据前来 */
            }
            else
            {
                flag = 0;       /* 数据0 */
            }
            
            temp <<= 1;         /* 将8个数据都缓存到这里 */
            temp |= flag;
        }
        data_dht[i] = temp;     /* 将模块一次输出的40个bit,存到数组里面去 */
    }
}

void DHT11_Display(void)
{
    Delay1000ms();
    Read_Data_From_DHT11();
    Send_String("Humi:");
    Send_Byte(data_dht[0]/10 + 0x30);       /* 湿度整数 */
    Send_Byte(data_dht[0]%10 + 0x30);       /* 数据与ASCII码表对应上 */
    Send_Byte('.');
    Send_Byte(data_dht[1]/10 + 0x30);       /* 湿度小数 */
    Send_Byte(data_dht[1]%10 + 0x30);       /* 数据与ASCII码表对应上 */
    Send_String("\r\n");
    
    Send_String("Temp:");
    Send_Byte(data_dht[2]/10 + 0x30);       /* 温度整数 */
    Send_Byte(data_dht[2]%10 + 0x30);       /* 数据与ASCII码表对应上 */
    Send_Byte('.');
    Send_Byte(data_dht[3]/10 + 0x30);       /* 温度小数 */
    Send_Byte(data_dht[3]%10 + 0x30);       /* 数据与ASCII码表对应上 */
    Send_String("\r\n");
}

void main(void)
{
    Uart1_Init();
    Delay1000ms();
    while(1)
    {
        DHT11_Display();
        LED2 = 0;
    }
}


void Uart1_Isr(void) interrupt 4
{
    char temp;
    static int i = 0;
    
    if (RI)                //检测串口1接收中断
    {
        RI = 0;            //清除串口1接收中断请求位
        temp = SBUF;
        if(temp == 'o'|| temp == 'c')
        {
            i = 0;
        }
        buffer[i++] = temp;
        if(i == 12) i = 0;
        
        if(buffer[0] == 'o' && buffer[1] == 'p')
        {
            LED1 = 0;
            memset(buffer, '\0', SIZE);
        }
        
        if(buffer[0] == 'c' && buffer[1] == 'l')
        {
            LED1 = 1;
            memset(buffer, '\0', SIZE);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值