18b20报警按键可设值1602显示51程序
#include<reg52.h>
#include<1602.h>
#include<intrins.h>
sbit ds = P2^2;
sbit beep = P2^3;
sbit rd = P3^7;
bit flag;
uchar ds_rom[8];
uchar H,L,Key_value;
void TempDelay(uchar us)
{
while(us--); //6.51us
}
void ds_reset()
{
ds=1;
_nop_();
ds=0;
TempDelay(80);//520us
ds=1;
TempDelay(5);
if(ds==0)
flag=1;
else
flag=0;
TempDelay(20);
ds=1;
}
bit ds_read_bit()
{
bit dat;
ds=0;
_nop_();
_nop_();
ds=1;
_nop_();
dat=ds;
TempDelay(12);
return dat;
}
uchar ds_read_byte()
{
uchar i,j,value;
for(i=0;i<8;i++)
{
j=ds_read_bit();
value=(j<<7)|(value>>1);
}
return value;
}
void ds_write_byte(uchar dat)
{
uchar i,onebit;
for(i=0;i<8;i++)
{
onebit=dat&0x01;
if(onebit) //д1
{
ds=0;
TempDelay(1);
ds=1;
_nop_();
}
else //д0
{
ds=0;
TempDelay(11);
ds=1;
_nop_();
}
dat>>=1;
}
}
void ds_change()
{
ds_reset();
ds_write_byte(0xcc);
ds_write_byte(0x44);
}
uchar get_temperature()
{
uint temp;
uchar a,b,c;
ds_reset();
ds_write_byte(0xcc);
ds_write_byte(0xbe);
a=ds_read_byte();
b=ds_read_byte();
temp=b;
temp<<=8;
temp|=a;
c = temp*0.0625;
return c;
}
void key()
{
Key_value = P3;
Key_value = Key_value & 0x0f;
if ( Key_value != 0x0f )
{
delay(5);
if ( Key_value != 0x0f )
{
switch(Key_value)
{
case 0x0e: H++; break;
case 0x0d: H--; break;
case 0x0b: L++; break;
case 0x07: L--; break;
}
}
}
}
void main()
{
uchar i;
rd = 0;
init_1602();
write_com( 0x80 + 0 + 3);
write_data( 'H' );
write_com( 0x80 + 0 + 8);
write_data( 'L' );
lcd_distostr(1,1,0);
H = 40;
L = 10;
while(1)
{
ds_change();
i = get_temperature();
lcd_dis( 1,14,uchartostr( i ) );
lcd_dis( 0,5,uchartostr( H ) );
lcd_dis( 0,10,uchartostr( L ) );
if ( i >= H || i <= L)
{
beep = 0;
}
else
{
beep = 1;
}
key();
}
}
|