微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > [C51代码]DS18B20驱动

[C51代码]DS18B20驱动

时间:11-10 来源:互联网 点击:

/*******************ds18b20.c**************************/
#include "Atmel/AT89X51.h"
#include "link.h"
#include "ds18b20.h"
#include "delay.h"

/**************定量定义***************/
union
{
uchar c[2];
uint x;

}temp;

uchar idata flag=0;
uint idata cc=0;
uchar idata disp[8];

/****************************************************************
*函数功能:复位DS18B20
*入口参数:无
*出口参数:presence,指示复位是否成功
****************************************************************/
uchar Reset_DS18B20(void)
{
uchar presence;
DQ = 0; //pull DQ line low
delay(248); // leave it low for 500us
DQ = 1; // allow line to return high
delay(48); // wait for presence 100us
presence = DQ; // get presence signal
delay(198); // wait for end of timeslot 400us
return(presence); // presence signal returned
} // 0=presence, 1 = no part

/****************************************************************
*函数功能:读取DS18B20的一个字节
*入口参数:无
*出口参数:value,读取的一个字节
****************************************************************/
uchar read_byte(void)
{
uchar i;
uchar value = 0;
for (i=8;i>0;i--)
{
value>>=1;
DQ = 0; // pull DQ low to start timeslot
DQ = 1; // then return high
delay(3); // 11us
if(DQ) value|=0x80;
delay(25); // wait for rest of timeslot 55us
}
return(value);
}

/****************************************************************
*函数功能:写一个字节到DS18B20
*入口参数:val,要写的一个字节
*出口参数:无
****************************************************************/
void write_byte(uchar val)
{
uchar i;
for (i=8; i>0; i--)
{
DQ = 0; // pull DQ low to start timeslot
DQ = val&0x01; // 写1时,使得15us以内拉高 整个写1时隙不低于60us,写0时保持低在60us到120us之间
delay(38); // hold value for remainder of timeslot 80us
DQ = 1;
val=val/2;
}
delay(38); //80us
}
/****************************************************************
*函数功能:读取DS18B20的温度值
*入口参数:无
*出口参数:temp.x温度值
****************************************************************/
uint Read_Temperature(void)
{ EA=0;
Reset_DS18B20();
write_byte(0xCC); // Skip ROM
write_byte(0xBE); // Read Scratch Pad
temp.c[1]=read_byte();
temp.c[0]=read_byte();
Reset_DS18B20();
write_byte(0xCC); //Skip ROM
write_byte(0x44); // Start Conversion
return temp.x;
}
/****************************************************************
*函数功能:将DS18B20的温度值送LCD显示
*入口参数:无
*出口参数:无
****************************************************************/
void DS18B20Disp(void)
{
flag=0; //温度正负标志位
cc=Read_Temperature();
if (cc>0xf800)
{
flag=1;

cc=~cc+1;
}
cc=cc*0.625;

disp[0]=0+cc/1000;
disp[1]=0+(cc/100)%10;
disp[2]=0+(cc/10)%10;
disp[3]=.; //小数点
disp[4]=0+cc%10;
disp[5]=0xdf;
disp[6]=C;
disp[7]=\0;

if(flag)
{ if (disp[1]==0)
{
disp[0]= ;
disp[1]=-;

}
else disp[0]=-;

}
else
{ if (disp[0]==0)
{
disp[0]= ;
if(disp[1]==0) disp[1]= ;
}

}
}
/* 延时函数为
void delay(unsigned char i)
{
while(--i);
}
*/

/*******************ds18b20.h**************************/
#ifndef _DS18B20_h_
#define _DS18B20_h_
#include "Atmel/AT89X51.h"
#include "link.h"
/**************引脚定义***************/
sbit DQ=P1^0; //IO

/**************变量声明***************/
extern uchar idata disp[8];

/**************函数声明***************/
extern uchar Reset_DS18B20(void);
extern uchar read_byte(void);
extern void write_byte(uchar val);
extern uint Read_Temperature(void);
extern void DS18B20Disp(void);

#endif

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top