PIC16F877A驱动DS18B20温度采集芯片
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 dont 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 ;
}
}
主程序
#include
#include
#include "main.h"
#include "t232.h"
#include "ds18b20.h"
const uchar rom1[8]={0x28,0x94,0xB8,0x1A,0x02,0x00,0x00,0x6E} ;
const uchar rom2[8]={0x28,0x4B,0xE6,0x1A,0x02,0x00,0x00,0xB9} ;
void init_all()
{
asm("clrwdt");
init_232() ;
}
void main()
{
const char str[]="hello world!" ;
uchar dat[16],td;
uint data[2] ;
init_all() ;
//get ID of DS18b20
//init_ds18b20() ;
//write_ds18b20(0x33) ;
//for(t=0;t<8;t++)
//{
//td=read_ds18b20() ;
//put_char(td) ;
//}
//configure_ds18b20(3) ;//configure the accuration of thermometer
//td=get_configure() ;
//put_char(td) ;
while(1)
{
start_convert() ;
data[0]=test(rom1) ;
data[1]=test(rom2) ;
DelayMs(70) ;
sprintf(dat,"temp1 %d temp2 %d",data[0],data[1]) ;
send_str(dat) ;
}
}
使用DS18B20中容易出现的问题:
1、读出的温度值不变,可能是硬件方面的问题,使用寄生电源方式很容易出现这种问题!建议使用独立电源供电,同时别忘了接4.7K上拉电阻 ;
2、DS18B20读写时序很重要,其实驱动DS18B20只需要写好三个函数就可以了,一个初始化函数,一个读字节函数,一个写字节函数,如果这两个函数验证通过了,你也就成功了一半了,DS18B20的其他功能均是封装这3个函数来实现的!只需要按照功能的使用命令即可!
PIC16F877A驱动DS18B20温度采集芯 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)
