新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于51單片機(jī)的天氣溫度報(bào)警系統(tǒng)

基于51單片機(jī)的天氣溫度報(bào)警系統(tǒng)

作者: 時(shí)間:2016-11-30 來源:網(wǎng)絡(luò) 收藏

/*向16602寫入指令*/
void write_com_1602(uchar com)
{
lcdrs=0;
lcdrw=0;
P3=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
/*從1602讀取數(shù)據(jù)*/
void write_data_1602(uchar date)
{
lcdrs=1;
lcdrw=0;
P3=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}

/*判斷溫度正負(fù)*/
void checkMSB(uchar ta)
{
if(ta==0x07)
{
write_com_1602(0x80+0x48);
write_data_1602(0x20);
TN=MSB*16+LSB/16;
TD=(LSB%16)*10/16;
if(TN>=TH){P1=~P1;} //如果溫度設(shè)置的超出上限,LED閃爍
else{P1=0xFF;}
}else{
write_com_1602(0x80+0x48);
display_minus();
LSB=~LSB+1;
MSB=~MSB;
TN=MSB*16+LSB/16;
TD=(LSB%16)*10/16;
P1=~P1; //如果溫度超出設(shè)置的下限(小于0),LED閃爍
}
}

/*向DS1302寫數(shù)據(jù),分別寫入地址和指令*/
void write_com_1302(uchar add,com)
{
uchar i;
CE=1;
for(i=0;i<8;i++)
{
SCLK=0;
IO=add&0x01;
add>>=1;
SCLK=1;
}
for(i=0;i<8;i++)
{
SCLK=0;
IO=com&0x01;
com>>=1;
SCLK=1;
}
SCLK=0;
CE=0;
}
/*從DS1302中讀數(shù)據(jù),但須先寫入地址*/
uchar read_data_1302(uchar add)
{
uchar Data,i;
CE=1;
for(i=0;i<8;i++)
{
SCLK=0;
IO=add&0x01;
add>>=1;
SCLK=1;
}
for(i=0;i<8;i++)
{
SCLK=0;
Data>>=1;
if(IO==1)Data|=0x80;
SCLK=1;
_nop_();
}
SCLK=0;
CE=0;
return Data;
}
/*將從DS1302中讀到的數(shù)據(jù)換化為10進(jìn)制*/
uchar changenum(uchar num)
{
uchar TH,TL,new_num;
TH=num&0xF0;
TH>>=4;
TH*=10;
TL=num&0x0F;
new_num=TH+TL;
return new_num;
}
void main()
{
while(1)
{
Temperature_conversion(); //溫度轉(zhuǎn)換
LSB=read_scratchpad(); //讀取LSB位數(shù)據(jù)
MSB=read_scratchpad(); //讀取MSB位數(shù)據(jù)
TH=read_scratchpad(); //讀取TH Register數(shù)據(jù)
TL=read_scratchpad(); //讀取TL Register數(shù)據(jù)
CR=read_scratchpad(); //讀取Configuration Register數(shù)據(jù)
TA=MSB|0x07; //按位與,從MSB位判斷溫度正負(fù)
checkMSB(TA); //判斷溫度正負(fù)
write_com_1602(0x80+0x49);
display_temp1(TN);
display_dot();
display_temp2(TD);
display_cent();
write_com_1602(0x80);
YY=read_data_1302(0x8D);
YY=changenum(YY);
display_temp3(YY);
display_minus();
MM=read_data_1302(0x89);
MM=changenum(MM);
display_temp3(MM);
display_minus();
DD=read_data_1302(0x87);
DD=changenum(DD);
display_temp3(DD);
write_data_1602(0x20);
write_data_1602(0x20);
write_data_1602(0x20);
HH=read_data_1302(0x85);
HH=changenum(HH);
display_temp3(HH);
display_minus();
MIN=read_data_1302(0x83);
MIN=changenum(MIN);
display_temp3(MIN);
WEEK=read_data_1302(0x8B)-1;
write_com_1602(0x80+0x45);
write_data_1602(digit[WEEK]);
//SEC=read_data_1302(0x81);
//SEC=changenum(SEC);
//display_temp3(SEC);
}
}


上一頁 1 2 下一頁

評(píng)論


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

關(guān)閉