找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1972|回复: 0
打印 上一主题 下一主题
收起左侧

基于STC12C5A60S2单片机的DHT11测量程序 1602液晶屏显示

[复制链接]
跳转到指定楼层
楼主
ID:504443 发表于 2020-2-24 08:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
基于STC12C5A60S2单片机的DHT11测量,采用1602液晶屏显示。
供大家学习交流。

#include<STC12C5A60S2.h>
#include"intrins.h"
#define uchar unsigned char
#define uint unsigned int

sbit e=P1^4;//液晶屏使能端
sbit lcdrs=P1^7;
sbit rw=P1^6;
sbit io=P1^0;  //dht11data端接单片机的P1^0口
uchar  table_humidity[13]={0};
uchar  table_temperature[13]={0};
uchar num;
uchar hh,hl,th,tl;//humidity 湿度 temperature 温度
uchar data_byte;
void Delay20ms()  //@11.0592MHz 20ms
{
unsigned char i, j, k;
i = 1;
j = 216;
k = 35;
do
{
  do
  {
   while (--k);
  } while (--j);
} while (--i);
}
void Delay60us()  //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 1;
j = 161;
do
{
  while (--j);
} while (--i);
}

void Delay10us()  //@11.0592MHz  10us
{
unsigned char i;
_nop_();
_nop_();
_nop_();
i = 24;
while (--i);
}
void Delay27us()  //@11.0592MHz
{
unsigned char i;
_nop_();
_nop_();
_nop_();
i = 71;
while (--i);
}
void Delay30us()  //@11.0592MHz
{
unsigned char i;
i = 80;
while (--i);
}

void Delay50us()  //@11.0592MHz 50us
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 1;
j = 133;
do
{
  while (--j);
} while (--i);
}

void delay(uchar num)//延时1MS
{

uint i,j;
for(i=num;i>0;i--)
  for(j=920;j>0;j--);  
}
void start()//开始信号
{
  io=1;
  Delay10us();//10us
  io=0;
  Delay20ms();// 主机把总线拉低必须大于18ms保证DHT11能检测到起始信号
  io=1;    //发送开始信号结束后拉高电平延时20-40us
  Delay10us();//以下三个延时函数差不多为30us符合要求
  Delay10us();
  Delay10us();
}
uchar receive_byte()//接收一个字节
{
  
  uchar i,temp;
data_byte=0x00;
  for(i=0;i<8;i++)//接收8bit的数据
  {
  while(!io);//等待50us的低电平开始信号结束
//  delay1();//开始信号结束之后延时26us-28us以下三个延时函数
//  delay1();
//  delay1();
//  temp=0;//时间为26us-28us表示接收的为数据'0'
//  if(io==1)
//  temp=1; //如果26us-28us之后还为高电平则表示接收的数据为'1'
Delay30us();
  if(io==1)
   temp=1;
  else
   temp=0;
data_byte<<=1;//接收的数据为高位在前左移
  data_byte|=temp;
while(io);//等待数据信号高电平'0'为26us-28us'1'为70us

  }
  return data_byte;

}

void receive()//接收数据//
{
  uchar T_H,T_L,R_H,R_L,check,num_check;
  start();//开始信号//
  io=1;   //主机设为输入判断从机DHT11响应信号
  if(!io)//判断从机是否有低电平响应信号//
  {  
while(!io);//判断从机发出 80us 的低电平响应信号是否结束//
while(io);//判断从机发出 80us 的高电平是否结束如结束则主机进入数据接收状态
R_H=receive_byte();//湿度高位
  R_L=receive_byte();//湿度低位
T_H=receive_byte();//温度高位
T_L=receive_byte();//温度低位
check=receive_byte();//校验位
io=0; //当最后一bit数据接完毕后从机拉低电平50us//
Delay50us();//50us
io=1;//总线由上拉电阻拉高进入空闲状态
num_check=R_H+R_L+T_H+T_L;
if(num_check==check)//判断读到的四个数据之和是否与校验位相同
  {
   hh=R_H;
   hl=R_L;
   th=T_H;  
   tl=T_L;
   check=num_check;
  }
  
  }
}

void write_com (uchar com)//写入指令
{
e=0;
lcdrs=0;
rw=0;
P2=com;
delay(5);
e=1;
delay(5);
e=0;
}
void write_dat(uchar dat)//写入数据
{
e=0;
lcdrs=1;
  rw=0;
P2=dat;
delay(5);
e=1;
delay(5);
e=0;
}

void init()
{
delay(15);  
e=0;
rw=0;
write_com(0x38);//设置16*2显示,5*7点阵,8位数据接口
delay(5);
write_com(0x08);    /*显示关闭*/

write_com(0x01);//显示清0,数据指针为0
write_com(0x06);//写一个字符后地址指针加1
write_com(0x0c);//设置开显示,不显示光标
}
void table0()
{
  table_humidity[0]='s';
  table_humidity[1]='h';
  table_humidity[2]='i';
  table_humidity[3]='d';
  table_humidity[4]='u';
  table_humidity[5]=':';

  table_temperature[0]='w';
  table_temperature[1]='e';
  table_temperature[2]='n';
  table_temperature[3]='d';
  table_temperature[4]='u';
  table_temperature[5]=':';
}
void table1()
{
uchar ss,sg,sx1,sx2,ws,wg,wx1,wx2;//数据变换  hh,hl,th,tl;
ss=hh/10+'0';
sg=hh%10+'0';
sx1=hl/10+'0';
sx2=hl%10+'0';
ws=th/10+'0';
wg=th%10+'0';
wx1=tl/10+'0';
wx2=tl%10+'0';

  
  table_humidity[6]=ss;
  table_humidity[7]=sg;
  table_humidity[8]='.';
  table_humidity[9]=sx1;
  table_humidity[10]=sx2;
  table_humidity[11]='%';
  table_humidity[12]=' ';
  
  
  table_temperature[6]=ws;
  table_temperature[7]=wg;
  table_temperature[8]='.';
  table_temperature[9]=wx1;
  table_temperature[10]=wx2;
  table_temperature[11]=0X27;
  table_temperature[12]='C';
  
}
void main()
{
e=0;//液晶屏使能端
lcdrs=0;
rw=0;
  init();//初始化液晶

while(1)
{
receive();
table0();
  table1();
// write_com(0x80);
// write_dat(hh);
// write_com(0x80+0x01);
// write_dat(hh/10+'0');
// write_com(0x80+0x02);
// write_dat(hh%10+'0');
// write_com(0x80+0x03);
// write_dat(hl);


write_com(0x80+0x02);//将数据写在液晶屏第一行第一个字处 后移2格  
for(num=0;num<13;num++)
{
  write_dat(table_humidity [num]);
  delay(5);
}
    write_com(0x80+0x40+0x02);//定位到第二行开始处 后移2格
for(num=0;num<13;num++)
{
  write_dat(table_temperature [num]);
  delay(5);
}

}
}



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表