#include<AT89X51.H>
#include<ABSACC.H>
#include<STDIO.H>
#include<MATH.H>
#include<FLOAT.H>
//#includ<Delay.h>
typedef unsigned char uchar;
typedef unsigned int uint;
typedef signed char schar;
typedef signed int sint;
#define DQ P3_7//温度检测P3.7
#define KEY_SET 14
#define KEY_COMFIRM 15
extern void DelayXms(uint xms);
extern uchar KeyScan();
uchar code dis_code[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
uint timecnt=0,step=1,stepcnt=0;
uchar temptable[11],position=0;
schar temp=0;//延时函数
void DelayXus(uint i)
{
while(i--);
}
//初始化函数?
void InitlalDS18B20()
{ uchar x=0;
DQ=1;//DQ复位
DelayXus(8);//稍做延时
DQ=0;//单片机将DQ拉低
DelayXus(80);// 精确延时大于480us
DQ=1;//拉高总线
DelayXus(14);
x=DQ;//稍做延时后如果x=0则初始化成功x=1则初始化失?
DelayXus(20);
}//读一个字节
uchar ReadByte()
{
uchar i=0,dat=0;
for(i=8;i>0;i--)
{
DQ=0;//给脉冲信号
dat>>=1;
DQ=1;//给脉冲信号
if(DQ)
dat|=0x80;
DelayXus(4);
}
return dat;
}//写一个字?
void WriteByte(uchar dat)
{uchar i=0;
for(i=8;i>0;i--)
{
DQ=0;
DQ=dat&0x01;
DelayXus(5);
DQ=1;
dat>>=1;
}//Delayxus(4)
}
//读取温度
uint ReadTemperature()
{ uchar a=0;
uchar b=0;
uint t=0;
float tt=0;
InitialDS18B20();
WriteByte(0xcc);//跳过度序号列的操作
WriteByte(0x44);//启动温度转换
InitialDS18B20();
WriteByte(0xcc);//跳过度序号列的操作
WriteByte(0xbe);//读取温度寄存器等,共可读9个寄存器,前两个就是温度
a=ReadByte();
b=ReadByte();
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;//5/8
//t=tt*10+0.5;//放大10倍输出并四舍五入
return t;
}
//ledplay 数码管显示,chan0-8,num数值0-9、符号*/
void ledplay(uchar chan,uchar num)
{if(chan<8&&num<=10)
{
P2=0xff;
P2=(~(0x01<<chan));//选取第N位
P0=dis_code[num];//显示
DelayXms(100);//delay();
}
}
void InitTimer1(void)
{
TMOD=0x10;
TH1=0xfc;
TL1=0x18;
EA=1;
ET1=1;
TR1=1;
}
/*
/*tempdisplay温度显示*/
void tempdisplay(schar tem)
{schar tempbit1=0;
schar tempbit2=0;
//if((tem<=-40||(tem>=125))return;
if(tem<0)
{ledplay(5,10);tem=256-tem;
}
if(tem>99)
{
{ledplay(5,1);tem=tem-100;
}
tempbit1=tem%10;//各位HexToBcd
tempbit2=tem/10;//十位
ledplay(6,tempbit2);
ledplay(7,tempbit1);
}}
//Timer1Interrupt定时器1000us*1000=1s
// void Timer1 Interrupt(void)interrupt3
void timer1() interrupt 3
{TH1=0xfc;
TL1=0x18;
timecnt++;
if(timecnt>=1000)//1s温度值存放
{timecnt=0;
stepcnt++;
if(stepcnt>=step)
{stepcnt=0;
if(position<10)temptable[position++]=temp;
else
{temptable[position]=temp;
position=0;
}
}
}
}
uint setStep()
{uchar key,i=0;
uint step=0;
P2=0xff;//清屏
for(;;)
{key=KeyScan();
if(key==KEY_COMFIRM)return step;
if(key<10)
{step=key;
for(;;)
{ key=KeyScan();
ledplay(7,step);
if(key==KEY_COMFIRM)return step;
if(key<10)
{step=step*10+key;
for(;;)
{key=keyScan();
if(key++KEY_COMFIRM)return step;
ledplay(6,step/10);
ledplay(7,step%10);
if(key<10)
{step=step*10+key;
for(;;)
{key=KeyScan();
if(key==KEY_COMFIRM)return step;
ledplay(5,step/100);
ledplay(6,(step%100)/10);
ledplay(7,step%10);
if(key<10)
{step=step*10+key;
for(;;)
{key=KeyScan();
if(key++KEY_COMFIRM)return step;
ledplay(4,step/1000);
ledplay(5,(step%1000)/100);
ledplay(6,(step%100)/10);
ledplay(7,step%10);
}
}
}
}
}
}
}
}
}
}
void main()
{uint i=0,j;
InitTimer 1();
for(;;)
{uchar ker=0xff,dis;
i=ReadTemperature();//读温度
if(i!=0xffff)temp=i>>4;
tempdisplay(temp);//主循环显示温度
key=KeYscan();//检查按键
ledplay(0,key);
if(key==KEY_SET)//进入设置状态
{if(0!=(j=setStep()))
step=j;
}
if(key<10)//如果有0-9按键按下,
{if(key==0)key=10;//按下0,显示10秒?
dis=((position>=key)?(position-key):(11+position-key));//找到前N秒的温度值存放占位置
for(j=0;j<20;j++)//显示之前的温度,并多次刷新、使之前显示一段时间
{if(key==10)
{ledplay(0,1);ledplay(1,0);}//显示10s
else ledplay(0,key);//显示XS
tempdisplay(temptable[dis]);//显示存储温度
}
}
}
}
void DelayXms(uint xms)
{uint i;
uchar j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}uchar KeyScan()
{uchar cord,keyValue;
cord=0xff;
P1=0xfe;
if((P1&0xf0)!=0xf0)
{DelayXms(5);
if((P1&0xf0)!=0xf0)//消除键盘抖动延时10ms
{cord=P1;
}
}
P1=0xff;
P1_2=0;
if((P1&0xf0)!=0xf0)
{DelayXms(5);if(P1&0xf0)!=0xf0//消除键盘抖动延时10ms
{cord=P1;
}
}
P1=0xf0;
while(P1!=0xf0);//等待按键释?
?/com=com1+com2;
switch(cord)
{case 0xee:keyValue=7;break;//7P1.0-P1.3为列,P1.4-1.7为行
case 0xed:keyValue=8;break;//8
case 0xeb:keyValue=9;break;//9
case 0xe7:keyValue=11;break;//%
case 0xde:keyValue=4;break;//4
case 0xdd:keyValue=5;break;//5
case 0xdb:keyValue=6;break;//6
case 0xd7:keyValue=12;break;//X
case 0xbe:keyValue=1;break;//1
case 0xbd:keyValue=2;break;//2
case 0xbb:keyValue=3;break;//3
case 0xb7:keyValue=13;break;//-
case 0x7e:keyValue=14;break;//ON/C
case 0x7d:keyValue=0;break;//0
case 0x7b:keyValue=15;break;//=
case 0x77:keyValue=16;break;//+
default:keyValue=0xff;
}
return keyValue;
} |