找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2234|回复: 0
收起左侧

51单片机显示CO与温湿度

[复制链接]
ID:192833 发表于 2017-5-5 12:20 | 显示全部楼层 |阅读模式
V_59IQ)76VA)GCL6NG$MN.png

  1. #include<reg52.h>
  2. #include <absacc.h>
  3. #define uint unsigned int
  4. #define uchar unsigned char
  5. sbit rs=P1^2;
  6. sbit rw=P1^3;
  7. sbit en=P1^4;
  8. sbit DQ=P3^4;
  9. sbit start=P1^0;              //     18b20总线
  10. sbit eoc=P3^3;
  11. sbit oe=P1^1;
  12. sbit CLK=P3^6;
  13. unsigned char tem;
  14. unsigned char  dat[8]={0};                        //温度
  15. unsigned char  shuju1[7]={0};         //湿度
  16. unsigned char  shuju2[9]={0};
  17. uchar codetable1[16]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x54,0x3a,0x2d,0x2e,0x20,0x2f};        //温度调用
  18. uchar codetable2[15]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x53,0x3a,0x25,0x2e,0x20};            //湿度调用
  19. uchar codetable3[15]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x43,0x6f,0x3a,0x20,0x2e};
  20. /**************1602d调用函数*******************/
  21. void write_com(uchar com);
  22. void write_shuju(uchar shuju);
  23. void init();
  24. void delay(uchar z);
  25. /**************18b20调用函数**************/
  26. unsigned char reset(void);           //18b20初始化//
  27. void write(uchar dat);               //18b20写时序
  28. unsigned char read(void);            //18b20读时序                  
  29. void readtemperature(void);          //18b20读二进制16位温度
  30. void yanshi(unsigned int us);
  31. /****************hs1101调用函数*************************/
  32. void readfrequency(void);                        //        读频率函数//
  33. bit minrange=0;                                               //湿度下限标志位//
  34. bit maxrange=0;                                               //湿度上限标志位//
  35. bit T0Over=0;                                               //定时器结束标记
  36. unsigned char w,x,y;



  37. /******************主函数******************/
  38. void main()                                         
  39. {
  40.        init();
  41.        while(1)        
  42.        {
  43.                 readtemperature();
  44.                 write_com(0x80+0x40);
  45.                 write_shuju(table1[dat[0]]);
  46.                write_com(0x80+0x41);
  47.                 write_shuju(table1[dat[1]]);
  48.                 write_com(0x80+0x42);
  49.                 write_shuju(table1[dat[2]]);
  50.                 write_com(0x80+0x43);
  51.                 write_shuju(table1[dat[3]]);
  52.                 write_com(0x80+0x44);
  53.                 write_shuju(table1[dat[4]]);
  54.                 write_com(0x80+0x45);
  55.                 write_shuju(table1[dat[5]]);
  56.                 write_com(0x80+0x46);
  57.                 write_shuju(table1[dat[6]]);
  58.                 write_com(0x80+0x47);
  59.                 write_shuju(table1[dat[7]]);
  60.                 readfrequency();
  61.        }
  62. }
  63. /*****************1602延时程序*****************/
  64. void delay(uchar z)
  65. {
  66.        uint a,b;
  67.        for(a=0;a<z;a++)
  68.                 for(b=0;b<110;b++);
  69. }
  70. /*******************1602初始化**************/
  71. void init()
  72. {
  73.        oe=0;
  74.        en=0;
  75.        write_com(0x38);                //设置为8位并行,显示2行,5*7点阵显示
  76.        write_com(0x0c);                //设置显示开无光标 光标不闪烁
  77.        write_com(0x01);                 //清屏指令
  78.         write_com(0x80);                 
  79. }

  80. /***************1602写指令*********************/
  81. void write_com(uchar com)
  82. {
  83.        rs=0;
  84.        rw=0;
  85.        P0=com;
  86.        delay(5);
  87.        en=1;
  88.        en=0;
  89. }
  90. /**************1602写数据**********************/
  91. void write_shuju(uchar shuju)
  92. {
  93.        rs=1;
  94.        rw=0;
  95.        P0=shuju;
  96.        delay(5);
  97.        en=1;
  98.        en=0;
  99. }
  100. /*************18b20微秒延时********************/
  101. void yanshi(unsigned int us)
  102. {
  103.        int s;
  104.        for(s=0;s<us;s++);
  105. }
  106. /************************18b20初始化********/
  107. unsigned char reset(void)
  108. {
  109.        unsigned char  presence;
  110.        DQ=0;
  111.        yanshi(60);       //大概480微秒
  112.        DQ=1;
  113.        yanshi(8);                 //延时大概50微秒
  114.        presence=DQ;
  115.        yanshi(14);
  116.        return(presence);                }







  117. /*************18b20写时序*************************/
  118. void write(unsigned char dat)
  119. {
  120.        unsigned char i;
  121.        for(i=8;i>0;i--)
  122.        {
  123.                 DQ=0;
  124.                 DQ=dat&0x01;
  125.                 yanshi(16);
  126.                 DQ=1;
  127.                 dat>>=1;
  128.        }
  129. }

  130. /***************18b20读时序*********************/
  131. unsigned char read(void)
  132. {
  133.        unsigned char i,dat=0;
  134.        for(i=8;i>0;i--)
  135.        {
  136.                 DQ=0;
  137.                 dat>>=1;
  138.                DQ=1;
  139.                 if(DQ)
  140.                         dat|=0x80;
  141.                 yanshi(7);
  142.        }
  143.        return (dat);
  144. }

  145. /**************18b20读温度**********************/
  146. void readtemperature(void)
  147. {

  148.        unsigned char temp;
  149.        int temperature;
  150.        reset();
  151.        write(0xcc);     //跳过读序号操作
  152.        write(0x44);    //启动温度转换
  153.        reset();
  154.        write(0xcc);     //跳过读序号操作
  155.        write(0xbe);    //读取温度寄存器
  156.        temperature=read();
  157.        temp=read();
  158.        temperature=temperature|(temp<<8);
  159.        dat[0]=10;
  160.        dat[1]=11;
  161.        dat[2]=14;
  162.        if(temperature<0)
  163.        {
  164.                 dat[2]=12;
  165.                 temperature=~temperature+1;
  166.        }
  167.        dat[3]=(temperature*10/16)/100;       //十位
  168.        dat[4]=((temperature*10/16)%100)/10;  //个位
  169.        dat[5]=13;
  170.        dat[6]=((temperature*10/16)%100)%10; //小数点后一位
  171.        dat[7]=15;
  172. }
  173. /****************中断*****************************/
  174. void timer0() interrupt 1
  175. {
  176.        TR1=0;     
  177.        TR0=0;     
  178.        TF0=0;     
  179.         ET0=0;   

  180.    T0Over=1;

  181. }
  182. /*******AD控制程序*******************************/
  183.           void zhuanhuan()
  184. {
  185.                 start=0;
  186.                 start=1;
  187.                 start=0;
  188.                 while(!eoc);        
  189.                oe=1;
  190.                 tem=P0;
  191.                 oe=0;
  192.                 tem=(tem*2*1*98.039216/100);  //AD采样换算
  193. }






  194. /*************湿度传感器频率采集及转换模块***************/
  195. void readfrequency()
  196. {
  197.        unsigned int ftequency,RH;
  198.        TMOD=0x51;
  199.        TH0=0x4C;
  200.        TL0=0X00;
  201.        TH1=0;
  202.        TL1=0;
  203.        TR1=1;
  204.        TR0=1;
  205.    ET0=1;
  206.    ET1=0;
  207.        EA=1;
  208.        T0Over=0;
  209.        while(!T0Over)
  210.        write_com(0x80+0x48);
  211.        write_shuju(table2[shuju1[0]]);
  212.        write_com(0x80+0x49);
  213.        write_shuju(table2[shuju1[1]]);
  214.        write_com(0x80+0x4a);
  215.        write_shuju(table2[shuju1[2]]);
  216.        write_com(0x80+0x4b);
  217.        write_shuju(table2[shuju1[3]]);
  218.        write_com(0x80+0x4c);
  219.        write_shuju(table2[shuju1[4]]);
  220.         write_com(0x80+0x4d);
  221.        write_shuju(table2[shuju1[5]]);
  222.        write_com(0x80+0x4e);
  223.        write_shuju(table2[shuju1[6]]);
  224.        ftequency=(TH1*256+TL1)*20;
  225.        minrange=0;
  226.        maxrange=0;
  227.        if(ftequency)
  228.        {
  229.                if(ftequency<6033)
  230.                         maxrange=1;
  231.                 if(ftequency>7351)
  232.                         minrange=1;                                       
  233.                 if(6330<ftequency<=7351)
  234.                         RH=5880-0.8*ftequency;
  235.                 if(6033<=ftequency<=6330)
  236.                 RH=5860-0.8*ftequency;
  237.                 shuju1[0]=10;
  238.                 shuju1[1]=11;
  239.                 shuju1[2]=RH/100;
  240.                 shuju1[3]=(RH%100)/10;
  241.                 shuju1[4]=13;
  242.                shuju1[5]=(RH%10);
  243.                 shuju1[6]=12;
  244.                 write_com(0x80);
  245.                 write_shuju(table3[shuju2[0]]);
  246.                 write_com(0x81);
  247.                 write_shuju(table3[shuju2[1]]);
  248.                 write_com(0x82);
  249.                 write_shuju(table3[shuju2[2]]);
  250.                 write_com(0x83);
  251.                 write_shuju(table3[shuju2[3]]);
  252.                 write_com(0x84);
  253.                 write_shuju(table3[shuju2[4]]);
  254.                 write_com(0x85);
  255.                 write_shuju(table3[shuju2[5]]);
  256.                 write_com(0x86);
  257.                 write_shuju(table3[shuju2[6]]);
  258.                 write_com(0x87);
  259.                 write_shuju(table3[shuju2[7]]);
  260.                 write_com(0x88);
  261.                 write_shuju(table3[shuju2[8]]);
  262.                 shuju2[0]=10;
  263.                 shuju2[1]=11;
  264.                 shuju2[2]=12;
  265.                 shuju2[3]=13;
  266.                 shuju2[4]= tem/1000;
  267.                 shuju2[5]= tem%1000/100;
  268.                 shuju2[6]= 14;
  269.                shuju2[7]= tem%100/10;
  270.                 shuju2[8]= tem%10;
  271.                                    zhuanhuan();

  272.        }
  273. }



复制代码


回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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