📄 ds18b20.c
字号:
/************************************************************
*文件名称: DS18B20.C
*功能描述: 18B20驱动程序,DQ为数据口,上拉4.7k电阻,接于P3.4
* 11.0592M晶振,
*************************************************************/
#include <reg51.h>
#include <intrins.h>
#include <Absacc.h>
typedef unsigned char uchar;
typedef unsigned int uint;
#define C8255_A XBYTE[0x7f00]
#define C8255_B XBYTE[0x7f01]
#define C8255_CON XBYTE[0x7f03]
sbit dq = P3^4;
bit flag;
uint Temperature;
uchar temp_buff[9]; //存储读取的字节,read scratchpad为9字节
uchar id_buff[8]; //read rom ID为8字节
uchar *p;
unsigned char code Led[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char data Dispbuff[] = {0x00,0x00,0x00};
/************************************************************
*Function:延时处理
*************************************************************/
void TempDelay (uint us)
{
while(us--);
}
/************************************************************
*Function:显示程序
************************************************************/
void Display()
{
uchar i, j=0xfe;
Dispbuff[0] = Temperature/100; // 百位
Dispbuff[1] = (Temperature%100)/10; // 十位
Dispbuff[2] = (Temperature%100)%10; // 个位
for(i=0; i<3; i++)
{
C8255_B = 0x00;
C8255_A = j;
C8255_B = Led[Dispbuff[i]];
TempDelay(60);
j = (j<<1)|(j>>7);
}
C8255_B = 0x00;
}
/************************************************************
*Function:18B20初始化
*************************************************************/
void Init18b20 (void)
{
dq=1;
_nop_();
dq=0;
TempDelay(86); //delay 530 uS
_nop_();
dq=1;
TempDelay(14); //delay 100 uS
_nop_();
_nop_();
_nop_();
if(dq==0)
flag = 1; //detect 1820 success!
else
flag = 0; //detect 1820 fail!
TempDelay(20); //20
_nop_();
_nop_();
dq = 1;
}
/************************************************************
*Function:向18B20写入一个字节
*************************************************************/
void WriteByte (uchar wr) //单字节写入
{
uchar i;
for (i=0;i<8;i++)
{
dq = 0;
_nop_();
dq=wr&0x01;
TempDelay(5); //delay 45 uS //5
_nop_();
_nop_();
dq=1;
wr >>= 1;
}
}
/************************************************************
*Function:读18B20的一个字节
*************************************************************/
uchar ReadByte (void) //读取单字节
{
uchar i,u=0;
for(i=0;i<8;i++)
{
dq = 0;
u >>= 1;
dq = 1;
if(dq==1)
u |= 0x80;
TempDelay (4);
_nop_();
}
return(u);
}
/************************************************************
*Function:读18B20 多字节读
*************************************************************/
void read_bytes (uchar j)
{
uchar i;
for(i=0;i<j;i++)
{
*p = ReadByte();
p++;
}
}
/************************************************************
*Function:读取温度
*************************************************************/
void GemTemp (void)
{
unsigned char temp1;
read_bytes (9); // 读取scratchpad中的值
temp1 = (temp_buff[0]>>4)&0x0f; // 舍去小数点
if((temp_buff[0]&0x08)==0x08) // 四舍五入
temp1 += 1;
temp1 = ((temp_buff[1]<<4)&0x70)|temp1;
Temperature = temp1;
TempDelay(1);
}
/************************************************************
*Function:内部配置
*************************************************************/
void Config18b20 (void) //重新配置报警限定值和分辨率
{
Init18b20();
WriteByte(0xcc); //skip rom
WriteByte(0x4e); //写 scratchpad, 后跟3个字节数据
WriteByte(0x1E); //上限: 30(TH)
WriteByte(0x0A); //下限: 10(TL)
WriteByte(0x7f); //设置分辨率: 12 bit
Init18b20();
WriteByte(0xcc); //skip rom
WriteByte(0x48); //保存设定值, 写EERAM
Init18b20();
WriteByte(0xcc); //skip rom
WriteByte(0xb8); //回调设定值, 读EERAM
}
/************************************************************
*Function:读18B20ID
*************************************************************/
void ReadID (void)//读取器件 id
{
Init18b20();
WriteByte(0x33); //read rom
read_bytes(8);
}
/************************************************************
*Function:18B20 测温处理
*************************************************************/
void TemperatuerResult(void)
{
Init18b20 ();
WriteByte(0xcc); //skip rom
WriteByte(0x44); //温度转换指令
TempDelay(300);
Init18b20 ();
WriteByte(0xcc); //skip rom
WriteByte(0xbe); //读取温度指令, 即读 scratchpad
p = temp_buff;
GemTemp();
}
void main(void)
{
p = id_buff;
ReadID();
Config18b20();
C8255_CON = 0x81; // 初始化8255
Display();
while(1)
{
TemperatuerResult(); // 测温
Display(); // 显示
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -