**********************************/
void WriteOneChar(unsigned char dat)//写一个字节
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
Delay(2);
DQ = 1;
dat>>=1;
}
}
/*****************************************************************************/
void ReadTemperature(void)//读取温度和报警温度上下限TH、TL值
{
unsigned char a=0;
unsigned char b=0;
unsigned char Data_L=0; //用来存放小数部分的温度值
unsigned char num=0;
unsigned int temp=0; //临时存放负数温度值
Init_DS18B20();
Delay(20);
WriteOneChar(0xCC); // 跳过读序号列号的操作
Delay(20);
WriteOneChar(0x44); // 启动温度转换
Delay(20);
Init_DS18B20();
Delay(20);
WriteOneChar(0xCC); //跳过读序号列号的操作
Delay(20);
WriteOneChar(0xBE); //读取温度寄存器
Delay(20);
a = ReadOneChar(); //读低8位
b = ReadOneChar(); //读高8位
Delay(20);
tx[4]= ReadOneChar(); //报警温度TH
tx[5]= ReadOneChar(); //报警温度TL
temp = b*256+a;
temp = temp * 0.625 + 0.5; //0.0625扩大10倍显示1位小数,若显示两位小数则扩大100倍为6.25
tx[2]= temp /1000; //温度的百位
tx[1]=temp %1000/100 ; //温度的十位
tx[0]=temp %100/10; //温度的个位
//读取温度正负值
if((b&0xf8)!=0x00){
tx[6]=1; //温度值是负数的标志位
//temp为补码,需要减一求反得原码
temp = ( b * 256 ) + a;
temp = ~temp + 1;
tx[1] = temp % 1000 / 100; //温度的十位
tx[0] = temp % 100 / 10; //温度的个位
}
tx[6]=0;
Data_L=a&0X0F;
tx[3]=table1[Data_L];
}
/*****************************************************************************/
void Display_SMG(void)
{
unsigned char a;
for(a=0;a<=50;a++)
{ //如果得到的温度是负数就显示符号
if(tx[6]==1){
P0=0xbf;
led1 = 0;
Delaynms(4);
led1 = 1; }
if(tx[2]>0){
P0=table[tx[2]];
led1 = 0;
Delaynms(4);
led1 = 1; }
P0=table[tx[1]];
led2 = 0;
Delaynms(4);
led2 = 1;
P0=(table[tx[0]])&0x7f;
led3 = 0;
Delaynms(4);
led3 = 1;
P0=table[tx[3]];
led4 = 0;
Delaynms(4);
led4 = 1;
}
}
/*****************************************************************************/
void beep()
{
unsigned char i=0;
if(((tx[1]*10+tx[0])>(tx[4])||(((tx[1]*10+tx[0])==(tx[4]))&&(tx[3]>tx[7]-1)))&&(tx[9]==5))
{
P1=_cror_(P1,1);
WAV=~WAV;
Delaynms(35); } if((tx[6]==1)&&((((tx[1]*10+tx[0])>(tx[5]))||(((tx[1]*10+tx[0])==(tx[5]))&&(tx[3]>tx[8]-1)))&&(tx[9]==5)))
{ P1=_crol_(P1,1); WAV=~WAV;Delaynms(35); } }
/*****************************************************************************/
void main(void)
{
TMOD=0x11; //T1,T0均工作于方式一
EA=1; //开总中断
ET0=1; //开定时器T0中断
ET1=1; //开定时器T1中断
TH1=(65536-20000)/256; //显示温度
TL1=(65536-20000)%256;
TH0=(65536-500)/256;
TL0=(65536-500)%256;
TR1=1; //启动定时器T1
TR0=1; //禁止定时器T0
P1=0x7f;
while(1)
{ WAV=1; }
}