⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 18b20_crc.c

📁 18B20温度测量
💻 C
字号:
/*DQ为数据口,接于P2.1
/*18B20采用三线接法,上拉4.7K电阻,11.0592M*/
/*Author:xiaohua  */
/*Date:05/10/1 */

#include <reg52.h>
#include <intrins.h>

#define uchar unsigned char
#define uint unsigned int

//
sbit dq=P2^1;
bit flag;
uint temp;
uchar temp_buff[9];
uchar id_buff[8];
uchar *p;
uchar crc_data;
uchar code crc_table[256]={

  0, 94,188,226, 97, 63,221,131,  194,156,126, 32,163,253, 31, 65,   //  0-- 15

157,195, 33,127,252,162, 64, 30,   95,  1,227,189, 62, 96,130,220,   // 16-- 31

 35,125,159,193, 66, 28,254,160,  225,191, 93,  3,128,222, 60, 98,   // 32-- 47

190,224,  2, 92,223,129, 99, 61,  124, 34,192,158, 29, 67,161,255,   // 48-- 63

 70, 24,250,164, 39,121,155,197,  132,218, 56,102,229,187, 89,  7,   // 64-- 79

219,133,103, 57,186,228,  6, 88,   25, 71,165,251,120, 38,196,154,   // 80-- 95

101, 59,217,135,  4, 90,184,230,  167,249, 27, 69,198,152,122, 36,   // 96--111

248,166, 68, 26,153,199, 37,123,   58,100,134,216, 91,  5,231,185,   //112--127

140,210, 48,110,237,179, 81, 15,   78, 16,242,172, 47,113,147,205,   //128--143

 17, 79,173,243,112, 46,204,146,  211,141,111, 49,178,236, 14, 80,   //144--159

175,241, 19, 77,206,144,114, 44,  109, 51,209,143, 12, 82,176,238,   //160--175

 50,108,142,208, 83, 13,239,177,  240,174, 76, 18,145,207, 45,115,   //176--191

202,148,118, 40,171,245, 23, 73,    8, 86,180,234,105, 55,213,139,   //192--207

 87,  9,235,181, 54,104,138,212,  149,203, 41,119,244,170, 72, 22,   //208--223

233,183, 85, 11,136,214, 52,106,   43,117,151,201, 74, 20,246,168,   //224--239

116, 42,200,150, 21, 75,169,247,  182,232, 10, 84,215,137,107, 53 }; //240--255

//

void delay(uchar us)
{
  while(us--);
}
//

void init_18b20(void)
{
  dq=1;
  _nop_();
  dq=0;
  delay(80);          //delay 530uS
  dq=1;
  delay(14);          //delay 100uS    
  if(dq==0)
     flag=1;          //detect 1820 success!
  else
     flag=0;          //detect 1820 fail!
  delay(20);
  dq=1;
}
//

void write(uchar wr)  //单字节写入
{
uchar i;
for(i=0;i<8;i++)
 {
  dq=0;
  _nop_();
  dq=wr&0x01;
  delay(5);           //delay 45 uS
  dq=1;
  wr>>=1;
 }
}
//

uchar read_byte(void) //读取单字节
{ 
uchar i,u=0;
for(i=0;i<8;i++)
 {
  dq=0;
  u>>=1;
  dq=1;
  if(dq==1)
     u|=0x80;
  delay(4);
 }
return(u);
}
//

void read_bytes(uchar j)//读取多字节
{
uchar i;
for(i=0;i<j;i++)
 {
  *p=read_byte();
  p++;
 }
}
//

uchar crc(uchar j)
{
uchar i,crc_data=0;
for(i=0;i<j;i++)         //查表校验
  crc_data=crc_table[crc_data^temp_buff[i]];
return(crc_data);
}
//

void get_temp(void)
{
read_bytes(9);
if(crc(9)==0)            //校验正确
 {
  temp=temp_buff[1]*0x100+temp_buff[0];
  temp*=0.625;
 }
}
//

void config(void)        //重新配置报警限定值和分辨率
{
init_18b20();
write(0xcc);             //skip rom 
write(0x4e);             //write scratchpad
write(0x19);             //上限
write(0x1a);             //下限
write(0x7f);             //set 11 bit (0.125)
init_18b20();
write(0xcc);             //skip rom
write(0x48);             //保存设定值
init_18b20();
write(0xcc);             //skip rom
write(0xb8);             //回调设定值
}
//

void read_id(void)       //读取器件 id
{
init_18b20();
write(0x33);             //read rom
read_bytes(8);
}
//

main()               //演示程序
{
p=id_buff;
read_id();
config();
for(;;)
 {
  init_18b20();
  write(0xcc);           //skip rom
  write(0x44);           //temp convert
  init_18b20();
  write(0xcc);           //skip rom
  write(0xbe);           //read temp
  p=temp_buff;
  get_temp();
 }
}
//

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -