本帖最后由 新手小白. 于 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);
复制代码
|