數(shù)字溫度計程序
#include
#include
sbitRST=P2^0;
sbitCLK=P2^1;
sbitDQ=P2^2;
sbitTSOR=P2^3;
sbitALERT=P2^4;
sbitRS=P2^7;
sbitRW=P2^6;
sbitEN=P2^5;
/*------------------------------------------全局變量-------------------------------------------------------*/
statICunsignedchartemp1,temp2;//溫度值的整數(shù)部分、小數(shù)部分
staticunsignedcharpos,posset;//數(shù)字電位器電位值、設(shè)定值
staticunsignedcharmin,sec;//分鐘、秒
staticunsignedcharcount;//Timer0中斷計數(shù)
staticunsignedcharminset;//設(shè)定的分鐘數(shù)
staticunsignedcharstatus1,status2;//狀態(tài)標(biāo)志
bitstop,timeover;//定時停止、結(jié)束
staticcharline0[]="00:00";
staticcharline1[]=".CW";
/*-------------------------------------------------------------------------------------------------------------*/
voidInitInterupt();
voidKeyboardDelay();
/*-------------------------------------------LCD驅(qū)動函數(shù)------------------------------------------------*/
voidDelayL();
voidDelayS();
voidWriteCommand(unsignedcharc);
voidWritEDAta(unsignedcharc);
voidShowChar(unsignedcharpos,unsignedcharc);
voidShowString(unsignedcharline,char*ptr);
voidInitLcd();
/*----------------------------------------------鍵盤-程序--------------------------------------------------*/
unsignedcharGetKey();
/*---------------------------------------------數(shù)字溫度計驅(qū)動-------------------------------------------*/
voidChangePos(bitsel,unsignedcharpos1,unsignedcharpos2);
/*------------------------------------------溫度傳感器驅(qū)動----------------------------------------------*/
voidDelay15();
voidDelay60();
voidDelay100ms();
voidWrite0TS();
voidWrite1TS();
bitReaDTS();
voidResetTS();
voidWriteByteTS(unsignedcharbyte);
unsignedcharReadByteTS();
voidInitTS();
voidGetTempTS();
/*-------------------------------------------------主程序---------------------------------------------------*/
voidmain(void){
charcodestr1[]="HelloWorld!";
charcodestr2[]="2002-10-20";
unsignedchari;
SP=0x50;
ALERT=0;//報警燈滅
TSOR=1;//1-wire總線釋放
DelayL();
InitLcd();//初始化LCD
DelayL();
ShowString(0,str1);//啟動畫面
ShowString(1,str2);
for(i=0;i<15;i++)
Delay100ms();
InitInterupt();//初始化中斷設(shè)置
minset=10;//缺省定時10分鐘
posset=0;//缺省電位器值0
min=minset;//初始化數(shù)據(jù)
pos=posset;
sec=0;
count=0;
P1=0xF0;
status1=0;
status2=0;
stop=1;
timeover=0;
ChangePos(0,255-pos,255-pos);//設(shè)置電位器
InitTS();//初始化溫度計
while(1)//循環(huán)顯示溫度值
{
GetTempTS();
line1[0]=0x20;
i=temp1;
if(i>39)//超過40攝氏度,告警燈亮
ALERT=1;
if(i>99)//超過100攝氏度,顯示溫度的百位
{
line1[0]=0x31;
i-=100;
}
line1[1]=i/10+0x30;//顯示溫度的十位
line1[2]=i%10+0x30;//顯示個位
line1[4]=temp2+0x30;//顯示小數(shù)位
if(timeover)//若定時結(jié)束,則電位器緩慢復(fù)0
{
for(;pos>0;pos--)
{
ChangePos(0,255-pos,255-pos);
_nop_();
_nop_();
}
timeover=0;
posset=0;
}
if(pos>posset)//若按鍵修改電位器位置
{
for(;pos>posset;pos--)//則緩變到設(shè)定值
{
ChangePos(0,255-pos,255-pos);
_nop_();
_nop_();
}
ChangePos(0,255-pos,255-pos);
}
elseif(pos
for(;pos
ChangePos(0,255-pos,255-pos);
_nop_();
_nop_();
}
ChangePos(0,255-pos,255-pos);
}
i=pos;
line1[9]=0x20;//顯示電位器等級值
if(i>99)
{
line1[9]=i/100+0x30;
i=i%100;
}
line1[10]=i/10+0x30;
line1[11]=i%10+0x30;
ShowString(1,line1);
line0[5]=min/10+0x30;//顯示時間
line0[6]=min%10+0x30;
line0[8]=sec/10+0x30;
line0[9]=sec%10+0x30;
ShowString(0,line0);
Delay100ms();
}
}
voidInitInterupt()
{
TMOD=0x21;//初始化中斷設(shè)置
TL1=0xFD;
TH1=0xFD;
PX0=1;
EA=1;
ES=1;
PCON=0;
TR1=1;
SCON=0x50;
TL0=0x00;//定時0.05m
TH0=0x4C;
ET0=1;
EX0=1;
IT0=1;
}
voidKeyboardDelay()//按鍵中斷延時
{
unsignedchari,j;
i=0x40;
j=0xFF;
while(i--)
while(j--);
}
/*--------------------------------------------中斷處理-----------------------------------------------------*/
Int0_process()interrupt0using0
{
unsignedcharkey;
unsignedcharkeycode[]="TP";
unsignedcharstep[3]={1,2,5};
EA=0;
key=GetKey();//獲得按鍵值
switch(key)
{
case0:
stop=!stop;
min=minset;
sec=0;
break;
case1:
case2:
case3:
if(stop)
{
minset+=step[key-1];
if(minset>60)
minset=0;
min=minset;
}
break;
case5:
case6:
case7:
if(stop)
{
minset-=step[key-5];
if(minset>60)
minset=0;
min=minset;
}
break;
case9:
case10:
case11:
posset+=step[key-9];
break;
case13:
case14:
case15:
posset-=step[key-13];
break;
default:
break;
}
TR0=!stop;
KeyboardDelay();
P1=0xF0;
EA=1;
}
Timer0_process()interrupt1using0
{
EA=0;
TR0=0;
TL0=0x00;
TH0=0x4C;
count++;
if(count==20)//如果到累計定時到達1s
{
if(sec==0)//定時處理
{
if(min==0)//總定時到,則置結(jié)束標(biāo)志
timeover=1;
else
{
min--;
sec=59;
}
}
else
sec--;
count=0;
}
TR0=1;
EA=1;
}
/*--------------------------------------LCD驅(qū)動子程序--------------------------------------------------*/
voidDelayL()
{unsignedchari,j;
i=0xF0;
j=0xFF;
while(i--)
while(j--);
}
voidDelayS()
{
unsignedchari;
i=0x1F;
while(i--);
}
voidWriteCommand(unsignedcharc)
{
DelayS();
EN=0;
RS=0;
RW=0;
_nop_();
EN=1;
P0=c;
EN=0;
}
voidWriteData(unsignedcharc)
{
DelayS();
EN=0;
RS=1;
RW=0;
_nop_();
EN=1;
P0=c;
EN=0;
RS=0;
}
voidShowChar(unsignedcharpos,unsignedcharc)
{
unsignedcharp;
if(pos>=0x10)
p=pos+0xB0;
else
p=pos+0x80;
WriteCommand(p);
WriteData(c);
}
voidShowString(unsignedcharline,char*ptr)
{
unsignedcharl,i;
l=line<<4;
for(i=0;i<16;i++)
ShowChar(l++,*(ptr+i));
}
voidInitLcd()
{
DelayL();
WriteCommand(0x38);
WriteCommand(0x38);
WriteCommand(0x06);
WriteCommand(0x0C);
WriteCommand(0x01);
WriteCommand(0x80);
}
/*---------------------------------------------鍵盤子程序-------------------------------------------------*/
unsignedcharGetKey()
{
unsignedk,t,i,j;
k=P1;
k=k&0xF0;
i=0;
while((k&0x10)&&i<4)
{
i++;
k=k>>1;
}
k=0x01;
j=0;
while(j<4)
{
P1=0xFF^k;
_nop_();
t=P1;
t=t^0xFF;
t=t&0xF0;
if(t)
break;
j++;
k=k<<1;
}
k=j*4+i;
returnk;
}
/*-----------------------------------------數(shù)字溫度計驅(qū)動子程序--------------------------------------*/
voidChangePos(bitsel,unsignedcharpos1,unsignedcharpos2)
{unsignedchari;
RST=0;
DQ=0;
CLK=0;
RST=1;
DQ=sel;
_nop_();
CLK=1;
_nop_();
CLK=0;
for(i=0;i<8;i++)
{
if(pos1&0x80)
DQ=1;
else
DQ=0;
_nop_();
CLK=1;
_nop_();
CLK=0;
pos1=pos1<<1;
}
for(i=0;i<8;i++)
{
if(pos2&0x80)
DQ=1;
else
DQ=0;
_nop_();
CLK=1;
_nop_();
CLK=0;
pos2=pos2<<1;
}
RST=0;
}
/*------------------------------------------溫度傳感器子程序-------------------------------------------*/
voidDelay100ms()//延時100ms
{
unsignedchari,j,k;
for(i=0;i<8;i++)
for(j=0;j<25;j++)
for(k=0;k<250;k++);
}
voidDelay15()//延時15us
{
unsignedchari;
for(i=0;i<8;i++);
}
voidDelay60()//延時60us
{
unsignedchari;
for(i=0;i<30;i++);
}
voidWrite0TS()//寫bit0
{
TSOR=1;
TSOR=0;
Delay15();
Delay15();
Delay15();
Delay15();
TSOR=1;
_nop_();
_nop_();
}
voidWrite1TS()//寫bit1
{
TSOR=1;
TSOR=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
TSOR=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
Delay15();
Delay15();
Delay15();
}
bitReadTS()
{
bitb;
TSOR=1;
TSOR=0;
_nop_();
_nop_();
_nop_();
_nop_();
TSOR=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
b=TSOR;
Delay15();
Delay15();
Delay15();
_nop_();
_nop_();
returnb;
}
voidResetTS()//復(fù)位
{unsignedchari;
TSOR=1;
TSOR=0;
for(i=0;i<8;i++)
Delay60();
TSOR=1;
while(TSOR);
for(i=0;i<8;i++)
Delay60();
}
voidWriteByteTS(unsignedcharbyte)//寫一個字節(jié)(byte)
{unsignedchari;
for(i=0;i<8;i++)
{
if(byte&0x01)
Write1TS();
else
Write0TS();
byte=byte>>1;
}
}
unsignedcharReadByteTS()//讀一個字節(jié)(byte)
{
unsignedchari,j;
bitb;
j=0;
for(i=0;i<8;i++)
{
b=ReadTS();
if(b)
j+=1;
j=_cror_(j,1);
}
returnj;
}
voidInitTS()//初始化溫度轉(zhuǎn)換
{
ResetTS();
WriteByteTS(0xCC);
WriteByteTS(0x4E);
WriteByteTS(0x64);
WriteByteTS(0x8A);
WriteByteTS(0x1F);
}
voidGetTempTS()//獲取溫度
{
ResetTS();
WriteByteTS(0xCC);
WriteByteTS(0x44);
Delay100ms();
ResetTS();
WriteByteTS(0xCC);
WriteByteTS(0xBE);
temp2=ReadByteTS();
temp1=ReadByteTS();
ReadByteTS();
ReadByteTS();
ReadByteTS();
ReadByteTS();
ReadByteTS();
ReadByteTS();
ReadByteTS();
temp1=temp1<<4;
temp1+=(temp2&0xF0)>>4;
temp2=(temp2&0x0F)?5:0;
}