#include <reg52.h>
#include <stdio.h>
#include <intrins.h>
#include "1602.h"
#include "./delay/delay.h"
sbit ds = P1^0;
//sbit LED = P1^1;
bit ack = 0; //应答信号
#define SUCC 1 //应答信号
#define ERR 0
void ds18b20_init()//初始化单总线信号时序图
{
ds = 0;
delay_us(200);
delay_us(200);
ds = 1;
delay_us(30);
if (ds == 0)
{
ack = SUCC;
}
else
{
ack = ERR;
}
delay_us(200);//区分位写入时序
}
void ds18b20_write_byte(unsigned char byte)//位写入时序
{
unsigned char i ;
for(i = 0;i <8;i++)
{
ds = 0;
_nop_();
_nop_();
ds = byte & 0x01;
byte >>= 1;
delay_us(30);
ds = 1;
}
delay_us(30);
}
bit ds_read_bit()//位读取时序
{
bit tmp;
ds = 1;
ds = 0;
_nop_();
_nop_();
ds = 1;
tmp = ds;
delay_us(30);
return tmp;
}
unsigned char ds_read_byte()//读取一个字节
{
unsigned char i,j,k;
for(i = 0;i < 8;i++)
{
j = ds_read_bit();
k = (j << 7)| (k >> 1);//j = j << 7; k = k >> 1; k = k+j;
}
return k;
}
void main()
{
unsigned char a;
unsigned int temp,b;
unsigned char dis_buf[20];
float temp_f;
// ds18b20_init();
LCD_Init();
LCD_Clear();
while(1)
{
ds18b20_init();
ds18b20_write_byte(0xcc);//跳过读ROM命令
ds18b20_write_byte(0x44);//打开温度转换
DelayMs(20);
ds18b20_init();
ds18b20_write_byte(0xcc);
ds18b20_write_byte(0xbe);//读寄存器
a = ds_read_byte();
b = ds_read_byte();
temp = (b << 8)+a;
temp_f = (float)temp*0.0625;
sprintf(dis_buf,"temp is % 7.3f",temp_f);
LCD_Write_String(0,0,dis_buf);
}
}