1.主代码:
/* 温度传感器 */
#include "DS18B20.h"
#include"def.h"
u16 get_temp (void)
{
float tt;
u16 temp;
u8 a,b;
ds_delay(40);
dsic_init();
write_byte(0xcc);
write_byte(0x44);
ds_delay(40);
dsic_init();
write_byte(0xcc);
write_byte(0xbe);
ds_delay(40);
a = read_byte();
b = read_byte();
temp = b;
temp <<= 8;
temp = temp|a;
tt = temp*0.0625;
temp = tt*10+0.5;
return temp;
}
void ds_delay(u16 i)
{
while(--i);
}
void dsic_init (void)
{
ds_dat = 1;
ds_delay (2);
ds_dat = 0;
ds_delay (250);
ds_dat = 1;
ds_delay (100);
}
u8 read_byte (void)
{
u8 i = 0;
u8 dat = 0;
for(i=8;i>0;i--)
{
ds_dat = 0;
_nop_();
dat >>= 1;
ds_dat = 1;
ds_delay(2);
if(ds_dat)
{
dat |= 0x80;
}
ds_delay(11);
}
return (dat);
}
void write_byte(u8 dat)
{
u8 i = 0;
for(i=8;i>0;i--)
{
ds_dat = 0;
_nop_();
ds_dat = dat&0x01;
ds_delay(11);
ds_dat = 1;
dat >>= 1;
ds_delay(2);
}
}
#include <reg52.h>
#include"lcd1602.h"
#include"DS18B20.h"
#include"def.h"
void main(void)
{
float temperature ;
lcd_init();
while(1)
{
temperature = get_temp ();
dispy1(temperature);
}
}
2.效果演示: