STC15单片机DHT11在LCD1602上显示程序.docx
- 文档编号:9479038
- 上传时间:2023-05-19
- 格式:DOCX
- 页数:23
- 大小:17.57KB
STC15单片机DHT11在LCD1602上显示程序.docx
《STC15单片机DHT11在LCD1602上显示程序.docx》由会员分享,可在线阅读,更多相关《STC15单片机DHT11在LCD1602上显示程序.docx(23页珍藏版)》请在冰点文库上搜索。
STC15单片机DHT11在LCD1602上显示程序
敬告:
没有51单片机基础的人请慎重下载
高质量实用性51单片机STC15W系列程序(4),STC8A系列可参考
STC15单片机DHT11在LCD1602上显示程序
注:
在本节关于DHT11的程序有两种
/*****************************************/
版本一:
/****************************************/
/****************************************/
main函数程序:
#include"Library.h"
unsignedcharstrbuf[4];
//voidValToStr(unsignedchar*str,unsignedchar*source,unsignedcharlen);
unsignedchartmrflag=0;
unsignedcharDHTbuf[5];
voidDelay1000ms()//@11.0592MHz
{
unsignedchari,j,k;
_nop_();
_nop_();
i=43;
j=6;
k=203;
do
{
do
{
while(--k);
}while(--j);
}while(--i);
}
voidmain()
{
unsignedcharDHTstr[8];
P3M1&=0xBF;P3M0&=0xBF;
P2M1&=0xE5;P2M0&=0xE5;
P0M1=0x00;P0M0=0x00;
LCD1602_init();
LCD1602_wBytes(2,0,".",1);
LCD1602_wBytes(8,0,"humi",4);
LCD1602_wBytes(2,1,".",1);
LCD1602_wBytes(8,1,"temp",4);
while
(1)
{
DHT11_start(DHTbuf);
//ValToStr(DHTstr,DHTbuf,sizeof(DHTbuf)-1);
DHTstr[0]=DHTbuf[0]/10+'0';
DHTstr[1]=DHTbuf[0]%10+'0';
DHTstr[2]=DHTbuf[1]/10+'0';
DHTstr[3]=DHTbuf[1]%10+'0';
DHTstr[4]=DHTbuf[2]/10+'0';
DHTstr[5]=DHTbuf[2]%10+'0';
DHTstr[6]=DHTbuf[3]/10+'0';
DHTstr[7]=DHTbuf[3]%10+'0';
LCD1602_wBytes(0,0,DHTstr,2);
LCD1602_wBytes(3,0,DHTstr+2,2);
LCD1602_wBytes(0,1,DHTstr+4,2);
LCD1602_wBytes(3,1,DHTstr+6,2);
Delay1000ms();
Delay1000ms();
}
}
//voidValToStr(unsignedchar*str,unsignedchar*source,unsignedcharlen)
//{
//unsignedchari;
//while(len>0)
//{
//str[i*2]=source[i]/10+'0';
//str[1+i*2]=source[i]%10+'0';
//i++;
//len--;
//}
//}
/*********************************************/
DHT11程序:
/******************************
8bithumidityintegerdata+8bithumiditydecimaldata
8bittemperatureintegerdata+8bittemperaturedecimaldata
8bitchecksum,highbitahead
*******************************/
#include"Library.h"
unsignedcharu8flag;
unsignedcharcheck_buf[5];
unsignedchartmp;
voiddelay_1s()//@11.0592MHz
{
unsignedchari,j,k;
_nop_();
_nop_();
i=43;
j=6;
k=203;
do
{
do
{
while(--k);
}while(--j);
}while(--i);
}
voidDelay10us()//@11.0592MHz
{
unsignedchari;
_nop_();
i=25;
while(--i);
}
voidDelay20ms()//@11.0592MHz
{
unsignedchari,j,k;
_nop_();
_nop_();
i=1;
j=216;
k=35;
do
{
do
{
while(--k);
}while(--j);
}while(--i);
}
unsignedcharRec_8bit()
{
unsignedchari;
unsignedcharret_8bit;
unsignedchartmp;
for(i=0;i<8;i++)
{
while(!
DHT11port);
Delay10us();
Delay10us();
Delay10us();
tmp=0;
if(DHT11port)
{
tmp=1;
}
u8flag=2;
while((DHT11port)&&u8flag++);
if(u8flag==1)break;
ret_8bit<<=1;
ret_8bit|=tmp;
}
returnret_8bit;
}
voidDHT11_start(unsignedchar*rec_buf)
{
//delay_1s();
DHT11port=0;
Delay20ms();
DHT11port=1;
Delay10us();
Delay10us();
Delay10us();
Delay10us();
//DHT11port=1;
if(!
DHT11port)
{
u8flag=2;
//while(!
DHT11port);
while((!
DHT11port)&&u8flag++);
//if(DHT11port)
//{
//while(DHT11port);
u8flag=2;
while((DHT11port)&&u8flag++);
check_buf[0]=Rec_8bit();
check_buf[1]=Rec_8bit();
check_buf[2]=Rec_8bit();
check_buf[3]=Rec_8bit();
check_buf[4]=Rec_8bit();
if(!
DHT11port)
{
while(!
DHT11port);
}
DHT11port=1;
tmp=check_buf[0]+check_buf[1]+check_buf[2]+check_buf[3];
if(tmp==check_buf[4])
{
rec_buf[0]=check_buf[0];
rec_buf[1]=check_buf[1];
rec_buf[2]=check_buf[2];
rec_buf[3]=check_buf[3];
}
//}
}
else
{
rec_buf[0]=0;
rec_buf[1]=0;
rec_buf[2]=0;
rec_buf[3]=0;
}
}
/*********************************************/
LCD1602程序:
#include"Library.h"
voidLCD1602_rsta()
{
unsignedchartmp;
P0=0xFF;//thisisamust
rs=0;
rw=1;
do{
en=1;
//Delay1us();
tmp=P0;
//Delay1us();
en=0;
}while(tmp&0x80);
}
voidLCD1602_wdat(unsignedchardat)
{
LCD1602_rsta();
rs=1;
rw=0;
P0=dat;
en=1;
//Delay1us();
en=0;
}
voidLCD1602_wcmd(unsignedcharcmd)
{
LCD1602_rsta();
rs=0;
rw=0;
P0=cmd;
en=1;
//Delay1us();
en=0;
}
voidSetcursor(unsignedcharx,unsignedchary)
{
if(y==0)
x=x+0x00;
elseif(y==1)
x=x+0x40;
LCD1602_wcmd(x|0x80);
}
voidLCD1602_wBytes(unsignedcharx,unsignedchary,unsignedchar*buf,unsignedcharbuf_len)
{
Setcursor(x,y);
while(buf_len>0)
{
LCD1602_wdat(*buf++);
buf_len--;
}
}
voidOnCursor()
{
LCD1602_wcmd(0x0F);
}
voidOffCursor()
{
LCD1602_wcmd(0x0C);
}
voidLCD1602_init()
{
//Delay15ms();
//LCD1602_wcmd(0x38);
//Delay5ms();
LCD1602_wcmd(0x38);
//LCD1602_wcmd(0x08);
LCD1602_wcmd(0x06);
LCD1602_wcmd(0x0C);
LCD1602_wcmd(0x01);
}
/*****************************************/
Library.h
#ifndef_Library_H
#define_Library_H
#include
#include
#defineMAIN_Fosc11059200L
sbitDHT11port=P3^6;
voidDHT11_start(unsignedchar*rec_buf);
sbitrs=P2^4;
sbitrw=P2^3;
sbiten=P2^1;
voidLCD1602_init();
voidLCD1602_wBytes(unsignedcharx,unsignedchary,unsignedchar*buf,unsignedcharbuf_len);
#endif
/*****************************************/
版本二(结构体):
/****************************************/
/****************************************/
main程序:
#include"Library.h"
#include"string.h"
voidDelay1000ms()//@11.0592MHz
{
unsignedchari,j,k;
_nop_();
_nop_();
i=43;
j=6;
k=203;
do
{
do
{
while(--k);
}while(--j);
}while(--i);
}
voidmain()
{
unsignedcharDHTstr[8];
P3M1&=0xBF;P3M0&=0xBF;
P2M1&=0xE5;P2M0&=0xE5;
P0M1=0x00;P0M0=0x00;
LCD1602_init();
LCD1602_wBytes(2,0,".",1);
LCD1602_wBytes(8,0,"humi",4);
LCD1602_wBytes(2,1,".",1);
LCD1602_wBytes(8,1,"temp",4);
while
(1)
{
DHT11_start();
//memset(DHTstr,0,8);
DHTstr[0]=ht.humi_h+'0';
DHTstr[1]=ht.humi_l+'0';
DHTstr[2]=ht.humi_dh+'0';
DHTstr[3]=ht.humi_dl+'0';
DHTstr[4]=ht.temp_h+'0';
DHTstr[5]=ht.temp_l+'0';
DHTstr[7]=ht.temp_dh+'0';
DHTstr[6]=ht.temp_dl+'0';
LCD1602_wBytes(0,0,DHTstr,2);
LCD1602_wBytes(3,0,DHTstr+2,2);
LCD1602_wBytes(0,1,DHTstr+4,2);
LCD1602_wBytes(3,1,DHTstr+6,2);
Delay1000ms();
Delay1000ms();
}
}
/***********************************************/
DHT11程序:
/******************************
8bithumidityintegerdata+8bithumiditydecimaldata
8bittemperatureintegerdata+8bittemperaturedecimaldata
8bitchecksum,highbitahead
*******************************/
#include"Library.h"
unsignedcharu8flag;
unsignedcharcheck_buf[5];
unsignedchartmp;
unsignedcharrec_buf[4];
htstructht;
voiddelay_1s()//@11.0592MHz
{
unsignedchari,j,k;
_nop_();
_nop_();
i=43;
j=6;
k=203;
do
{
do
{
while(--k);
}while(--j);
}while(--i);
}
voidDelay10us()//@11.0592MHz
{
unsignedchari;
_nop_();
i=25;
while(--i);
}
voidDelay20ms()//@11.0592MHz
{
unsignedchari,j,k;
_nop_();
_nop_();
i=1;
j=216;
k=35;
do
{
do
{
while(--k);
}while(--j);
}while(--i);
}
unsignedcharRec_8bit()
{
unsignedchari;
unsignedcharret_8bit;
unsignedchartmp;
for(i=0;i<8;i++)
{
while(!
DHT11port);
Delay10us();
Delay10us();
Delay10us();
tmp=0;
if(DHT11port)
{
tmp=1;
}
u8flag=2;
while((DHT11port)&&u8flag++);
if(u8flag==1)break;
ret_8bit<<=1;
ret_8bit|=tmp;
}
returnret_8bit;
}
voidDHT11_start()
{
//delay_1s();
DHT11port=0;
Delay20ms();
DHT11port=1;
Delay10us();
Delay10us();
Delay10us();
Delay10us();
//DHT11port=1;
if(!
DHT11port)
{
u8flag=2;
while((!
DHT11port)&&u8flag++);
u8flag=2;
while((DHT11port)&&u8flag++);
check_buf[0]=Rec_8bit();
check_buf[1]=Rec_8bit();
check_buf[2]=Rec_8bit();
check_buf[3]=Rec_8bit();
check_buf[4]=Rec_8bit();
if(!
DHT11port)
{
while(!
DHT11port);
}
DHT11port=1;
tmp=check_buf[0]+check_buf[1]+check_buf[2]+check_buf[3];
if(tmp==check_buf[4])
{
rec_buf[0]=check_buf[0];
rec_buf[1]=check_buf[1];
rec_buf[2]=check_buf[2];
rec_buf[3]=check_buf[3];
}
ht.humi_h=rec_buf[0]/10;
ht.humi_l=rec_buf[0]%10;
ht.humi_dh=rec_buf[1]/10;
ht.humi_dl=rec_buf[1]%10;
ht.temp_h=rec_buf[2]/10;
ht.temp_l=rec_buf[2]%10;
ht.temp_dh=rec_buf[3]/10;
ht.temp_dl=rec_buf[3]%10;
}
else
{
ht.humi_h=0;
ht.humi_l=0;
ht.humi_dh=0;
ht.humi_dl=0;
ht.temp_h=0;
ht.temp_l=0;
ht.temp_dh=0;
ht.temp_dl=0;
}
}
/************************************************/
LCD1602程序:
#include"Library.h"
voidLCD1602_rsta()
{
unsignedchartmp;
P0=0xFF;//thisisamust
rs=0;
rw=1;
do{
en=1;
//Delay1us();
tmp=P0;
//Delay1us();
en=0;
}while(tmp&0x80);
}
voidLCD1602_wdat(unsignedchardat)
{
LCD1602_rsta();
rs=1;
rw=0;
P0=dat;
en=1;
//Delay1us();
en=0;
}
voidLCD1602_wcmd(unsignedcharcmd)
{
LCD1602_rsta();
rs=0;
rw=0;
P0=cmd;
en=1;
//Delay1us();
en=0;
}
voidSetcursor(unsignedcharx,unsignedchary)
{
if(y==0)
x=x+0x00;
elseif(y==1)
x=x+0x40;
LCD1602_wcmd(
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- STC15 单片机 DHT11 LCD1602 显示 程序