標(biāo)題:
DS18B20溫度檢測(cè)顯示控制
[打印本頁(yè)]
作者:
思如儂
時(shí)間:
2017-6-2 23:13
標(biāo)題:
DS18B20溫度檢測(cè)顯示控制
DS18B20溫度檢測(cè)顯示控制,已經(jīng)過(guò)仿真驗(yàn)證
下載:
DS18B20溫度檢測(cè)控制.docx
(15.85 KB, 下載次數(shù): 9)
2017-6-2 23:11 上傳
點(diǎn)擊文件名下載附件
DS18B20溫度檢測(cè)
下載積分: 黑幣 -5
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS=P3^5; //定義LCD端口
sbit RW=P3^6;
sbit E=P3^7;
sbit DQ=P3^4; //定義溫度總線端口
sbit Wa=P1^5; //定義報(bào)警端口
sbit ZR=P1^7; //制熱
sbit ZL=P1^6; //制冷
uchar LCD_3,LCD_2,LCD_1,LCD_0;
uchar ds18b20_romA[8]={0x8e,0x00,0x00,0x00,0xb8,0xc5,0x30,0x28};
uchar _romB[8]={0x52,0x00,0x00,0x00,0xb8,0xc5,0x34,0x28};
uchar _romC[8]={0x65,0x00,0x00,0x00,0xb8,0xc5,0x35,0x28};
uchar _romD[8]={0x3c,0x00,0x00,0x00,0xb8,0xc5,0x36,0x28};
uint key_[2]={4,0}; //設(shè)置溫度標(biāo)準(zhǔn)值
uchar code LCDData[] ="0123456789";
uchar code dot_tab[] ="0112334456678899";
uchar first_line[16]="A:000.0 B:000.0"; //LCD第一行顯示緩存數(shù)組 A:溫度 B:濕度
void delay10us(uint t);
void check_busy();
void write_command(uchar tempdata);
void write_data(uchar tempdata);
void init_lcd1602();
bit resetpulse();
void ds18b20_init();
uchar read_bit()
uchar read_byte();
void write_bit(uchar bitval);
void write_byte(uchar val);
bit match_rom(uchar *rom);
void convert_T(uchar temp_data_h,uchar temp_data_l);
void display();
void work_temp();
//************************************
//延時(shí)程序
//************************************
//延時(shí)t*10us
void delay10us(uint t)
{
do
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
while(--t);
}
//************************************
//LCD1602測(cè)忙
//************************************
void check_busy(void)
{
while(1)
{
P0=0xff;
E=0;
_nop_();
RS=0;
_nop_();
_nop_();
RW=1;
_nop_();
_nop_();
E=1;
_nop_();
_nop_();
_nop_();
_nop_();
if((P0&0x80)==0) //等于1表示液晶正忙,重復(fù)檢測(cè)直到其等于0為止
{
break;
}
E=0;
}
}
//************************************
//LCD1602寫(xiě)命令
//************************************
void write_command(uchar tempdata)
{
E=0;
_nop_();
_nop_();
RS=0;
_nop_();
_nop_();
RW=0;
P0=tempdata;
_nop_();
_nop_();
E=1;
_nop_();
_nop_();
E=0;
_nop_();
check_busy();
}
//************************************
//LCD1602寫(xiě)數(shù)據(jù)
//************************************
void write_data(uchar tempdata)
{
E=0;
_nop_();
_nop_();
RS=1;
_nop_();
_nop_();
RW=0;
P0=tempdata;
_nop_();
_nop_();
E=1;
_nop_();
_nop_();
E=0;
_nop_();
check_busy();
}
//************************************
//初始化LCD1602
//************************************
void init_lcd1602()
{
write_command(0x01); //清除屏幕
write_command(0x38); //功能設(shè)定(8位,2行,5*7點(diǎn)陣式)
write_command(0x0c); //顯示器ON,光標(biāo)OFF,閃爍OFF
write_command(0x06); //地址加1,光標(biāo)右移,顯示屏不移動(dòng)
}
//************************************
//初始化ds18b20
//************************************
bit resetpulse(void)
{
DQ=0;
delay10us(60); //延時(shí)600us
DQ=1;
delay10us(5); //延時(shí)50us
return(DQ); //讀取P3.4的狀態(tài)
}
void ds18b20_init(void)
{
while(1)
{
if(!resetpulse()) //收到ds18b20的應(yīng)答信號(hào)
{
DQ=1;
delay10us(20); //延時(shí)200us
break;
}
else
resetpulse(); //否則再發(fā)復(fù)位信號(hào)
}
}
//************************************
//讀ds18b20一位
//************************************
uchar read_bit(void)
{
DQ=0;
_nop_();
_nop_();
DQ=1;
delay10us(3); //延時(shí)30us
return(DQ); //讀取數(shù)據(jù)口狀態(tài)
}
//************************************
//讀ds18b20一個(gè)字節(jié)
//************************************
uchar read_byte(void)
{
uchar i,m,receive_data;
m=1;
receive_data=0;
for(i=0;i<8;i++) //低位在前
{
if(read_bit())
{
receive_data=receive_data+(m<<i);
}
delay10us(6); //延時(shí)60us
}
return(receive_data);
}
//************************************
//向ds18b20寫(xiě)一位
//************************************
void write_bit(uchar bitval)
{
DQ=0;
if(bitval==1)
DQ=1;
delay10us(6); //延時(shí)60us
DQ=1;
}
//************************************
//向DS18B20寫(xiě)一個(gè)字節(jié)命令
//************************************
void write_byte(uchar val)
{
uchar i,temp;
for(i=0;i<8;i++)
{
temp=val>>i;
temp=temp&0x01;
write_bit(temp);
delay10us(6);
}
}
//*************************************
//溫度的ROM序列號(hào)匹配 //*
//*************************************
bit match_rom(uchar *rom)
{
uchar i;
ds18b20_init();
write_byte(0x55);
for(i=8;i>0;i--)
{
write_byte(*(rom+i-1));
}
return 1;
}
//*************************************
//溫度轉(zhuǎn)換,將從ds18b20中讀出來(lái)的溫度值
//轉(zhuǎn)化成十進(jìn)制的溫度值,并存放顯示緩存
//*************************************
void convert_T(uchar temp_data_h,uchar temp_data_l)
{
uchar temp;
if((temp_data_h&0xf0)==0xf0) //如果溫度寄存器里的高位為1,
{ //則溫度為負(fù)
temp_data_l=~temp_data_l; //負(fù)溫度將補(bǔ)碼轉(zhuǎn)成二進(jìn)制,
if(temp_data_l==0xff) //取反再加1
{
temp_data_l=temp_data_l+0x01;
temp_data_h=~temp_data_h;
temp_data_h=temp_data_h+0x01;
}
else
{
temp_data_l=temp_data_l+0x01;
temp_data_h=~temp_data_h;
}
LCD_0=dot_tab[temp_data_l&0x0f]; //查表得小數(shù)位的值
temp=((temp_data_l&0xf0)>>4)|((temp_data_h&0x0f)<<4);
LCD_3='-'; //顯示"-"號(hào)
LCD_2=LCDData[(temp%100)/10]; //查表得負(fù)溫度溫度十位
LCD_1=LCDData[(temp%100)%10]; //查表得負(fù)溫度個(gè)位
}
else //溫度為正
{
LCD_0=dot_tab[temp_data_l&0x0f]; //查表得小數(shù)位的值
temp=((temp_data_l&0xf0)>>4)|((temp_data_h&0x0f)<<4);
LCD_3=LCDData[temp/100]; //查表得溫度百位
LCD_2=LCDData[(temp%100)/10]; //查表得溫度十位
LCD_1=LCDData[(temp%100)%10]; //查表得溫度個(gè)位
}
}
void display(void) //LCD顯示
{
uchar i;
write_command(0x80);
for(i=0;i<16;i++)
{
write_data(first_line[i]);
}
write_command(0xc0);
for(i=0;i<16;i++)
{
write_data(second_line[i]);
}
}
//*************************************
//加熱控制,制冷控制,報(bào)警
//*************************************
void work_temp()
{
uchar tt_[2];
uchar tt;
uchar temp_data_l;
uchar temp_data_h;
temp_data_l= read_byte(); //溫度低8位
temp_data_h = read_byte(); //溫度高8位
tt_[0]=temp_data_l&0xf0;
tt_[1]=temp_data_h&0x0f;
tt=tt_[0]+tt_[1];
if(tt>0x0A) //判斷是否高于10度
{
Wa=0; //報(bào)警
……………………
…………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
作者:
by64214
時(shí)間:
2017-6-3 06:32
謝謝分享 學(xué)習(xí)一下
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1