欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > STC89C51 温湿度传感器+LCD1602+电位器

STC89C51 温湿度传感器+LCD1602+电位器

2024/10/23 15:25:11 来源:https://blog.csdn.net/cykaw2590/article/details/143060340  浏览:    关键词:STC89C51 温湿度传感器+LCD1602+电位器

电位器接线:

VCC 接开发板5V

GND接开发板GND

OUT接LCD1602 VO口

LCD1602接线:

VDD接开发板5V

VSS接开发板GND

VO接电位器OUT

RS接P1.1

RW接P1.2

EN接P1.3

D0-D7依次接P0.0-P0.7

温湿度传感器接线:

VCC接开发板5V

GND接开发板GND

DATA接开发板P1.5

代码:

#include "reg52.h"
#include "intrins.h"
#define databuffer P0
sfr AUXR = 0x8E;
sbit dht = P1^5;
char datas[5];
sbit RS = P1^1;
sbit RW = P1^2;
sbit EN = P1^3;
sbit led = P3^1;
void check_busy()
{
    char tmp = 0x80;
    databuffer = 0x80;
    while(tmp & 0x80)
    {
        RS = 0;
        RW = 1;
        EN = 0;
        _nop_();
        EN = 1;
        _nop_();
        _nop_();//Òª¼ÓÁ½¸önop
        tmp = databuffer;
        _nop_();
        EN = 0;
        _nop_();
    }
}
void Write_Cmd_Func(char cmd)
{
    check_busy();
    RS = 0;
    RW = 0;
    EN = 0;
    _nop_();
    databuffer = cmd;
    _nop_();
    EN = 1;
    _nop_();
    _nop_();
    EN = 0;
    _nop_();
}
void Write_Data_Func(char datas)
{
    check_busy();
    RS = 1;
    RW = 0;
    EN = 0;
    _nop_();
    databuffer = datas;
    _nop_();
    EN = 1;
    _nop_();
    _nop_();
    EN = 0;
    _nop_();
}
void WDF(char datas)
{
    RS = 1;
    RW = 0;
    EN = 0;
    _nop_();
    databuffer = datas;
    _nop_();
    EN = 1;
    _nop_();
    _nop_();
    EN = 0;
    _nop_();
}
void Delay15ms()        //@11.0592MHz
{
    unsigned char i, j;

    i = 27;
    j = 226;
    do
    {
        while (--j);
    } while (--i);
}
void Delay5ms()        //@11.0592MHz
{
    unsigned char i, j;

    i = 9;
    j = 244;
    do
    {
        while (--j);
    } while (--i);
}

void LCD1602_INIT()
{
    Delay15ms();
    //WDF(0x38);
    Write_Cmd_Func(0x38);//Òª¼ì²â
    Delay5ms();
    Write_Cmd_Func(0x38);
    Write_Cmd_Func(0x08);
    Write_Cmd_Func(0x01);
    Write_Cmd_Func(0x06);
    Write_Cmd_Func(0x0c);
}
void showline(char row,char col,char *string)
{
    switch(row){
        case 1:
            Write_Cmd_Func(0x80+col);
            while(*string){
                Write_Data_Func(*string);
                string++;
            }
            break;
        case 2:
            Write_Cmd_Func(0x80+0x40+col);
            while(*string){
                Write_Data_Func(*string);
                string++;
            }
            break;
    }
}

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

    _nop_();
    i = 11;
    j = 130;
    k = 111;
    do
    {
        do
        {
            while (--k);
        } while (--j);
    } while (--i);
}

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

    i = 54;
    j = 199;
    do
    {
        while (--j);
    } while (--i);
}
void Delay50us()        //@11.0592MHz
{
    unsigned char i;

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

void Delay60us()        //@11.0592MHz
{
    unsigned char i;

    i = 25;
    while (--i);
}

void cDHT()
{
    dht = 1;
    dht = 0;
    Delay30ms();
    dht = 1;
    while(dht);
    while(!dht);
    while(dht);
}
void rd()
{
    int i;
    int j;
    char tmp;
    char flag;
    cDHT();
    for (i=0;i<5;i++)
    {
        for(j=0;j<8;j++)
        {
            while(!dht);
            Delay50us();
            if (dht)
            {
                flag = 1;
                while(dht);
            }
            else
            {
                flag = 0;
            }
            tmp <<= 1;
            tmp |= flag;
        }
        datas[i] = tmp;
    }
}
void UartInit(void)        //9600bps@11.0592MHz
{
    PCON &= 0x7F;        //??????
    SCON = 0x50;        //8???,?????
    AUXR &= 0xBF;        //???1???Fosc/12,?12T
    AUXR &= 0xFE;        //??1?????1???????
    TMOD &= 0x0F;        //?????1???
    TMOD |= 0x20;        //?????1?8???????
    TL1 = 0xFD;        //??????
    TH1 = 0xFD;        //????????
    ET1 = 0;        //?????1??
    TR1 = 1;        //?????1
}
void sendString(char* str)
{
    while(*str !='\0')
    {
        SBUF = *str;
        while(!TI);
        TI = 0;
        str++;
    }
}
void main()
{
    char sd[2];
    char sdx[2];
    char wd[2];
    char wdx[2];
    char textsd[11];
    char textwd[11];
    LCD1602_INIT();
    Delay1500ms();
    UartInit();
    while(1)//ÈÃmain²»Í˳ö£¬²»È»»áÖØмÓÔØmainµÆ»áÉÁ
    {
        rd();
        sd[0] = datas[0]/10 + 0x30;
        sd[1] = datas[0]%10 + 0x30;
        sd[2] = 0;
        sendString("H: ");
        sendString(sd);
        sendString(".");
        sdx[0] = datas[1]/10 + 0x30;
        sdx[1] = datas[1]%10 + 0x30;
        sdx[2] = 0;
        sendString(sdx);
        sendString("  ");
        wd[0] = datas[2]/10 + 0x30;
        wd[1] = datas[2]%10 + 0x30;
        wd[2] = 0;
        sendString("T: ");
        sendString(wd);
        sendString(".");
        wdx[0] = datas[3]/10 + 0x30;
        wdx[1] = datas[3]%10 + 0x30;
        wdx[2] = 0;
        sendString(wdx);
        sendString("\r\n");
        showline(1,0,sd);
        textsd[0] = 'H';
        textsd[1] = ':';
        textsd[2] = ' ';
        textsd[3] = sd[0];
        textsd[4] = sd[1];
        textsd[5] = '.';
        textsd[6] = sdx[0];
        textsd[7] = sdx[1];
        textsd[8] = ' ';
        textsd[9] = '%';
        textsd[10] = 0;
        showline(1,0,textsd);
        textwd[0] = 'T';
        textwd[1] = ':';
        textwd[2] = ' ';
        textwd[3] = wd[0];
        textwd[4] = wd[1];
        textwd[5] = '.';
        textwd[6] = wdx[0];
        textwd[7] = wdx[1];
        textwd[8] = 223;
        textwd[9] = 'C';
        textwd[10] = 0;
        showline(2,0,textwd);
        Delay1500ms();
    }
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com