找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2471|回復(fù): 3
收起左側(cè)

我設(shè)計(jì)一個(gè)溫室檢測(cè),但是單片機(jī)程序無法讓蜂鳴器報(bào)警和控制繼電器,求大佬指點(diǎn)

[復(fù)制鏈接]
ID:506700 發(fā)表于 2019-4-21 12:52 | 顯示全部樓層 |閱讀模式
10黑幣
TH 和RH是300ms檢測(cè)一次得到的數(shù)據(jù),Data1和2是自己通過按鍵可以調(diào)整的閥值,這兩個(gè)數(shù)據(jù)都能在lcd1602上正確顯示,但是就是那個(gè)報(bào)警函數(shù)warn(),燒寫后跟不不按照預(yù)定想法來實(shí)行。怎么樣都不會(huì)報(bào)警,我把控制蜂鳴器的管腳連接led也沒用。不知道是不是我程序?qū)懙挠袉栴},希望大佬們幫我看一下。

單片機(jī)源程序如下:
  1. #include"reg52.h"
  2. #include"dht11.h"
  3. #include "lcd1602.h"
  4. #include"keypros.h"
  5. #include"i2c.h"

  6. sbit beep=P1^4;
  7. sbit switch1=P1^5;
  8. sbit switch2=P1^6;
  9. extern uint RH,TH;
  10. extern uint rec_dat1[2]; //用于顯示的接收數(shù)據(jù)數(shù)組
  11. extern uint rec_dat2[2];
  12. extern uint Data1,Data2;
  13. extern uchar menu,flat;
  14. uchar DHT11;
  15. uchar i;
  16. uchar menu1_1[]={"Temp:   C   T:"},menu1_2[]={"Humi:  %RH  H:"},menu2[]={"Temp"},menu3[]={"Humi"};
  17. void time_init()   
  18. {
  19. EA   = 1;     //開總中斷
  20. TMOD = 0X01;   //定時(shí)器0、定時(shí)器1工作方式1
  21. ET0  = 1;    //開定時(shí)器0中斷
  22. TR0  = 1;    //允許定時(shí)器0定時(shí)
  23. }

  24. void warn()  //報(bào)警函數(shù)
  25. {

  26.   if(Data1<=TH)  //檢測(cè)的溫度高于設(shè)定溫度報(bào)警值
  27.                               
  28.      {
  29.       beep=1;    //報(bào)警
  30.    switch1=1;
  31.      }      
  32.    
  33. if(Data2<=RH)  //檢測(cè)的濕度高于設(shè)定的濕度值
  34.                               
  35.      {
  36.       beep=1;
  37.       switch2=1;    //報(bào)警
  38.      }      
  39.    
  40. if( Data1<=TH && Data2<=RH )  
  41.     {
  42.     beep=0; //停止報(bào)警
  43.    switch1=0;
  44.    switch2=0;
  45.   }
  46.    
  47.    
  48.   
  49. }




  50. void main()
  51. {   
  52.    
  53.     Data1=At24c02Read(1);
  54.    delay(10);
  55.    Data2=At24c02Read(2);
  56.   time_init();   
  57.    while(1)
  58.   {
  59.    Key();     //通過按鍵1來選擇模式 模式1顯示數(shù)據(jù) 模式二 調(diào)整溫度閥值 模式三調(diào)整濕度閥值
  60.    if(menu==0)
  61.    {
  62.     if(flat==1)
  63.     {
  64.      flat=0;
  65.      LcdWriteCom(0x38);  //開顯示
  66.          LcdWriteCom(0x0c);  //開顯示不顯示光標(biāo)
  67.          LcdWriteCom(0x06);  //寫一個(gè)指針加1
  68.          LcdWriteCom(0x01);
  69.      
  70.      LcdWriteCom(0x80);
  71.      for(i=0;i<14;i++)
  72.      LcdWriteData(menu1_1[i]);
  73.      LcdWriteCom(0x87);
  74.      LcdWriteData(0xdf);
  75.    
  76.      LcdWriteCom(0x40+0x80);
  77.       for(i=0;i<14;i++)
  78.      LcdWriteData(menu1_2[i]);
  79.          
  80.      LcdWriteCom(0x0e+0x80);
  81.      LcdWriteData((Data1/10)+0x30);
  82.      
  83.      LcdWriteCom(0x0f+0x80);
  84.      LcdWriteData((Data1%10)+0x30);
  85.      
  86.      LcdWriteCom(0x4e+0x80);
  87.      LcdWriteData((Data2/10)+0x30);
  88.      
  89.      LcdWriteCom(0x4f+0x80);
  90.      LcdWriteData((Data2%10)+0x30);
  91.          
  92.     }
  93.     if(DHT11==1)
  94.     {
  95.      DHT11=0;
  96.      DHT11_receive();
  97.      warn();
  98.      LcdWriteCom(0x85);
  99.      LcdWriteData(rec_dat2[0]);
  100.      LcdWriteCom(0x86);
  101.      LcdWriteData(rec_dat2[1]);
  102.      LcdWriteCom(0x45+0x80);
  103.      LcdWriteData(rec_dat1[0]);
  104.      LcdWriteCom(0x46+0x80);
  105.      LcdWriteData(rec_dat1[1]);
  106.         
  107.      
  108.     }
  109.    }
  110.    
  111.    
  112.    
  113.    if(menu==1)
  114.    {
  115.     if(flat==1)
  116.     {
  117.      flat=0;
  118.      LcdInit1();  //初始化 函數(shù)
  119.      
  120.      LcdWriteCom(0x86);
  121.       for(i=0;i<4;i++)
  122.      LcdWriteData(menu2[i]);
  123.     }
  124.     shezhi();                              //此函數(shù)是通過按鍵增加溫度閥值
  125.     LcdWriteCom(0x47+0x80);
  126.     LcdWriteData((Data1/10)+0x30);
  127.      
  128.     LcdWriteCom(0x48+0x80);
  129.     LcdWriteData((Data1%10)+0x30);
  130.    }
  131.    
  132.    
  133.    if(menu==2)
  134.    {
  135.     if(flat==1)
  136.     {
  137.      flat=0;
  138.      LcdInit1();
  139.      
  140.      LcdWriteCom(0x86);
  141.       for(i=0;i<4;i++)
  142.      LcdWriteData(menu3[i]);
  143.   
  144.     }
  145.     shezhi();   ////此函數(shù)是通過按鍵增加濕度閥值
  146.     LcdWriteCom(0x47+0x80);
  147.     LcdWriteData((Data2/10)+0x30);
  148.      
  149.     LcdWriteCom(0x48+0x80);
  150.     LcdWriteData((Data2%10)+0x30);
  151.    }   
  152.   }   
  153. }
  154. void time0_int() interrupt 1
  155. {
  156. static uchar i;
  157. TH0 = 0x3c;
  158. TL0 = 0xb0;  // 50ms
  159. i++;  
  160. if(i==6)
  161. {
  162.   i = 0;
  163.   DHT11=1;    //300ms
  164. }
  165. }
復(fù)制代碼

回復(fù)

使用道具 舉報(bào)

ID:507641 發(fā)表于 2019-4-23 20:23 | 顯示全部樓層
蜂鳴器選型選對(duì)了沒?
回復(fù)

使用道具 舉報(bào)

ID:280876 發(fā)表于 2019-4-24 20:01 | 顯示全部樓層
void warn()  //報(bào)警函數(shù)
{

  if(Data1<=TH)  //檢測(cè)的溫度高于設(shè)定溫度報(bào)警值
                              
     {
      beep=1;    //報(bào)警
   switch1=1;
     }      
   
if(Data2<=RH)  //檢測(cè)的濕度高于設(shè)定的濕度值
                              
     {
      beep=1;
      switch2=1;    //報(bào)警
     }      
   
if( Data1<=TH && Data2<=RH )  
    {
    beep=0; //停止報(bào)警
   switch1=0;
   switch2=0;
  }
   
   
  
}
你判斷邏輯都有問題
回復(fù)

使用道具 舉報(bào)

ID:520972 發(fā)表于 2019-4-24 23:25 | 顯示全部樓層
前面兩個(gè)IF語句應(yīng)該是大于等于,你的邏輯有點(diǎn)小問題
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表