本帖最后由 新手小白. 于 2020-11-25 21:35 編輯
51單片機測溫蜂鳴器報警,再加上舵機!用來測溫,在溫度滿足的情況下,舵機旋轉90度(我下面的程序只是給了延時,還沒具體弄成90度),然后大概停留一分鐘,再轉回來!跟小區的護欄差不多吧!現在程序的邏輯有問題,超過33度后不能進行二次測溫了,除非關掉電源!然后舵機也不轉動!不知道邏輯怎么修改!求大佬們幫們看看!提提意見!
下面是單片機程序:
- #include"reg52.h"
- #include"intrins.h"
- #define uint unsigned int
- #define uchar unsigned char
- #define Nack_counter 10
- uchar num;
- signed char H_alarm=38; //默認為38攝氏度報警
- signed char L_alarm=33;
- signed char tempalarm=0;
- unsigned char szl[3];
- //************** 端口定義**************
- //LCD 控制線接口
- uchar flag1;
- sbit RS=P0^7;
- sbit RW=P0^6;
- sbit LCDE=P0^5;
- sbit PWM = P0^1; //設定PWM輸出的I/O端口
- uchar v;
- sbit flay3=P3^0;
- //按鍵接口
- sbit key1=P0^0;
- sbit key2=P0^1;
- sbit key3=P0^2;
- //蜂鳴器報警接口
- sbit buzzer= P1^5;
- //mlx90614 端口定義
- sbit SCL=P1^6;//時鐘線
- sbit SDA=P1^7;//數據線
- //************ 數據定義****************
- bdata uchar flag;//可位尋址數據
- sbit bit_out=flag^7;
- sbit bit_in=flag^0;
- uchar DataH,DataL,Pecreg,t,num;
- //************ 函數聲明*****************************************
- void start_bit(); //MLX90614 發起始位子程序
- void stop_bit(); //MLX90614·發結束位子程序
- uchar rx_byte(void); //MLX90614 接收字節子程序
- void send_bit(void); //MLX90614·發送位子程序
- void tx_byte(uchar dat_byte); //MLX90614 接收字節子程序
- void receive_bit(void); //MLX90614接收位子程序
- void delay(uint N); //延時程序
- uint memread(void); //讀溫度數據
- void init1602(void); //LCD 初始化子程序
- void chk_busy_flg(void); //LCD 判斷忙子程序
- void dis_cmd_wrt(uchar cmd); //LCD 寫命令子程序
- void dis_dat_wrt(uchar dat); //LCD 寫數據子程序
- void display(uint Tem); // 顯示子程序
- void baojing(void); //報警子程序
- void duoji();
- //*************主函數*******************************************
- void main()
- {
- uint Tem,k; //函數部分
- SCL=1;
- SDA=1;
- _nop_(); _nop_();_nop_();_nop_();
- SCL=0;
- delay(1000);
- SCL=1;
- init1602();
- while(1)
- { if(!key1)
- {
- delay(4000);
- if(!key1&&H_alarm<125)
- { delay(4000);
- H_alarm++;
- delay(4000);
- }
- }
- if(!key3)
- {
- delay(4000);
- if(!key3&&H_alarm>0)
- {
- delay(4000);
- H_alarm--;
- delay(4000);
- }
- }
- dis_cmd_wrt(0x80);
- dis_dat_wrt('T');
- dis_dat_wrt(':');
- dis_cmd_wrt(0x80+0x40);
- dis_dat_wrt('E');
- dis_dat_wrt(':');
- szl[0]=H_alarm/100+0x30;
- szl[1]=H_alarm%100/10+0x30;
- szl[2]=H_alarm%100%10+0x30;
- dis_cmd_wrt(0x80+0x43);
- for(k=0;k<3;k++)
- {
- dis_dat_wrt(szl[k]);
- delay(10);
- }
- dis_cmd_wrt(0x80+0x48);
- dis_dat_wrt(0xdf);
- dis_dat_wrt('C');
- dis_cmd_wrt(0x88);
- dis_dat_wrt(0xdf);
- dis_dat_wrt('C');
- Tem=memread();
- display(Tem);
- delay(20);
- tempalarm=num;
- baojing();
- duoji ();
- }
- }
- //******************************
- //*************報警控制*******************************************
- void baojing(void)
- {
- if(tempalarm>=H_alarm)//報警判斷
- {
- for(t=0;t<100;t++)
- {
- buzzer=0;//打開報警蜂鳴器
- delay(2000);
- buzzer=1;
- delay(2000);
- }
- }
- }
- void delay_nms(unsigned int n)
- {
- unsigned int j=0;
- unsigned char i=0;
- for(j=0;j<n;j++)
- {
- for(i=0;i<120;i++)
- {
- _nop_();
- }
- }
- }
- //*********輸入轉換并顯示*********
- void display(uint Tem)
- {
- uint T,a,b;
- T=Tem*2;
- if(!key2)
- {
- delay(20);
- if(!key2)
- {
- delay(20);
- T=(0.1923*(T*0.01-273.15)+29.9250)*100+27315; //è?ì?ì???213¥
-
- }
- }
- dis_cmd_wrt(0x83); //初始化顯示位置
- if(T>=27315)
- {
- T=T-27315;
- a=T/100;
- num=a;
- b=T-a*100;
- //---------------------------
- if(a>=100)
- {
- dis_dat_wrt(0x30+a/100);
- a=a%100;
- dis_dat_wrt(0x30+a/10);
- a=a%10;
- dis_dat_wrt(0x30+a);
- }
- else if(a>=10)
- {
- dis_dat_wrt(0x30+a/10);
- a=a%10;
- dis_dat_wrt(0x30+a);
- }
- else
- {
- dis_dat_wrt(0x30+a);
- }
- dis_dat_wrt(0x2e);// 顯示點
- //---------------------------
- if(b>=10)
- {
- dis_dat_wrt(0x30+b/10);
- b=b%10;
- }
- else
- {
- dis_dat_wrt(0x30);
- }
- }
- //==========
- else
- {
- T=27315-T;
- a=T/100;
- num=a;
- b=T-a*100;
- dis_dat_wrt(0x2d);
- //--------------------------
- if(a>=10)
- {
- dis_dat_wrt(0x30+a/10);
- a=a%10;
- dis_dat_wrt(0x30+a);
- }
- else
- {
- dis_dat_wrt(0x30+a);
- }
- dis_dat_wrt(0x2e);//顯示點
- //--------------------------
- if(b>=10)
- {
- dis_dat_wrt(0x30+b/10);
- b=b%10;
- dis_dat_wrt(0x30+b);
- }
- else
- {
- dis_dat_wrt(0x30);
- dis_dat_wrt(0x30+b);
- }
- }
- }
- //************************************
- void start_bit(void)
- {
- SDA=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SCL=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SDA=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SCL=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- }
- //------------------------------
- void stop_bit(void)
- {
- SCL=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SDA=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SCL=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SDA=1;
- }
- //--------- ·發送一個字節---------
- void tx_byte(uchar dat_byte)
- {
- char i,n,dat;
- n=Nack_counter;
- TX_again:
- dat=dat_byte;
- for(i=0;i<8;i++)
- {
- if(dat&0x80)
- bit_out=1;
- else
- bit_out=0;
- send_bit();
- dat=dat<<1;
- }
- receive_bit();
- if(bit_in==1)
- {
- stop_bit();
- if(n!=0)
- {n--;goto Repeat;}
- else
- goto exit;
- }
- else
- goto exit;
- Repeat:
- start_bit();
- goto TX_again;
- exit: ;
- }
- //-----------發送一個位---------
- void send_bit(void)
- {
- if(bit_out==0)
- SDA=0;
- else
- SDA=1;
- _nop_();
- SCL=1;
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- SCL=0;
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- }
- //---------- 接收一個字節--------
- uchar rx_byte(void)
- {
- uchar i,dat;
- dat=0;
- for(i=0;i<8;i++)
- {
- dat=dat<<1;
- receive_bit();
- if(bit_in==1)
- dat=dat+1;
- }
- send_bit();
- return dat;
- }
- //----------接收一個位----------
- void receive_bit(void)
- {
- SDA=1;bit_in=1;
- SCL=1;
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- bit_in=SDA;
- _nop_();
- SCL=0;
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- }
- //------------延時--------------
- void delay(uint N)
- {
- uint i;
- for(i=0;i<N;i++)
- _nop_();
- }
- //------------------------------
- uint memread(void)
- {
- start_bit();
- tx_byte(0xB4); //Send SlaveAddress ==============================
- tx_byte(0x07); //Send Command
- //------------
- start_bit();
- tx_byte(0x01);
- bit_out=0;
- DataL=rx_byte();
- bit_out=0;
- DataH=rx_byte();
- bit_out=1;
- Pecreg=rx_byte();
- stop_bit();
- return(DataH*256+DataL);
- }
- //******************LCD 顯示子函數***********************
- void init1602(void) //初始化LCD
- {
- dis_cmd_wrt(0x01);
- dis_cmd_wrt(0x0c);
- dis_cmd_wrt(0x06);
- dis_cmd_wrt(0x38);
- }
- void chk_busy_flg(void) //LCD 忙標志判斷
- {
- flag1=0x80;
- while(flag1&0x80)
- {
- P2=0xff;
- RS=0;
- RW=1;
- LCDE=1;
- flag1=P2;
- LCDE=0;
- }
- }
- void dis_cmd_wrt(uchar cmd) // 寫命令子函數
- {
- chk_busy_flg();
- P2=cmd;
- RS=0;
- RW=0;
- LCDE=1;
- LCDE=0;
- }
- void dis_dat_wrt(uchar dat) // 寫數據子函數
- {
- chk_busy_flg();
- if(flag1==16)
- {
- P2=0XC0;
- RS=0;
- RW=0;
- LCDE=1;
- LCDE=0;
- }
- P2=dat;
- RS=1;
- RW=0;
- LCDE=1;
- LCDE=0;
- }
- void duoji ()
- {
- flay3=0;
- if(tempalarm>=L_alarm&&tempalarm<=H_alarm)
- {
- PWM=1;
- delay(20);
- PWM=0;
- delay(20);
- flay3=1;
- }
- if(flay3==1)
- {
- delay(20);
復制代碼
|