液晶显示温度计程序
扫描二维码
随时随地手机看文章
#include
#include
sbit RST = P2^0;
sbit CLK = P2^1;
sbit DQ = P2^2;
sbit TSOR = P2^3;
sbit ALERT =P2^4;
sbit RS = P2^7;
sbit RW = P2^6;
sbit EN = P2^5;
/*------------------------------------------全局变量-------------------------------------------------------*/
statICunsigned char temp1,temp2; //温度值的整数部分、小数部分
static unsigned char pos,posset; //数字电位器电位值、设定值
static unsigned char min,sec; //分钟、秒
static unsigned char count; //Timer0中断计数
static unsigned char minset; //设定的分钟数
static unsigned char status1,status2;//状态标志
bit stop,timeover;//定时停止、结束
static char line0[] =" 00:00 ";
static char line1[] =" . C W";
/*-------------------------------------------------------------------------------------------------------------*/
void InitInterupt();
void KeyboardDelay();
/*-------------------------------------------LCD驱动函数------------------------------------------------*/
void DelayL();
void DelayS();
void WriteCommand(unsigned char c);
void WritEDAta(unsigned char c);
void ShowChar(unsigned char pos,unsigned char c);
void ShowString(unsigned char line,char *ptr);
void InitLcd();
/*----------------------------------------------键盘-程序--------------------------------------------------*/
unsigned char GetKey();
/*---------------------------------------------数字温度计驱动-------------------------------------------*/
void ChangePos(bit sel,unsigned char pos1,unsigned char pos2);
/*------------------------------------------温度传感器驱动----------------------------------------------*/
void Delay15();
void Delay60();
void Delay100ms();
void Write0TS();
void Write1TS();
bit ReaDTS();
void ResetTS();
void WriteByteTS(unsigned char byte);
unsigned char ReadByteTS();
void InitTS();
void GetTempTS();
/*-------------------------------------------------主程序---------------------------------------------------*/
void main (void) {
char code str1[] =" Hello World! ";
char code str2[] =" 2002-10-20 ";
unsigned char i;
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(); //初始化中断设置
minset=10; //缺省定时10分钟
posset=0; //缺省电位器值0
min=minset;//初始化数据
pos=posset;
sec=0;
count=0;
P1=0xF0;
status1=0;
status2=0;
stop=1;
timeover=0;
ChangePos(0,255-pos,255-pos); //设置电位器
InitTS(); //初始化温度计
while(1)//循环显示温度值
{
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;//显示小数位
if(timeover) //若定时结束,则电位器缓慢复0
{
for(;pos>0;pos--)
{
ChangePos(0,255-pos,255-pos);
_nop_();
_nop_();
}
timeover=0;
posset=0;
}
if(pos>posset) //若按键修改电位器位置
{
for(;pos>posset;pos--)//则缓变到设定值
{
ChangePos(0,255-pos,255-pos);
_nop_();
_nop_();
}
ChangePos(0,255-pos,255-pos);
}
else if(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();
}
}
void InitInterupt()
{
TMOD=0x21; //初始化中断设置
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;
}
void KeyboardDelay() //按键中断延时
{
unsigned char i,j;
i=0x40;
j=0xFF;
while(i--)
while(j--);
}
/*--------------------------------------------中断处理-----------------------------------------------------*/
Int0_process() interrupt 0 using 0
{
unsigned char key;
unsigned char keycode[]= "TP";
unsigned char step[3]={1,2,5};
EA=0;
key=GetKey(); //获得按键值
switch(key)
{
case 0:
stop=!stop;
min=minset;
sec=0;
break;
case 1:
case 2:
case 3:
if(stop)
{
minset+=step[key-1];
if(minset>60)
minset=0;
min=minset;
}
break;
case 5:
case 6:
case 7:
if(stop)
{
minset-=step[key-5];
if(minset>60)
minset=0;
min=minset;
}
break;
case 9:
case 10:
case 11:
posset+=step[key-9];
break;
case 13:
case 14:
case 15:
posset-=step[key-13];
break;
default:
break;
}
TR0=!stop;
KeyboardDelay();
P1=0xF0;
EA=1;
}