需要的可以自行下载(包括原理图文件)
单片机源程序如下:
- #include <REGX51.H>
- #include "18b20.h"
- #include "LCD1602.h"
- #include <INTRINS.H>
- #include "Buzzer.h"
- sbit K1=P3^0;
- sbit K2=P3^1;
- sbit K3=P3^2;
- float T;
- unsigned flag=0; //转换上下限温度设置
- main()
- {
- int high=37,high1=37,low=0,low1=0;//警告温度上下限,不能用unsigned
- LCD_Init(); //lcd初始化
- DS18B20_ConvertT(); //转换温度
- delay(3000); //等待转换
-
- while(1)
- {
- //===================非警告状态显示字符串、数字提示==============================
- LCD_ShowString(2,10,"'C");
- LCD_ShowString(1,1,"high");
- LCD_ShowString(1,9,"low");
- LCD_ShowString(2,1,"Temp");
- LCD_ShowNum(1,6,high1,2);
- LCD_ShowNum(1,13,low1,2);
- delay(400);
-
- //==================报警温度上下限设置==============================
-
- if(K1==0){delay(200);if(K1==0)flag++;}//上下限设置转换
-
- if(flag%2==0) //上限设置
- {
- if(K2==0){delay(400);if(K2==0)high++;}
- if(K3==0){delay(400);if(K3==0)high--;}
- }
- if(flag%2!=0) //下限设置
- {
- if(K2==0){delay(400);if(K2==0)low++;}
- if(K3==0){delay(400);if(K3==0)low--;}
- }
-
- //=================零下温度处理=====================================
- if(high<0){high1=-high;LCD_ShowChar(1,5,'-');}
- else high1=high;
- if(low<0){low1=-low;LCD_ShowChar(1,12,'-');}
- else low1=low;
-
- //===================判断报警======================================
- while(DS18B20_ReadT()<(float)low||DS18B20_ReadT()>(float)high)
- {
- DS18B20_ConvertT(); //转换温度,保证温度回归正常值后跳出报警
- Buzzer_Time(200);
- LCD_ShowString(2,3,"warning!!!");
- delay(400);
- LCD_WriteCommand(0x01);//清屏,一定要清屏
- }
-
- while(DS18B20_ReadT()<low||DS18B20_ReadT()>high)
- {
- DS18B20_ConvertT(); //转换温度,保证温度回归正常值后跳出报警
- Buzzer_Time(200);
- LCD_ShowString(2,3,"warning!!!");
- delay(400);
- LCD_WriteCommand(0x01);//清屏,一定要清屏
- }
-
-
- //=====================显示温度====================================
-
- DS18B20_ConvertT(); //转换温度
- T=DS18B20_ReadT(); //读取温度
- delay(100); //等待转换,不能删除
-
- if(T<0) //如果温度小于0
- {
- LCD_ShowChar(2,5,'-'); //显示负号
- T=-T; //将温度变为正数
- }
- else //如果温度大于等于0
- {
- LCD_ShowChar(2,5,' '); //显示正号
- }
-
- LCD_ShowNum(2,6,T,2); //显示温度整数部分
- LCD_ShowChar(2,8,'.'); //显示小数点
- LCD_ShowNum(2,9,(unsigned long)(T*10)%10,1);//显示温度小数部分
- }
- }
复制代码
Keil代码与Proteus仿真下载:
18b20ccc.rar
(183.14 KB, 下载次数: 292)
|