ds18b20 温度液晶显示
单片机源代码:
- //------------------------------------------------------
- //用数码管与DS18B20设计温度报警器
- //------------------------------------------------------
- //本例将报警温度设为高:40度 低:-3度,当DS18B20感知到温度达到此临界值时
- //系统发出报警声
- #include <REG52.H>
- #include <intrins.h>
- #include "delay.h"
- #include "DS18B20.h"
- uchar display_digit[4]={0,0,0,0}; //待显示的各温度数位
- uchar LCD_display[16]=" "; //LCD显示的温度值
- uchar LCD_alarm_display[16]="H: L: "; //显示报警温度
- bit HI_Alarm=0,LO_Alarm=0; //高低温报警标志
- bit DS18B20_IS_OK; //传感器正常标志
- //温度小数位对照表
- uchar code df_table[]={0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9};
- //报警温度上下限,为进行正负数比较,此处注意设为char类型
- //取值范围为-128~+127,DS18B20支持范围为-50~+125
- char Alarm_temp_HL[4]={30,-3,0,0};//报警温度预设及存储
- uchar currentT=0; //当前读取的温度整数部分
- uchar temp_value[]={0x00,0x00}; //从DS18B20读取的温度值
- //初始化DS18B20
- uchar init_DS18B20()
- {
- uchar status;
- DQ=1; delay(8);
- DQ=0; delay(90);
- DQ=1; delay(8);
- status=DQ;
- delay(100);
- DQ=1;
- return status; //初始化成功时返回0
- }
- //读一字节
- uchar readonebyte()
- {
- uchar i,dat=0;
- DQ=1; _nop_();
- for(i=0;i<8;i++)
- {
- DQ=0;dat>>=1;
- DQ=1;_nop_();_nop_();//拉高,延时进行采样 单总线是进行与的
- if(DQ) dat|=0x80; //如果DQ是1,把1存到dat里
- delay(30);DQ=1;
- }
- return dat;
- }
- //写一字节
- void writeonebyte(uchar dat)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- DQ=0;DQ=dat&0x01; //先取最低位,先读的也是最低位
- delay(5);DQ=1;dat>>=1;
- }
- }
- //读取温度值
- void read_temp()
- {
- if(init_DS18B20()==1) //DS18B20故障
- DS18B20_IS_OK=0;
- else
- {
- writeonebyte(0xcc); //跳过序列号
- writeonebyte(0x44); //启动温度转换
- init_DS18B20();
- writeonebyte(0xcc); //跳过序列号
- writeonebyte(0xbe); //读取温度寄存器
- temp_value[0]=readonebyte(); // 温度低8位
- temp_value[1]=readonebyte(); // 温度高8位
- Alarm_temp_HL[2]=readonebyte(); //报警温度TH
- Alarm_temp_HL[3]=readonebyte(); //报警温度TL
- DS18B20_IS_OK=1;
- }
- }
- //设置DS18B20温度报警值
- void set_Alarm_temp()
- {
- init_DS18B20();
- writeonebyte(0xcc); //跳过序列号
- writeonebyte(0x4e); //将设定的温度报警值写入DS18B20
- writeonebyte(Alarm_temp_HL[0]); //写TH
- writeonebyte(Alarm_temp_HL[1]); //写TL
- writeonebyte(0x7f); //12位精度
- init_DS18B20();
- writeonebyte(0xcc); //跳过序列号
- writeonebyte(0x48); //温度报警值存入DS18B20
- }
- //在液晶上显示温度处理
- void chuli_temp()
- {
- //------------------显示温度处理-------------------------
- uchar ng=0; //负数标识
- uchar t=150; //延时值
- char signed_current_temp; //注意类型为char
- //如果为负数则取反加1,并设置负号标识及负号显示位置
- if((temp_value[1]&0xf8)==0xf8)
- {
- temp_value[1]=~temp_value[1];
- temp_value[0]=~temp_value[0]+1;
- if(temp_value[0]==0x00) temp_value[1]++;
- ng=1;
-
- }
- //查表得到温度小数部分
- display_digit[0]=df_table[temp_value[0]&0x0f];
- //获取温度整数部分(无符号)
- currentT=((temp_value[0]&0xf0)>>4)|((temp_value[1]&0x07)<<4);
- //有符号的当前温度值,注意定义为char,其值可为-128~+127
- signed_current_temp=ng?-currentT:currentT;
- //高低温报警标志设置(与定义为char类型的Alarm_temp_HL比较,这样可区分正负比较)
- HI_Alarm=signed_current_temp>=Alarm_temp_HL[2]? 1 : 0;
- LO_Alarm=signed_current_temp<=Alarm_temp_HL[3]? 1 : 0;
- //将整数部分分解为三位待显示数字
- display_digit[3]=currentT/100;
- display_digit[2]=currentT%100/10;
- display_digit[1]=currentT%10;
- //LCD显示数据
- LCD_display[2]=display_digit[3]+'0'; //bai
- LCD_display[3]=display_digit[2]+'0'; //shi
- LCD_display[4]=display_digit[1]+'0'; //ge
- LCD_display[5]='.';
- LCD_display[6]=display_digit[0]+'0'; //xiaoshu
- LCD_display[7]=0xdf; //。
- LCD_display[8]=0x43; // C
- if(display_digit[3]==0)
- {
- LCD_display[2]=' '; //高位为0则不显示
- if(display_digit[2]==0)
- {
- LCD_display[3]=' ';
- }
- }
- //负号显示在恰当位置
- if(ng)
- {
- if(LCD_display[3]==' ')
- LCD_display[3]='-';
- else if(LCD_display[2]==' ')
- LCD_display[2]='-';
- else LCD_display[1]='-';
- }
- //------------------------------------------------------
- }
- void chuli_alarm_temp()
- {
- //---------------------报警温度处理---------------------
- uchar ng;
- //由于Alarm_temp_HL类型为char 故可以直接进行正负比较
- //高温报警值
- ng=0;
- if(Alarm_temp_HL[2]<0)
- {
- Alarm_temp_HL[2]=~Alarm_temp_HL[2]+1;
- ng=1;
- }
- //分解高温各数位到待显示数组中
- LCD_alarm_display[3]=Alarm_temp_HL[2]/100+'0';
- LCD_alarm_display[4]=Alarm_temp_HL[2]%100/10+'0';
- LCD_alarm_display[5]=Alarm_temp_HL[2]%10+'0';
- //屏蔽高位不为0的
- if(LCD_alarm_display[3]=='0') LCD_alarm_display[3]=' ';
- if(LCD_alarm_display[3]==' ' && LCD_alarm_display[4]=='0')
- LCD_alarm_display[4]=' ';
- //'-' 显示
- if(ng)
- {
- if(LCD_alarm_display[4]==' ') LCD_alarm_display[4]='-';
- else
- if(LCD_alarm_display[3]==' ') LCD_alarm_display[3]='-';
- else LCD_alarm_display[2]='-';
- }
- //低温报警值
-
-
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
完整代码下载:
ds18b20液晶显示.zip
(63.44 KB, 下载次数: 177)
|