现在的位置: 首页 > 综合 > 正文

PIC16F877A驱动DS18B20温度采集芯片

2018年03月16日 ⁄ 综合 ⁄ 共 8417字 ⁄ 字号 评论关闭

天开始驱动DS18B20温度采集芯片!

从网上收集的资料

DS18B20的内部结构

    DS18B20内部结构主要由四部分组成:64位光刻ROM、温度传感器、非挥发的温度报警触发器TH和TL、配置寄存器。DS18B20的管脚排列如下:

    

    DQ为数字信号输入/输出端;GND为电源地;VDD为外接供电电源输入端(在寄生电源接线方式时接地)。

    光刻ROM中的64位序列号是出厂前被光刻好的,它可以看作是该DS18B20的地址序列码。64位光刻ROM的排列是:开始8位(28H)是产品类型标号,接着的48位是该DS18B20自身的序列号,最后8位是前面56位的循环冗余校验码(CRC=X8+X5+X4+1)。光刻ROM的作用是使每一个DS18B20都各不相同,这样就可以实现一根总线上挂接多个DS18B20的目的。

    DS18B20中的温度传感器可完成对温度的测量,以12位转化为例:用16位符号扩展的二进制补码读数形式提供,以0.0625℃/LSB形式表达,其中S为符号位。

 

   这是12位转化后得到的12位数据,存储在18B20的两个8比特的RAM中,二进制中的前面5位是符号位,如果测得的温度大于0,这5位为0,只要将测到的数值乘于0.0625即可得到实际温度;如果温度小于0,这5位为1,测到的数值需要取反加1再乘于0.0625即可得到实际温度。

    例如+125℃的数字输出为07D0H,+25.0625℃的数字输出为0191H,-25.0625℃的数字输出为FF6FH,-55℃的数字输出为FC90H。

 

DS18B20温度传感器的存储器

     DS18B20温度传感器的内部存储器包括一个高速暂存RAM和一个非易失性的可电擦除的E2RAM,后者存放高温度和低温度触发器TH、TL和结构寄存器。

    暂存存储器包含了8个连续字节,前两个字节是测得的温度信息,第一个字节的内容是温度的低八位,第二个字节是温度的高八位。第三个和第四个字节是TH、TL的易失性拷贝,第五个字节是结构寄存器的易失性拷贝,这三个字节的内容在每一次上电复位时被刷新。第六、七、八个字节用于内部计算。第九个字节是冗余检验字节。

 

该字节各位的意义如下:

TM R1 R0 1 1 1 1 1

低五位一直都是1 ,TM是测试模式位,用于设置DS18B20在工作模式还是在测试模式。在DS18B20出厂时该位被设置为0,用户不要去改动。R1和R0用来设置分辨率,如下表所示:(DS18B20出厂时被设置为12位)

分辨率设置表:

R1
R0
分辨率
温度最大转换时间
0
0
9位
93.75ms
0
1
10位
187.5ms
1
0
11位
375ms
1
1
12位
750ms

    根据DS18B20的通讯协议,主机控制DS18B20完成温度转换必须经过三个步骤:每一次读写之前都要对DS18B20进行复位,复位成功后发送一条ROM指令,最后发送RAM指令,这样才能对DS18B20进行预定的操作。复位要求主CPU将数据线下拉500微秒,然后释放,DS18B20收到信号后等待16~60微秒左右,后发出60~240微秒的存在低脉冲,主CPU收到此信号表示复位成功。

 

DS1820使用中注意事项

  DS1820虽然具有测温系统简单、测温精度高、连接方便、占用口线少等优点,但在实际应用中也应注意以下几方面的问题:

  (1)较小的硬件开销需要相对复杂的软件进行补偿,由于DS1820与微处理器间采用串行数据传送,因此,在对DS1820进行读写编程时,必须严格的保证读写时序,否则将无法读取测温结果。在使用PL/M、C等高级语言进行系统程序设计时,对DS1820操作部分最好采用汇编语言实现。

  (2)在DS1820的有关资料中均未提及单总线上所挂DS1820数量问题,容易使人误认为可以挂任意多个DS1820,在实际应用中并非如此。当单总线上所挂DS1820超过8个时,就需要解决微处理器的总线驱动问题,这一点在进行多点测温系统设计时要加以注意。

  (3)连接DS1820的总线电缆是有长度限制的。试验中,当采用普通信号电缆传输长度超过50m时,读取的测温数据将发生错误。当将总线电缆改为双绞线带屏蔽电缆时,正常通讯距离可达150m,当采用每米绞合次数更多的双绞线带屏蔽电缆时,正常通讯距离进一步加长。这种情况主要是由总线分布电容使信号波形产生畸变造成的。因此,在用DS1820进行长距离测温系统设计时要充分考虑总线分布电容和阻抗匹配问题。

  (4)在DS1820测温程序设计中,向DS1820发出温度转换命令后,程序总要等待DS1820的返回信号,一旦某个DS1820接触不好或断线,当程序读该DS1820时,将没有返回信号,程序进入死循环。这一点在进行DS1820硬件连接和软件设计时也要给予一定的重视。

 

经过1天半的奋战终于完成了DS18B20组网的驱动!不过只是两点的组网,不过多点组网应该也不存在技术上难题!

下面是驱动程序

头文件

#ifndef DS18B20_H
#define DS18B20_H
#include "main.h"
//define port and port direction registor
#define Dout RD0
#define DoutDir TRISD0
//define command word
#define ROM_READ 0x33
#define ROM_MATCH 0x55
#define ROM_SKIP 0xCC
#define ROM_SERCH 0xF0
#define ROM_ALARM 0xEC

#define MEM_WRITE 0x4E
#define MEM_READ 0xBE
#define MEM_COPY 0x48
#define MEM_CONVERT 0x44
#define MEM_RECALL 0xB8
#define POWER_READ 0xB4
//define function
void configure_ds18b20(uchar flag) ;//configure the accuration of thermometer
uchar init_ds18b20() ;//初始化ds18b20
uchar read_ds18b20() ;//read  a byte  from ds18b20
void write_ds18b20(uchar data) ;//write a byte to ds18b20
void get_sequence(uchar *seq) ;//acquire the rom sequence of ds18b20
char get_configure() ;//acquire the configuration of accuration of thermometer
uint get_temp() ;//capture temperature from ds18b20
float process_temp(uint data) ;//process the  return value of the get_temp function

void start_convert() ;//start thermometer convert
uint read_temp() ;
void match_rom(uchar data[8]) ;
uint test(const uchar *data) ;//read temperature from ds18b20 specified by data
#endif

子程序

#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  ;
 } 
}

主程序

#include <stdio.h>
#include <stdlib.h>


#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个函数来实现的!只需要按照功能的使用命令即可!

抱歉!评论已关闭.