#include "reg51.h"
#include "math.h"
#include "INTRINS.h"
#define uchar unsigned char
#define uint unsigned int
sbit rs=P3^5;
sbit lcden=P3^4;
sbit DQ = P1^0;
//sbit up = P0^1;
//sbit down = P0^2;
sbit condition = P0^0;
//unsigned char musicNum;
sbit ring = P0^3;
uchar flag_get,num,tflag; //tflag温度正负标识
uint temp;
int top = 70;
int bottom = -20;
unsigned char code str1[]={"temper top bot"};
uchar bai,shi,ge,xiao,flagdat,tflagdat,bflagdat;
uchar TempH,TempL;
void delay(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=10;b>0;b--);
}
void delay_9us(uint i) //延时9us
{
while(i--);
}
void delay_5us(uchar i)
{
while(i--);
}
void delay_1ms(uint count)
{
uint i,j;
for(i = 0;i<count;i++)
{
for(j = 0;j<120;j++);
}
}
void write_com(uchar com)//根据时序图写出来的写指令函数,字符形式
{
P2=com;
rs=0;
lcden=0;
delay(10);
lcden=1;
delay(10);
lcden=0;
}
void write_date(uchar date)//根据时序图写出来的写数据函数,字符形式,也可以用16进制给指令指针
{
P2=date;
rs=1;
lcden=0;
delay(10);
lcden=1;
delay(10);
lcden=0;
}
void display(unsigned char *p)//显示字符串//
{
while(*p!='\0')
{
write_date(*p);
p++;
delay_1ms(1);
}
}
void init() //1602初始化
{
write_com(0x38); //设置16×2显示,5×7点阵,8位数据接口
delay(10);
write_com(0x0c); //开显示不显示光标
delay(10);
write_com(0x06); //读写一字符后地址指针加1,光标加1,整屏显示不移动
delay(10);
write_com(0x01); //清屏
delay(10);
}
void Init_DS18B20(void)
{
unsigned char x = 0;
DQ = 1;//DQ复位
delay_9us(8); //稍作延迟
DQ = 0;
delay_9us(80); //精确延时约480us
DQ = 1;
delay(40);//等待DQ返回0
x = DQ;//稍作延迟后 如果x = 0初始化成功,x = 1则初始化失败
delay_9us(50);
DQ = 1 ;
_nop_();
_nop_();
}
unsigned char ReadOneChar(void)
{
unsigned char i = 0;
unsigned char dat = 0;
for(i = 8;i>0;i--)
{
DQ = 0; //给脉冲信号
dat = dat>>1;
DQ = 1; //给脉冲信号
_nop_();
_nop_();
_nop_();
if(DQ)
dat |= 0x80;
delay_9us(6);
}
return(dat);
}
void WriteOneChar(unsigned char dat)
{
unsigned char i = 0;
for(i = 8;i>0;i--)
{
DQ = 0;
_nop_();
_nop_();
DQ = dat&0x01;
delay_9us(6);
DQ = 1;
dat = dat>>1;
}
delay(5);
}
unsigned int ReadTemperature(void)
{
uchar a = 0;
uint b = 0;
uint t =0;
Init_DS18B20();
WriteOneChar(0xcc);//跳过读序号列号操作
WriteOneChar(0x44);//启动温度转换
delay_1ms(750);
Init_DS18B20();
WriteOneChar(0xcc);//跳过读序号列号操作
WriteOneChar(0xbe);//启动温度转换
a = ReadOneChar();
b = ReadOneChar();
b = b<<8;
t = a+b;
return(t);
}
void display_tem()
{
temp = ReadTemperature();
if(temp>=0x0fff)
{
temp = ~temp;
temp += 1;
tflag = 1;
}
else
{
tflag = 0;
}
TempH = temp>>4;
TempL = temp&0x0f;
TempL = TempL*0.625+0.375;
if(bai == 0x30)
{
bai = 0x20;
}
bai = TempH/100+0x30;
shi = (TempH%100)/10+0x30;
ge = (TempH%100)%10+0x30;
xiao = TempL+0x30;
if(tflag == 0)
{
flagdat = 0x20;//正温度不显示负号
}
else
{
flagdat = 0x2d;
}
if(bai == 0x30)
{
bai = 0x20;
if(shi == 0x30)
{
shi = 0x20;
}
}
write_com(0x80+0x40); //将程序指针放到第二行,在第二行写内容
write_date(flagdat);
write_date(bai);
write_date(shi);
write_date(ge);
write_date('.');
write_date(xiao);
}
void display_top()
{
uchar topflag,tshi,tge;
uint to;
if(top <= 0)
{
to = 0 - top;
topflag = 1;
}
else
{
topflag = 0;
to = top;
}
if(topflag == 0)
{
tflagdat = 0x20;//正温度不显示负号
}
else
{
tflagdat = 0x2d;
}
tshi = to/10+0x30;
tge = to%10+0x30;
write_com(0x80+0x47); //将程序指针放到第二行,在第二行写内容
write_date(tflagdat);
write_date(tshi);
write_date(tge);
}
void display_bottom()
{
uint bot;
uchar bflag,bshi,bge;
if(bottom <= 0)
{
bot = 0 - bottom;
bflag = 1;
}
else
{
bflag = 0;
bot = bottom;
}
if(bflag == 0)
{
bflagdat = 0x20;//正温度不显示负号
}
else
{
bflagdat = 0x2d;
}
bshi = bot/10+0x30;
bge = bot%10+0x30;
write_com(0x80+0x4b); //将程序指针放到第二行,在第二行写内容
write_date(bflagdat);
write_date(bshi);
write_date(bge);
}
void main()
{
IT0=1; //下降沿触发
IT1=1; //下降沿触发
PX0=1;
EA=1;
EX1=1;
EX0=1;
init();
display(str1);
while(1)
{
display_tem();
display_top();
display_bottom();
if(tflag == 1)
{
if(bottom < 0&&top < 0)
{
if(TempH>(0-bottom)|| TempH<(0-top))
{
ring = 0;
}
else
{
ring = 1;
}
}
else if(bottom < 0&&top >= 0)
{
if(TempH<(0-bottom))
{
ring = 0;
}
}
else
{
ring = 0;
}
}
else
{
if(top > 0)
{
if(TempH>top||TempH<bottom)
{
ring = 0;
}
else
{
ring = 1;
}
}
else
{
ring = 0;
}
}
}
}
void counter0(void) interrupt 0
{
if(condition == 1)
{
top++;
}
else
{
bottom++;
}
}
void counter1(void) interrupt 2
{
if(condition == 1)
{
top--;
}
else
{
bottom--;
}
}