新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于51DS18B20室內(nèi)溫度報(bào)警系統(tǒng)設(shè)計(jì)

基于51DS18B20室內(nèi)溫度報(bào)警系統(tǒng)設(shè)計(jì)

作者: 時(shí)間:2016-11-29 來源:網(wǎng)絡(luò) 收藏
#include
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P2^2;
sbit smg1=P2^4;
sbit smg2=P2^5;
sbit smg3=P2^6;
uchar temp;
unsigned char code smg_du[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
unsigned char code smg_we[]={0x08,0x18,0x28,0x38,0x48,0x58,0x68,0x78};
void init_ds18b20(void);
void delay(uint t);
void write_byte(uchar dat);
uchar read_byte(void);
uchar readtemperature(void);
void display(void);
//************************************************
//延時(shí)函數(shù),在12MHz的晶振頻率下
//大約50us的延時(shí)
//************************************************
void delay_50us(uint t)
{
uchar j;
for(;t>0;t--)
for(j=19;j>0;j--);
}
void main(void)
{
while(1)
{
temp=readtemperature();
display();
}
}
void delay(uint t)
{
while(t--);
}
void init_ds18b20(void)
{
uchar n;
DQ=1;
delay(8);
DQ=0;
delay(80);
DQ=1;
delay(8);
n=DQ;
delay(4);
}
void write_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
DQ=0;
DQ=dat&0x01;
delay(4);
DQ=1;
dat>>=1;
}
delay(4);
}
uchar read_byte(void)
{
uchar i,value;
for(i=0;i<8;i++)
{
DQ=0;
value>>=1;
DQ=1;
if(DQ)
value|=0x80;
delay(4);
}
return value;
}
uchar readtemperature(void)
{
uchar a,b;
init_ds18b20();
write_byte(0xcc); //跳過ROM
write_byte(0x44); //啟動(dòng)溫度測(cè)量
delay(300);
init_ds18b20();
write_byte(0xcc);
write_byte(0xbe);
a=read_byte();
b=read_byte();
b<<=4;
b+=(a&0xf0)>>4;
return b;
}
void display(void)
{
smg3=0;
smg2=0;
smg1=0;
P1=smg_du[temp%10];
delay_50us(20);
smg2=0;
smg1=1;
P1=smg_du[temp/10];
delay_50us(20);
}


評(píng)論


技術(shù)專區(qū)

關(guān)閉