找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2414|回复: 2
收起左侧

51单片机温度测量报警代码

[复制链接]
ID:279549 发表于 2018-1-25 10:59 | 显示全部楼层 |阅读模式
运用18b20进行温度测量,达到设定阈值由蜂鸣器进行报警。

单片机源程序如下:
  1. #include<reg51.h>
  2. #include"temp.h"
  3. int m,t;
  4. unsigned char DisplayData[5];//数据缓存
  5. unsigned char intr;
  6. unsigned char z;
  7. unsigned char h,s;
  8. unsigned char g,n,a,b,c;
  9. unsigned char p,q,d,e,f;
  10. bit temper_flag = 0;//温度读取标志
  11. bit Flag_EN=1;//阈值标志位
  12. bit Flag_ENM=1;//模式标志位
  13. sbit dian=P0^7;//设置小数点
  14. sbit fmq=P0^6;//蜂鸣器位
  15. code unsigned char tab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
  16.                             0xff};//0-9段码显示
  17. unsigned char duan[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
  18.                             0xff,0xbf,0xc7,0x89};//0-9,-、L、H显示段码
  19.                                                        
  20. //声明全局函数
  21. void display(int);
  22. void Display();
  23. unsigned char key_band();
  24. void Delayms();
  25. void LDisplay();
  26. void HDisplay();
  27. void LSet();
  28. void HSet();
  29. //1ms延时函数
  30. void Delay1ms(unsigned int y)
  31. {
  32.         unsigned int x;
  33.         for( ; y>0; y--)
  34.         {
  35.                 for(x=110; x>0; x--);
  36.         }
  37. }

  38. void main(void)
  39. {
  40.         TMOD |= 0x01;  //配置定时器工作模式
  41.     TH0 = (65536-50000)/256;
  42.     TL0 = (65536-50000)%256;  
  43.     EA = 1;
  44.     ET0 = 1;  //打开定时器中断
  45.     TR0 = 1;  //启动定时器
  46.         P0=0x00;
  47.         P2=0xa0;
  48.         P2=0x00;//关闭蜂鸣器
  49.         h=0;s=0;z=0;a=11;b=11;c=11;d=11;e=11;f=11;
  50.         m=rd_temperature();
  51.     while(1)
  52.     {
  53.         if(key_band()==15)
  54.                 {
  55.                         Delay1ms(30);
  56.                       if(key_band()==15)
  57.                           {
  58.                                   if(Flag_ENM)
  59.                                   {
  60.                                           z++;
  61.                                            Flag_ENM=0;
  62.                                   }
  63.                                   if(z==3)
  64.                                   {z=0;}
  65.                           }
  66.                   }
  67.                 if(key_band()!=15)
  68.                         Flag_ENM=1;
  69.             if(z==0)
  70.                 {
  71.                         LSet();
  72.                 }
  73.                 if(z==1)
  74.                 {
  75.                         HSet();
  76.                 }
  77.                 if(z==2)
  78.                 {
  79.                    if(temper_flag==1)
  80.                   {
  81.                           temper_flag = 0;
  82.                           m=rd_temperature();
  83.                   }
  84.                   display(m);
  85.                   if(t<=(a*1000+b*100+c*10)||t>=(d*1000+e*100+f*10))
  86.                 {
  87.                         fmq=1;
  88.                         P2=0xa0;
  89.                         P2=0x00;
  90.                 }
  91.          else
  92.                  {
  93.            fmq=0;
  94.                     P2=0xa0;
  95.                         P2=0x00;
  96.                  }                         
  97.             }
  98.                
  99.         }
  100. }
  101.                        
  102.       

  103. //定时器中断服务函数
  104. void isr_timer_0(void)  interrupt 1  //默认中断优先级 1
  105. {
  106.     TH0 = (65536-50000)/256;
  107.     TL0 = (65536-50000)%256;  //定时器重载  
  108.         if(++intr == 200)  //10ms执行一次
  109.         {
  110.         intr = 0;
  111.                 temper_flag = 1;  //10s温度读取标志位置1
  112.     }
  113. }
  114. //赋值缓存函数
  115. void display(int temp)
  116. {
  117.         float tp;
  118.         DisplayData[0] = 0x00;
  119.                 tp=temp;
  120.                 temp=tp*0.0625*100+0.5;       
  121.         t=temp;
  122.         DisplayData[1] = tab[temp % 10000 / 1000];
  123.         DisplayData[2] = tab[temp % 1000 / 100] | 0x80;
  124.         DisplayData[3] = tab[temp % 100 / 10];
  125.         DisplayData[4] = tab[temp % 10];
  126.         Display();                //扫描显示
  127. }
  128. //温度显示函数
  129. void Display()
  130. {
  131.         P0=0x10;
  132.         P2=0xc0;
  133.         P2=0x00;
  134.         P0=DisplayData[1];
  135.         P2=0xe0;
  136.         P2=0x00;
  137.         Delay1ms(1);
  138.        
  139.         P0=0x20;
  140.         P2=0xc0;
  141.         P2=0x00;
  142.         P0=DisplayData[2];
  143.         dian=0;
  144.         P2=0xe0;
  145.         P2=0x00;
  146.         Delay1ms(1);
  147.        
  148.         P0=0x40;
  149.         P2=0xc0;
  150.         P2=0x00;
  151.         P0=DisplayData[3];
  152.         P2=0xe0;
  153.         P2=0x00;
  154.         Delay1ms(1);
  155.        
  156.         P0=0x80;
  157.         P2=0xc0;
  158.         P2=0x00;
  159.         P0=DisplayData[4];
  160.         P2=0xe0;
  161.         P2=0x00;
  162.         Delay1ms(1);
  163. }
  164. //按键扫描函数
  165. unsigned char key_band()
  166. {
  167.         P3=0xfe;
  168.         if(P3!=0xfe)
  169.         {
  170.                 if(P3==0xee) return 3;
  171.                 if(P3==0xde) return 2;       
  172.                 if(P3==0xbe) return 1;
  173.                 if(P3==0x7e) return 0;
  174.         }
  175.         P3=0xfd;
  176.         if(P3!=0xfd)
  177.         {
  178.                 if(P3==0xed) return 7;
  179.                 if(P3==0xdd) return 6;       
  180.                 if(P3==0xbd) return 5;
  181.                 if(P3==0x7d) return 4;
  182.         }
  183.         P3=0xfb;
  184.         if(P3!=0xfb)
  185.         {
  186.                 if(P3==0xeb) return 11;
  187.                 if(P3==0xdb) return 10;       
  188.                 if(P3==0xbb) return 9;
  189.                 if(P3==0x7b) return 8;
  190.         }
  191.         P3=0xf7;
  192.         if(P3!=0xf7)
  193.         {
  194.                 if(P3==0xe7) return 15;
  195.                 if(P3==0xd7) return 14;       
  196.                 if(P3==0xb7) return 13;
  197.                 if(P3==0x77) return 12;
  198.         }
  199.           return 16;
  200. }
  201. //低阈值显示
  202. void LDisplay()
  203. {
  204.         P0=0x10;
  205.         P2=0xc0;
  206.         P2=0x00;
  207.         P0=duan[12];
  208.         P2=0xe0;
  209.         P2=0x00;
  210.         Delay1ms(1);
  211.        
  212.         P0=0x20;
  213.         P2=0xc0;
  214.         P2=0x00;
  215.         P0=duan[a];
  216.         P2=0xe0;
  217.         P2=0x00;
  218.         Delay1ms(1);
  219.        
  220.         P0=0x40;
  221.         P2=0xc0;
  222.         P2=0x00;
  223.         P0=duan[b];
  224.         dian=0;
  225.         P2=0xe0;
  226.         P2=0x00;
  227.         Delay1ms(1);
  228.        
  229.         P0=0x80;
  230.         P2=0xc0;
  231.         P2=0x00;
  232.         P0=duan[c];
  233.         P2=0xe0;
  234.         P2=0x00;
  235.         Delay1ms(1);
  236. }
  237. //高阈值显示
  238. void HDisplay()
  239. {
  240.         P0=0x10;
  241.         P2=0xc0;
  242.         P2=0x00;
  243.         P0=duan[13];
  244.         P2=0xe0;
  245.         P2=0x00;
  246.         Delay1ms(1);
  247.        
  248.         P0=0x20;
  249.         P2=0xc0;
  250.         P2=0x00;
  251.         P0=duan[d];
  252.         P2=0xe0;
  253.         P2=0x00;
  254.         Delay1ms(1);
  255.        
  256.         P0=0x40;
  257.         P2=0xc0;
  258.         P2=0x00;
  259.         P0=duan[e];
  260.         dian=0;
  261.         P2=0xe0;
  262.         P2=0x00;
  263.         Delay1ms(1);

  264.         P0=0x80;
  265.         P2=0xc0;
  266.         P2=0x00;
  267.         P0=duan[f];
  268.         P2=0xe0;
  269.         P2=0x00;
  270.         Delay1ms(1);
  271. }
  272. //低阈值设置
  273. void LSet()
  274. {

  275.         LDisplay();
  276.         g=key_band();
  277.         if(g!=15)
  278.         {
  279.                 if(g!=16)
  280.                 {
  281.                         Delay1ms(30);
  282.                         if(g!=16)
  283.                         {       
  284.                                 n=g;
  285.                                 if(Flag_EN)
  286.                                 {
  287.                                         Flag_EN=0;
  288.                                         h++;
  289.                                 }
  290.                         if(h==4)
  291.                         {h=0;}
  292.                         }       
  293.             }
  294.                 if(g==16)
  295.                 Flag_EN=1;
  296.                
  297.         }
  298.         if(h==0);
  299.         if(h==1)
  300.         {
  301.                 a=n;
  302.         }
  303.         if(h==2)
  304.         {
  305.                 b=n;
  306.         }
  307.         if(h==3)
  308.         {
  309.                 c=n;
  310.         }
  311. }
  312. ……………………

  313. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
温度显示报警程序.zip (50.84 KB, 下载次数: 20)


回复

使用道具 举报

ID:352673 发表于 2018-6-20 21:08 | 显示全部楼层
你好,没有材料清单吗,原理图
回复

使用道具 举报

ID:571410 发表于 2019-6-25 09:50 | 显示全部楼层
为什么有5处警告
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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