基于51单片机温度检测报警

张开发
2026/5/17 10:39:57 15 分钟阅读
基于51单片机温度检测报警
基于51单片机温度检测报警仿真程序原理图设计报告功能介绍具体功能1.采用DS18B20测温测量温度范围-55~125度2.三极管进行驱动数码管显示3.按键可设置温度阈值4.当测得温度超过阈值蜂鸣器LED声光报警程序#include reg52.h //包含头文件一般情况不需要改动头文件包含特殊功能寄存器的定义 #include intrins.h #define u8 unsigned char #define uchar unsigned char #define uint unsigned int #define u16 unsigned int uchar yushe_wendu50; //温度预设值 uint wendu; //温度值全局变量 //运行模式 uchar Mode0; //1是设置温度阀值 0是正常监控模式 void Init_Timer0(void); bit ReadTempFlag;//定义读时间标志 //管脚声明 sbit Led_Reg P1^4; //红灯 sbit Buzzer P1^1; //蜂鸣器 sbit DQ P1^0; //ds18b20的数据引脚 #define SMG_NUM 4 u8 code DisplayNum[16]{ 0xc0, //0 0xf9, //1 0xa4, //2 0xb0, //3 0x99, //4 0x92, //5 0x82, //6 0xf8, //7 0x80, //8 0x90, //9 0x88, //A 0x83, //b 0xc6, //C 0xa1, //d 0x86, //E 0x8e //F }; // u8 code DisplayOther[]{ 0xff, //0 空 0x7f, //1 . 0xbf, //2 - 0xa7 //3 c }; /******************************************************************** * 名称 : delay_1ms() * 功能 : 延时1ms函数 * 输入 : q * 输出 : 无 ***********************************************************************/ void delay_ms(uint q) { uint i,j; for(i0;iq;i) for(j0;j110;j); } //数码管位选定义 sbit smg_we1 P2^0; //数码管位定义 sbit smg_we2 P2^1; //数码管位定义 sbit smg_we3 P2^2; //数码管位定义 sbit smg_we4 P2^3; //数码管位定义 /***********************数码位选函数*****************************/ void smg_we_switch(uchar i) { switch(i) { case 0: smg_we1 0; smg_we2 1; smg_we3 1; smg_we4 1; break; case 1: smg_we1 1; smg_we2 0; smg_we3 1; smg_we4 1; break; case 2: smg_we1 1; smg_we2 1; smg_we3 0; smg_we4 1; break; case 3: smg_we1 1; smg_we2 1; smg_we3 1; smg_we4 0; break; } } //完整资料 //微信公众号木子单片机 /******************************************************************** * 名称 : u8 ChangeFor(u8 dat) * 功能 : 交换一个字节位的位置,用于数码管显示 * 输入 : 需要改变的数 * 输出 : 改变后的数 ***********************************************************************/ #define LED_a 0 //数码管段选的a段接在段选IO口的第0位 #define LED_b 2 #define LED_c 6 #define LED_d 4 #define LED_e 3 #define LED_f 1 #define LED_g 7 #define LED_dp 5 u8 ChangeFor(u8 dat) { u8 temp0; if(dat0x01) //判断数据的第一位是否为1 temp|0x01LED_a;//如果为1,放到控制数码管a段的位置 if(dat0x02) temp|0x01LED_b; if(dat0x04) temp|0x01LED_c; if(dat0x08) temp|0x01LED_d; if(dat0x10) temp|0x01LED_e; if(dat0x20) temp|0x01LED_f; if(dat0x40) temp|0x01LED_g; if(dat0x80) temp|0x01LED_dp; return temp; } uchar dis_smg[SMG_NUM]; //显示缓存数组 /******************************************************************** * 名称 : display() * 功能 : 数码管显示 * 输入 : 无 * 输出 : 无 ***********************************************************************/ void DisplayScan() { static uchar i; P0 0xff; //消隐 smg_we_switch(i); //位选 P0 ChangeFor(dis_smg[i]); //段选 i; if(iSMG_NUM) i0; } /*****延时子程序该延时主要用于ds18b20延时*****/ void Delay_DS18B20(int num) { while(num--) ; } /*****初始化DS18B20*****/ void Init_DS18B20(void) { unsigned char x0; DQ 1; //DQ复位 Delay_DS18B20(8); //稍做延时 DQ 0; //单片机将DQ拉低 Delay_DS18B20(80); //精确延时大于480us DQ 1; //拉高总线 Delay_DS18B20(14); x DQ; //稍做延时后如果x0则初始化成功x1则初始化失败 Delay_DS18B20(20); } /*****读一个字节*****/ unsigned char ReadOneChar(void) { unsigned char i0; unsigned char dat 0; for (i8;i0;i--) { ET00; //定时器中断关闭 DQ 0; // 给脉冲信号 dat1; DQ 1; // 给脉冲信号 if(DQ) dat|0x80; Delay_DS18B20(4); ET01; //定时器中断打开 } return(dat); } /*****写一个字节*****/ void WriteOneChar(unsigned char dat) { unsigned char i0; for (i8; i0; i--) { ET00; //定时器中断关闭 DQ 0; DQ dat0x01; Delay_DS18B20(5); DQ 1; dat1; ET01; //定时器中断打开 } } u8 tempflag0; //负号显示 /*****读取温度*****/ unsigned int ReadTemperature(void) { unsigned char a0; unsigned char b0; unsigned int t0; u8 tt0; u8 ttt0; Init_DS18B20(); WriteOneChar(0xCC); //跳过读序号列号的操作 WriteOneChar(0x44); //启动温度转换 Init_DS18B20(); WriteOneChar(0xCC); //跳过读序号列号的操作 WriteOneChar(0xBE); //读取温度寄存器 aReadOneChar(); //读低8位 bReadOneChar(); //读高8位 tb; t8; tt|a; //合并高低位为一个字节 tt(u8)(t4); //得到整数位 ttt(u8)((t%16)*0.0625*10); //得到小数位;乘以10代表保留一位小数; if(tt0x80) { tempflag1; //负号显示 tt~tt1; //实际温度值为读取值的补码 } else tempflag0; //正号显示 t tt*10ttt; //放大10倍 return(t); } /*****校准温度*****/ u16 check_wendu(void) { u16 c; cReadTemperature(); //获取温度值并减去DS18B20的温漂误差 return c; } //按键 sbit Key1P1^5; //设置键 sbit Key2P3^3; //加按键 sbit Key3P3^4; //减按键 #define KEY_SET 1 //设置 #define KEY_ADD 2 //加 #define KEY_MINUS 3 //减 // // 函数: u8 Key_Scan() // 应用: tempu8 Key_Scan(); // 描述: 按键扫描并返回按下的键值 // 参数: NONE // 返回: 按下的键值 // 版本: VER1.0 // 日期: 2015-05-29 // 备注: 该函数带松手检测,按下键返回一次键值后返回0,直至第二次按键按下 // u8 Key_Scan() { static u8 key_up1;//按键按松开标志 if(key_up(Key10||Key20||Key30)) { delay_ms(10);//去抖动 key_up0; if(Key10) return 1; else if(Key20)return 2; else if(Key30)return 3; } else if(Key11Key21Key31) key_up1; return 0;// 无按键按下 } void main (void) { u8 key; dis_smg[0] DisplayOther[2] ; dis_smg[1] DisplayOther[2] ; dis_smg[2] DisplayOther[2] ; dis_smg[3] DisplayOther[2] ; Init_Timer0(); wenducheck_wendu(); //初始化时调用温度读取函数 防止开机85°C delay_ms(1000); wenducheck_wendu(); //初始化时调用温度读取函数 防止开机85°C while (1) //主循环 { keyKey_Scan(); //按键扫描 if(ReadTempFlag1) { ReadTempFlag0; wenducheck_wendu(); //读取温度值 } if(keyKEY_SET) { Mode; } switch(Mode) //判断模式的值 { case 0: //监控模式 if(tempflag) dis_smg[0] DisplayOther[2] ; else dis_smg[0] DisplayNum[wendu/1000%10] ; dis_smg[1] DisplayNum[wendu/100%10]; dis_smg[2] DisplayNum[wendu/10%10]DisplayOther[1] ; dis_smg[3] DisplayNum[wendu%10] ; if(wendu(yushe_wendu*10)) //温度大于等于预设温度值时为什么是大于预设值*10因为我们要显示的温度是有小数点后一位是一个3位数25.9°C时实际读的数是259所以判断预设值时将预设值*10 { Buzzer0; //打开蜂鸣器报警 Led_Reg0; //打开温度报警灯 } else //温度值小于预设值时 { Led_Reg1; //关闭报警灯 Buzzer1; //停止报警 } break; case 1://预设温度模式 { dis_smg[0] DisplayNum[yushe_wendu/100%10]; dis_smg[1] DisplayNum[yushe_wendu/10%10] ; dis_smg[2] DisplayNum[yushe_wendu%10]DisplayOther[1] ; dis_smg[3] DisplayOther[3] ; if(keyKEY_ADD) //加键按下 { yushe_wendu; //预设温度值阀值加1 if(yushe_wendu99) //当阀值加到大于等于99时 yushe_wendu99; //阀值固定为99 } if(keyKEY_MINUS) //减键按下 { if(yushe_wendu1) //当温度上限值减小到1时 yushe_wendu1; //固定为1 yushe_wendu--; //预设温度值减一,最小为0 } break; //执行后跳出switch } default : { Mode0; //恢复正常模式 break; } } } }硬件设计使用元器件单片机STC89C52注意单片机是通用的无论51还是52、无论stc还是at都一样引脚功能都一样。程序也是一样的。蜂鸣器BUZZER排针4-Pin直插瓷片电容:30P自锁开关直插电解电容:10ufPNP 三极管5MM直插发光二极管DC电源插座色环电阻:1K、10K、2.2K0.56寸4位共阳数码管数字温度管:DS18B20按键6X6X5MM晶振:12MHZ导线若干结构框图设计资料01仿真图本设计使用proteus7.8和proeus8.9两个版本设计具体如图02原理图本系统原理图采用Altium Designer19设计具体如图03程序本设计使用软件keil5版本编程设计具体如图04设计报告七千字设计报告具体如下05设计资料全部资料包括程序含注释、仿真源文件 、AD原理图、任务书、开题报告、设计报告、实物图、元件清单等。具体内容如下全网最全! ​大家共同学习进步点赞分享一起学习成长。

更多文章