找回密码
 立即注册

QQ登录

只需一步,快速开始

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

新手求助,at89c52温度计的报警功能无法实现

[复制链接]
跳转到指定楼层
楼主
单片机c语言程序有点问题,但不知道怎么改
proteous仿真应该是没问题的,按键从上到下依次是
加减控制开关
摄氏华氏转换
显示温度报警and返回
高温报警温度调节
低温报警温度调节
附图
拜托各位大神告知下哪里的问题,谢谢


电路原理图如下:

  1. //******************************************************
  2. #include<reg51.h>
  3. #include<intrins.h>       //shiyongyanshi
  4. #include<math.h>                                                                 
  5. #define uchar unsigned char
  6. #define uint unsigned int
  7. sbit DQ=P3^3;
  8. sbit RS =P2^0;
  9. sbit RW =P2^1;
  10. sbit EN =P2^2;
  11. sbit k=P1^0;
  12. sbit k1=P1^4;
  13. sbit k2=P1^5;
  14. sbit k3=P1^6;
  15. sbit k4=P1^7;
  16. sbit led_red=P2^5;
  17. sbit led_blue=P2^6;
  18. sbit BEEP=P3^7;
  19. uchar bz=1;
  20. //BEEP=0;
  21. uchar ng=0; //fuhaobiaoshiwei  
  22. uchar TempBuffer[] ={"TEMP:           "};
  23. int temp_value;      //温度值
  24. uchar code dis_title[]={"--current temp--"};
  25. void xianshi_huashi();
  26. uchar gw=40;
  27. char dw= 10;
  28. uchar xianshi_title[]={"  TEMP ALARM    "};
  29. uchar xianshi_baojing[]={"HI:    LO:   "};         //345  10 `` 12
  30. //----------------延时--------------------------------
  31. voiddelayxus(uint x)
  32. {
  33. uchar i;
  34. while(x--)  for(i=0;i<200;i++);
  35. }
  36. //*********************LCD  控制********************
  37. //读lcd 状态
  38. uchar read_lcd_state()
  39. {
  40. uchar state ;
  41. RS=0;RW=1;EN=1;delayxus(1);state=P0;EN=0;delayxus(1);
  42. return state;
  43. }                                                                                                            
  44. //  忙等待
  45. void lcd_busy_wait()
  46. {
  47.       while((read_lcd_state()&0x80)==0x80);
  48.        delayxus(5);
  49. }
  50. //向LCD写数据
  51. voidwrite_lcd_data(uchar dat)
  52. {
  53.   lcd_busy_wait();
  54.   RS=1;RW=0;EN=0;P0=dat;EN=1;delayxus(1);EN=0;
  55. }
  56. //向LCD写指令
  57. voidwrite_lcd_cmd(uchar cmd)
  58. {lcd_busy_wait();
  59.   RS=0;RW=0;EN=0;P0=cmd;EN=1;delayxus(1);EN=0;
  60. }
  61. //-----------------------------------------------------------------------------------------------------
  62. //LCD初始化
  63. void init_lcd()
  64. {
  65.   write_lcd_cmd(0x38);       delayxus(1);
  66.   write_lcd_cmd(0x01) ; delayxus(1);
  67.   write_lcd_cmd(0x06);       delayxus(1);
  68.   write_lcd_cmd(0x0C) ; delayxus(1);
  69. }
  70. //-----------------------------------------------
  71. //设置液晶显示位置
  72. voidset_lcd_pos(uchar p)
  73. {
  74.   write_lcd_cmd(p| 0x80);
  75. }
  76. //---------------------------------------
  77.   //在LCD上显示字符串
  78. void dis_lcd_string(uchar p,uchar *s)     //位置,字符指针
  79.   {
  80.   uchar i;
  81.    set_lcd_pos(p);
  82.        for(i=0;i<16;i++)      //16*2
  83.        {
  84.          write_lcd_data(s[ i]) ;
  85.          delayxus(1);
  86.        }
  87.   }
  88. //-------------------------------------------------------------------
  89. voiddelay_18B20(unsigned int i)
  90. {
  91.        while(i--);
  92. }
  93. //--------------------蜂鸣器---------------------------
  94. voidbeep()
  95.    {
  96.       uchari;
  97.          for(i=0;i<100;i++)
  98.          {
  99.             delayxus(1);
  100.               BEEP=~BEEP;
  101.          }
  102.      BEEP=1;
  103.    }
  104. //延时2
  105. void delay2(uint x)
  106. {
  107. while(--x);
  108. }
  109. //==================================================
  110. //初始化DS!*
  111. /**********ds18b20初始化函数**********************/
  112. void Init_DS18B20(void)
  113. {
  114.         unsigned char x=0;
  115.         DQ = 1;          //DQ复位
  116.         delay_18B20(8);  //稍做延时
  117.         DQ = 0;          //单片机将DQ拉低
  118.         delay_18B20(80); //精确延时 大于 480us
  119.         DQ = 1;          //拉高总线
  120.         delay_18B20(14);
  121.         x=DQ;            //稍做延时后如果x=0则初始化成功 x=1则初始化失败
  122.         delay_18B20(20);
  123. }
  124. /***********ds18b20读一个字节**************/  
  125. ucharReadOneChar(void)
  126. {
  127.        uchari=0;
  128.        uchardat = 0;
  129.        for(i=8;i>0;i--)
  130.         {
  131.                 DQ = 0; // 给脉冲信号
  132.                 dat>>=1;
  133.                 DQ = 1; // 给脉冲信号
  134.                 if(DQ)
  135.                 dat|=0x80;
  136.                 delay_18B20(4);
  137.         }
  138.       return(dat);
  139. }
  140. /*************ds18b20写一个字节****************/  
  141. void WriteOneChar(uchar dat)
  142. {
  143.       unsigned char i=0;
  144.       for (i=8; i>0; i--)
  145.       {
  146.             DQ = 0;
  147.              DQ = dat&0x01;
  148.           delay_18B20(5);
  149.              DQ = 1;
  150.           dat>>=1;
  151. }
  152. }
  153. /**************读取ds18b20当前温度************/
  154. void ReadTemp(void)
  155. {
  156.        unsignedchar a=0;
  157.        unsignedchar b=0;
  158.        unsignedchar t=0;
  159.        Init_DS18B20();
  160.        WriteOneChar(0xCC);        //跳过读序号列号的操作
  161.        WriteOneChar(0x44);    // 启动温度转换
  162.        delay_18B20(100);       // this message is wery important
  163.        Init_DS18B20();
  164.        WriteOneChar(0xCC);   //跳过读序号列号的操作
  165.        WriteOneChar(0xBE);   //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
  166.        delay_18B20(100);
  167.        a=ReadOneChar();       //读取温度值低位 aaaaaaaaaaaaaaaaaaaa
  168.        b=ReadOneChar();               //读取温度值高位  bbbbbbbbbb  b.a
  169. temp_value= b<<4;
  170. temp_value+=(a&0xf0)>>4;
  171. }
  172. void temp_to_str()   //温度数据转换成液晶字符显示
  173. {     
  174.   if((temp_value   &  0xE0)==0xE0)
  175.        {
  176.         temp_value =~temp_value +1 ;
  177.         ng=1;
  178.        }
  179.        else
  180.        ng=0;
  181.         
  182.         if(k1==0) {//while(k1==0);
  183.          bz=(bz+1) %2;       }
  184.           if(bz==0)
  185.          {   
  186.      temp_value =(int)(temp_value )*(9.0/5.0)+32;
  187.          }
  188.    
  189.        if(ng==1)
  190.        {
  191.                TempBuffer[9]=temp_value/100+'0';
  192.                  if(TempBuffer[9]= ' ')
  193.                TempBuffer[9]='-';
  194.        TempBuffer[10]=temp_value%100/10+'0';  //十位
  195.                
  196.        TempBuffer[11]=temp_value%10+'0';  //个位
  197.          TempBuffer[12]=0xdf;              //温度符号
  198.          TempBuffer[13]='C';
  199.        TempBuffer[14]='\0';      
  200.       //TempBuffer[1]=ng %10+ '\0';
  201.        }
  202. if(ng==0)
  203.        {  
  204.         TempBuffer[9]=temp_value/100+'0';
  205.          if(temp_value/100==0)  TempBuffer[9] =' ';
  206.     TempBuffer[10]=temp_value % 100/10+'0'; //十位
  207.           if(temp_value % 100/10==0) TempBuffer[10]=' ';
  208.          TempBuffer[11]=temp_value%10+'0'; //个位
  209.         // if(temp_value%10==0) TempBuffer[11] =' ';
  210.   TempBuffer[13]='C';//温度符号
  211. if(bz==0)          TempBuffer[13]='F';   
  212. TempBuffer[14]='\0';   
  213.   //TempBuffer[1]=ng%10+'\0';
  214.    }
  215. }
  216. void Delay1ms(unsigned int count)
  217. {
  218.        unsignedint i,j;
  219.        for(i=0;i<count;i++)
  220.        for(j=0;j<120;j++);
  221. }
  222. /*延时子程序*/
  223. void mdelay(uint delay)
  224. {     uinti;
  225.       for(;delay>0;delay--)
  226.            {
  227. for(i=0;i<62;i++) ;     //1ms延时.
  228.            }
  229. }
  230. void show_time()   //液晶显示程序
  231. {
  232. ReadTemp();
  233.                   //开启温度采集程序
  234. temp_to_str();                 //温度数据转换成液晶字符
  235. dis_lcd_string(0x40,TempBuffer);            //显示温度
  236. dis_lcd_string(0x00,dis_title); //
  237.    
  238. Delay1ms(400);                 //扫描延时
  239. }
  240. void xianshi_dw()
  241. {
  242. if(dw>=0)
  243.   {xianshi_baojing[3]=gw/100+'0';
  244.   xianshi_baojing[4]=gw%100/10+'0';
  245.   xianshi_baojing[5]=gw%10+'0';
  246.    xianshi_baojing[10]=dw/100+'0';
  247.   xianshi_baojing[11]=dw%100/10+'0';
  248.   xianshi_baojing[12]=dw%10+'0';
  249.        dis_lcd_string(0x00,xianshi_title);
  250.        dis_lcd_string(0x40,xianshi_baojing);
  251.        }
  252.        else
  253.        if(dw<0)
  254.        {
  255.          xianshi_baojing[3]=gw/100+'0';
  256.   xianshi_baojing[4]=gw%100/10+'0';
  257.   xianshi_baojing[5]=gw%10+'0';
  258.    xianshi_baojing[10]= '-';
  259.   xianshi_baojing[11]=abs(dw)%100/10+'0';
  260.   xianshi_baojing[12]=abs(dw)%10+'0';
  261.        dis_lcd_string(0x00,xianshi_title);
  262.        dis_lcd_string(0x40,xianshi_baojing);
  263.        }
  264. }
  265. void main()
  266. {
  267. led_red=0;
  268. led_blue=0;
  269. BEEP=0;
  270. delayxus(10);
  271. init_lcd();
  272. //init_DS();
  273. Init_DS18B20();
  274. delay2(500);
  275. delay2(500);
  276. while(1)
  277. {
  278.       if( k2==1 &&k3==1 && k4==1)
  279.               show_time();
  280.            }
  281.        if(k2==0)
  282.        {
  283.        //  while(k2==0);
  284.        xianshi_dw();
  285.    
  286.        }
  287.         
  288.      if(k3==0 && k2==0 &&k==1)
  289.      {     while(k3==0);
  290.             gw++;
  291.     }           
  292.        if(k4==0 && k2==0 &&k==1)
  293.        {
  294.        while(k4==0);
  295.      dw++;
  296.      }
  297.                
  298.          if(k3==0 && k2==0 &&k==0)
  299.          {
  300. while(k3==0);
  301.               gw--;
  302. }   
  303.      if(k4==0 && k2==0 &&k==0)
  304.          {
  305.          while(k4==0);
  306.        dw--;   
  307.         
  308.         if(dw<0) dw=0;
  309.             /*    if(dw<0)
  310.                      {
  311.                        xianshi_baojing[10]='-';
  312.                }         */
  313.        }  
  314.   
  315.        delayxus(100);
  316.    //报警设置
  317.   if(bz==1)
  318.   { if(temp_value>gw  &&bz==1)
  319.         { beep(); delayxus(50);led_red=1;}
  320.         else
  321.         led_red=0;
  322.               
  323.       if( temp_value<dw  &&bz==1)
  324.        { beep(); delayxus(50);led_blue=1;}
  325.               else
  326.               led_blue=0;
  327.   if( (TempBuffer[9]=='-')&& abs( temp_value)>=abs(dw)&& bz==1 )
  328.        {beep(); delayxus(50);}
  329. if(  dw<=0 && abs(temp_value)>=abs(dw) &&bz==1 )
  330.        {beep(); delayxus(50);}              
  331. if((-abs(temp_value))<=(-abs(dw)) && dw<=0&&bz==1)
  332.   
  333.               { beep(); delayxus(50);}
  334.        }
  335. }
  336. }                           
  337. //*******************************************************
复制代码


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

使用道具 举报

沙发
ID:304879 发表于 2018-6-19 07:54 | 只看该作者
本帖最后由 paytonshi 于 2018-7-7 22:12 编辑

31行处voiddelayxus 没有空格 呵呵
回复

使用道具 举报

板凳
ID:96682 发表于 2018-6-19 09:55 | 只看该作者
这仿真图就有问题得改改才行
回复

使用道具 举报

地板
ID:349686 发表于 2018-6-19 10:42 | 只看该作者
paytonshi 发表于 2018-6-19 07:54
31行处voiddelayxus(uint x) 没有空格 呵呵

哦,我试着改改,谢谢
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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