📄 ds18b20.h
字号:
/**************************************************
*Progect: Temperature measure with DS18B20
*/
#include <iom16v.h>
#include <macros.h>
#include "def.h"
void Delay_us(uint16_t us);
uint8_t Init_18b20(void);
uint8_t Read_1_byte(void);
void Write_1_byte(uint8_t cmd);
void Write_1(void);
void Write_0(void);
void Temperature(uint8_t Th,uint8_t Tl);
void Temp_display(uint8_t Tl,uint8_t Th);
/*************************************************/
void Temp_display(uint8_t Tl,uint8_t Th)
{
uint8_t temp_h,temp_l,temp_num,temp_decimal;
temp_decimal=Tl;
temp_h=Th<<4;
temp_l=Tl>>4;
temp_num=(temp_h|temp_l);
temp_decimal&=0x0f;
temp_h=(temp_num/10);
temp_l=(temp_num%10);
LCD_write_byte(0,temp_h+48);
LCD_write_byte(0,temp_l+48);
switch(temp_decimal)
{
case 0 : LCD_write_string(12,2,".00");break;
case 1 : LCD_write_string(12,2,".06");break;
case 2 : LCD_write_string(12,2,".12");break;
case 3 : LCD_write_string(12,2,".18");break;
case 4 : LCD_write_string(12,2,".25");break;
case 5 : LCD_write_string(12,2,".31");break;
case 6 : LCD_write_string(12,2,".37");break;
case 7 : LCD_write_string(12,2,".43");break;
case 8 : LCD_write_string(12,2,".50");break;
case 9 : LCD_write_string(12,2,".56");break;
case 10 : LCD_write_string(12,2,".61");break;
case 11 : LCD_write_string(12,2,".68");break;
case 12 : LCD_write_string(12,2,".75");break;
case 13 : LCD_write_string(12,2,".81");break;
case 14 : LCD_write_string(12,2,".87");break;
case 15 : LCD_write_string(12,2,".93");break;
}
}
/************************************************
************************************************/
void Temperature(uint8_t Th,uint8_t Tl)
{
uint16_t temp_negative,th_h,tl_l;
if((Th&0x80)==0)//温度为正
{
LCD_set_xy(9,2);
Positive();
Temp_display(Tl,Th);
}
else //温度为负
{
LCD_set_xy(9,2);
Negative();
th_h=(uint16_t)Th;
tl_l=(uint16_t)Tl;
th_h<<=8;
temp_negative=(th_h|tl_l);
temp_negative=~temp_negative+1;
Tl=(uint8_t)temp_negative;
temp_negative>>=8;
Th=(uint8_t)temp_negative;
Temp_display(Tl,Th);
}
}
/*************************************************
函 数 名:Delay_us
功 能:软件延时1.1us
入口参数:us 延时时间
出口参数:无
修改时间:2007.02.16
*************************************************/
void Delay_us(uint16_t us)
{
while(us>1)
{
us--;
}
}
/*************************************************
函 数 名:Reset_18b20
功 能:复位DS18B20
入口参数:无
出口参数:exist : exist=1,存在;exist=0,不存在
修改日期:2007.02.16
*************************************************/
uint8_t Init_18b20(void)
{
uint8_t exist=1;
Set_low(); //设置成输出模式
Delay_us(600); //复位低电平480us~960us
Set_high(); //拉高电平
Read(); //设置成输入模式
Delay_us(100); //延时100us
exist=~(DS18B20_PIN&DS18B20_IO); //读端口电平
Delay_us(480); //延时480us
DDRA|=0X10; //设置成输出模式
return (exist); //返回exist的值
}
/*************************************************
函 数 名:Read_1_byte
功 能:从DS18B20读取一个字节的数据
入口参数:无
出口参数:r_data
修改日期:2007.02.16
*************************************************/
uint8_t Read_1_byte(void)
{
uint8_t i,r_data=0x00,temp;
for(i=0;i<8;i++)
{
Set_low();
Delay_us(3);
Read();
Delay_us(8);
temp=(DS18B20_PIN&DS18B20_IO);
if(temp)
{
r_data|=(1<<i);
}
else
{
r_data&=~(1<<i);
}
Delay_us(50);
Set_high();
}
return (r_data);
}
/*************************************************
函 数 名:Write_1_byte
功 能:写一个字节到DS18B20
入口参数:cmd
出口参数:无
修改日期:2007.02.16
*************************************************/
void Write_1_byte(uint8_t cmd)
{
uint8_t i,temp;
for(i=0;i<8;i++)
{
temp=(cmd>>i);
temp&=0x01;
if(temp)
{
Write_1();
}
else
{
Write_0();
}
}
}
/************************************************
函 数 名:Write_1
功 能:写一个Bit到DS18B20
入口参数:无
出口参数:无
修改日期:2007.02.16
************************************************/
void Write_1(void)
{
Set_low(); //设置成输出模式
Delay_us(4);
Set_high();
Delay_us(76);
}
/**********************************************
函 数 名:Write_0
功 能:写一个Bit到DS18B20
入口参数:无
出口参数:无
修改日期:2007.02.16
**********************************************/
void Write_0(void)
{
Set_low();
Delay_us(80);
Set_high();
}
/*********************************************
*********************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -