#include "ds18b20.h"
const char crc_code[256]={
0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,
17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,
175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238,
50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53
};
uchar init_ds18b20()
{
uchar temp=0 ;
DoutDir=0 ;
Dout=1 ;
DelayUs(1) ;
Dout=0 ;
DelayUs(600) ;
Dout=1 ;
DoutDir=1 ;
while(Dout) ;
while(!Dout) ///这里使用了超时设置
{
DelayUs(24)
if(temp==10)
{
if(Dout)
{
DoutDir=0 ;
return 1 ;
}
else
{
return 0 ;
}
}
temp++ ;
}
DoutDir=0 ;
return 1 ;
}
uchar read_ds18b20()
{
uchar temp,i ;
temp= 0 ;
DoutDir=0 ;
for(i=0;i<8;i++)
{
Dout=1 ;
DelayUs(1) ;
temp=temp>>1 ;
Dout=0 ;
DelayUs(1) ;
Dout=1 ;
DoutDir=1 ;
DelayUs(8) ;
if(Dout)
temp=temp|0x80 ;
DelayUs(60) ;
DoutDir=0 ;
}
Dout=1 ;
return temp ;
}
void write_ds18b20(uchar data)
{
uchar temp ,i ;
DoutDir=0 ;
for(i=0;i<8;i++)
{
temp=data&0x01 ;
if(temp)
{
Dout=0 ;
DelayUs(2) ;
Dout=1 ;
DelayUs(57) ;
}
else
{
Dout=0 ;
DelayUs(57) ;
Dout=1 ;
DelayUs(2) ;
}
data=data>>1 ;
}
Dout=1 ;
}
uint get_temp()
{
uchar th,tl ;
uint temp ;
th=0 ;
tl=0 ;
init_ds18b20() ;
write_ds18b20(ROM_SKIP) ;
write_ds18b20(MEM_CONVERT) ;
init_ds18b20() ;
write_ds18b20(ROM_SKIP) ;
write_ds18b20(MEM_READ) ;
tl=read_ds18b20() ;
th=read_ds18b20() ;
if(!(th&0xf0))
{
th=th&07 ;
temp=th*256+tl ;
return temp ;
}
else
{ th=th&0x07 ;
temp=th*256 +tl ;
return temp ;
}
}
void get_sequence(uchar *seq)
{
uchar i ;
uchar *temp ;
temp=seq ;
init_ds18b20() ;
write_ds18b20(ROM_READ) ;
for(i=0;i<8;i++)
{
*temp++=read_ds18b20() ;
}
return ;
}
char get_configure()
{
char temp,i ;
i=0 ;
init_ds18b20() ;
write_ds18b20(ROM_SKIP) ;
write_ds18b20(MEM_READ) ;
temp=0 ;
while(++i)
{
temp=read_ds18b20() ;
if(i==5)
return temp ;
}
}
float process_temp(uint data)
{
return data*0.0625 ;
}
void configure_ds18b20(uchar flag)
{
uchar tmp1,tmp2 ;
init_ds18b20() ;
write_ds18b20(ROM_SKIP) ;
write_ds18b20(MEM_READ) ;
tmp1=read_ds18b20() ;
tmp2=read_ds18b20() ;
tmp1=read_ds18b20() ;//get TH
tmp2=read_ds18b20() ;//get Tl
init_ds18b20() ;// reset bus
write_ds18b20(ROM_SKIP) ;//skip rom
write_ds18b20(MEM_WRITE) ;//write command
write_ds18b20(tmp1) ;//
write_ds18b20(tmp2) ;//
switch(flag)
{
case 0 :
write_ds18b20(0x1F) ;
break ;
case 1 :
write_ds18b20(0x3F) ;
break ;
case 2 :
write_ds18b20(0x5F) ;
break ;
case 3 :
write_ds18b20(0x7F) ;
break ;
default :
write_ds18b20(0x1F) ;
break ;
}
init_ds18b20() ;//reset bus
write_ds18b20(ROM_SKIP) ;//skip rom
write_ds18b20(MEM_COPY) ;//copy ram to eeprom
write_ds18b20(MEM_RECALL) ;//recopy eeprom to ram
}
uchar crc_check(uchar *data)
{
uchar temp ,i;
uchar *p=data ;
//complement ;
temp=0 ;
for(i=0;i<7;i++)
{
temp=crc_code[temp^*p] ;
p++ ;
}
if(temp==data[7])
return 1 ;
else
return 0 ;
}
void start_convert()
{
init_ds18b20() ;
write_ds18b20(ROM_SKIP) ;
write_ds18b20(MEM_CONVERT) ;
}
uint read_temp()
{
uchar tl,th ;
uint temp ;
tl=0 ;
th=0 ;
// init_ds18b20() ;
// write_ds18b20(0xcc) ;//note that don't skip rom command here ;
write_ds18b20(MEM_READ) ;
tl=read_ds18b20() ;
th=read_ds18b20() ;
if(!(th&0xf0))
{
th=th&07 ;
temp=th*256+tl ;
return temp ;
}
else
{ th=th&0x07 ;
temp=th*256 +tl ;
return temp ;
}
}
void match_rom(uchar data[8])
{
//uchar *temp=data ;
uchar i ;
init_ds18b20() ;
write_ds18b20(ROM_MATCH) ;
for(i=0;i<8;i++)
write_ds18b20(data[i]) ;
}
uint test(const uchar *data)//注意这里一定要用const关键字
{
uint temp ;
uchar t,tl,th;
tl=0 ;
th=0 ;
// start_convert() ;
init_ds18b20() ;
write_ds18b20(ROM_MATCH) ;
for(t=0;t<8;t++)
{
write_ds18b20(*data++) ;
}
write_ds18b20(MEM_READ) ;
tl=read_ds18b20() ;
th=read_ds18b20() ;
if(!(th&0xf0))
{
th=th&07 ;
temp=th*256+tl ;
return temp ;
}
else
{ th=th&0x07 ;
temp=th*256 +tl ;
return temp ;
}
}
DS18B20组网程序
需积分: 10 148 浏览量
2009-08-16
09:14:17
上传
评论
收藏 81KB RAR 举报
好清闲
- 粉丝: 319
- 资源: 20
最新资源
- MyBatis 动态 SQL:灵活而强大的查询构建器.pdf
- com.accordion.prettyo.apk
- 毕业设计:基于SSM的mysql-ssm软件bug管理系统(源码 + 数据库 + 说明文档)
- MTSQL8.0.35windows(64bit)-mysql-installer-community-8.0.35.0
- 人工智能引领音乐创作新时代之Suno AI
- Public-bicycle-usage-forecast-master.zip
- 通道处理过程模拟:从理论到实践.pdf
- 数据库第七次作业E-R图第一题
- 大厂面试真题Java语法基础面试专题及答案
- IMG20240428211124.jpg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈