⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wsdxs.txt

📁 51单片控制温度和湿度的显示
💻 TXT
字号:

#include <reg52.h>
#include"intrins.h"     //_nop_();延时函数用
#define uchar unsigned char
#define uint unsigned int

sbit DQ=P3^3;           //温度控制口
sbit SD=P3^4;           //湿度控制口

sbit RS=P3^5;
sbit E=P3^7;
sbit RW=P3^6;

uchar add;

uchar data dis[17]=
{'T',':',0x00,0x00,0x00,0x00,'.',0x00,0xeb,'C',' ','S',':',0x00,0x00,'%'};    //存计算结果
uchar code ditab[16]=
{0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};
//小数转换表
//*****************************
//延时11us函数
void delay(uint t)
{
 for(;t>0;t--);
}
//****************************
//复位程序
ow_reset(void)
{
 bit presence=1;
 while(presence)
  {
    while(presence)
      {
        DQ=1;
        _nop_();
        _nop_();
        DQ=0;
        delay(50);     //550US
        DQ=1;
        delay(6);      //66US
        presence=DQ;   //presence=0继续下一步
      }
    delay(45);         //500US
    presence=~DQ;
  }
 DQ=1;
}
//****************************
//写命令函数,写一个字节
void write_byte(uchar val)
{
 uchar i;
 for(i=8;i>0;i--)
  {
    DQ=1; 
    _nop_();
    _nop_();
    DQ=0;
    _nop_(); _nop_(); _nop_(); _nop_(); _nop_();//5us
    DQ=val&0x01;             //最低位移出
    delay(6);                //66us
    val=val>>1;              //右移1位
  }
 DQ=1;
 delay(1);
}
//*****************************
//读一字节函数
uchar read_byte(void)
{
 uchar i;
 uchar value=0;
 for(i=8;i>0;i--)
  {
    DQ=1;
    _nop_();
    _nop_();
    value>>=1;
    DQ=0;
    _nop_(); _nop_(); _nop_(); _nop_();   //4us
    DQ=1;
    _nop_(); _nop_(); _nop_(); _nop_();   //4us
    if(DQ)
       {
         value|=0x80;
       }
    delay(6);          //66us
  }
 DQ=1;
 return(value);
}
//*************************************
//温度数据处理函数
void chuli(uchar gaowei,uchar diwei)
{
 uchar zong;
 if(gaowei>127)            //要是负值调整
   {
     gaowei=256-gaowei;
     diwei=256-diwei;
     dis[2]=0xb0;         //显示负号
   }
 else
   {
     dis[2]=0x20;
   }
 
 dis[7]=diwei&0x0f;       //转换成液晶码
 dis[7]=ditab[dis[7]]+0x30;
 zong=((diwei&0xf0)>>4)|((gaowei&0x0f)<<4);
 dis[3]=zong/100+0x30;
 dis[5]=zong%100;
 dis[4]=dis[5]/10+0x30;
 dis[5]=dis[5]%10+0x30;
 
}
//*************************************
//湿度数据
int ceshidu()          //测湿度子函数
{
        int i,R6,R7,j,k,h,g,fhz;
        float shiduzhi;
yy:     TMOD=0X15; //设置t0计数,t1计时 ,湿度测量程序
	TR0=1;   //开t1
        TR1=1;   //开t0
        for(i=100;i>0;i--)  //计时1秒
 {		TH1=0XD8;
                TL1=0XF0;
                do{}while(!TF1);//判断是否溢出
  		TF1=0;
  }
                TR1=0;
                TR0=0;
  		R6=TH0<<8;//高字节送给r6,低字节送给r7
  		R7=TL0;
                fhz=R6+R7;
                j=fhz/1000;
                fhz=fhz%1000;
                k=fhz/100;
                fhz=fhz%100;
                h=fhz/10;
                g=fhz%10;
                fhz=j*1000+k*100+h*10+g;
                if(7351<fhz)        //得到湿度值
                {goto yy;}
                else if(6853<fhz&fhz<=7351)
                {shiduzhi=(fhz-6853)/12.7;}
                else if(6728<fhz&fhz<=6853)
                {shiduzhi=10+(fhz-6728)/12.4;}
                else if(6600<fhz&fhz<=6728)
                {shiduzhi=20+(fhz-6600)/12.4;}
                else if(6468<fhz&fhz<=6600)
                {shiduzhi=30+(fhz-6468)/12.3;}
                else if(6600<fhz&fhz<=6728)
                {shiduzhi=40+(fhz-6600)/12.8;}
                else if(6468<fhz&fhz<=6600)
                {shiduzhi=50+(fhz-6468)/13.2;}
                else if(6330<fhz&fhz<=6468)
                {shiduzhi=60+(fhz-6330)/13.8;}
                else if(6186<fhz&fhz<=6330)
                {shiduzhi=70+(fhz-6186)/14.4;}
                else if(6033<fhz&fhz<=6186)
                {shiduzhi=80+(fhz-6033)/15.3;}
                else if(6033<fhz&fhz<=6186)
                {shiduzhi=80+(fhz-6033)/15.3;}
                else if(5900<fhz&fhz<=6033)
                {shiduzhi=80+(fhz-5900)/15.3;}
                else
                {goto yy;}
                return(shiduzhi);
}
//******************************
//湿度数据处理函数
void chulishidu(uchar d)
{
 dis[13]=d/10+0x30;       //转换成液晶码
 dis[14]=d%10+0x30;
}
//***************************************
//显示子函数
//####################################################################
/*************延时*************/

   delay1(uint z)
 {
   uint i,j;
   for(i=0;i<z;i++)
     for(j=0;j<120;j++)
    ;
 }

//*****************************
//写指令子程序

   wrcod(uchar i)
 { 
   P1=i;
   RS=0;
   E=0;
   delay1(7);
   E=1;
 }
 //*****************************
//写数据子程序
   wrshu(uchar i)
  {
   P1=i;
   RS=1;
   E=0;
   delay1(2);
   E=1;
  }
//*****************************
//初始化液晶
   chushi()
  {
   uchar coad;
   coad=0x01;           //清屏
   wrcod(coad);
   coad=0x06;
   wrcod(coad);         //输入方式控制
   coad=0x0e;
   wrcod(coad);         //显示开关控制
   coad=0x38;
   wrcod(coad);         //功能设定:设置16x2显示,5x7显示,8位数据接口    
} 
//*******************************
//显示程序
   xian1()
  {
    uchar i=0; 
    while(i<17)
   {
    wrshu(dis[i]);
    i++;    
   }
  }
//########################################################################
//*************************************
//主程序
main()
{
 
 uchar wenh,wenl,r4;      //存高低温度值
 RW=0;
 chushi();
 while(1)
  { 
    ow_reset();           //转换
    write_byte(0xcc);
    write_byte(0x44); 
    ow_reset();           
    write_byte(0xcc);     //发读命令
    write_byte(0xbe); 
    wenl=read_byte();     //温度低八位
    wenh=read_byte();     //温度高八位
    chuli(wenh,wenl);     //数据处理
    r4=(uchar)ceshidu(); 
    chulishidu(r4);
    add=0x80;             //显示
    wrcod(add);   
    xian1();
    delay1(200);
    
  }
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -