欢迎光临MCUWAY! 本站网名:单片机之路  永久域名:www.mcuway.com

设为首页

加入收藏

电子基础 初学MCU 单片机类型 MCU程序 MCU应用 设计软件 测控电路 技术论坛
 您现在的位置: 单片机之路 >> 单片机程序 >> 单片机源码 >> 文章正文

温度传感器18B20单总线驱动程序

/************************************************************
*18B20驱动程序,DQ为数据口,接于P2.1
*11.0592M晶振,上拉4.7k电阻
*Author:fyb
*2005-3-25 11:23,OK!
*************************************************************/
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint  unsigned int
sbit dq = P1^7;
bit  flag;
uint Temperature;
uchar temp_buff[9]; //存储读取的字节,read scratchpad为9字节,read rom ID为8字节
uchar id_buff[8];
uchar *p;
uchar crc_data;
uchar code CrcTable [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};
//
/************************************************************
*Function:延时处理
*parameter:
*Return:
*Modify:
*************************************************************/
void TempDelay (uchar us)
{
  while(us--);
}
/************************************************************
*Function:18B20初始化
*parameter:
*Return:
*Modify:
*************************************************************/
void Init18b20 (void)
{
 dq=1;
 _nop_();
 dq=0;
 TempDelay(86);   //delay 530 uS//80
 _nop_();
 dq=1;
 TempDelay(14);   //delay 100 uS//14
 _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写入一个字节
*parameter:
*Return:
*Modify:
*************************************************************/
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的一个字节
*parameter:
*Return:
*Modify:
*************************************************************/
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);
}

本文转自网络,经“单片机之路”(www.mcuway.com)摘选整理,版权归原作者所有

[1] [2] 下一页

普通文章28课:单片机音乐汇编程序设计
普通文章27课:单片机的一些基本概念
普通文章26课:单片机矩阵式键盘接口技术
普通文章51单片机教程(从原理开始基于汇编)
普通文章1课:单片机概述
普通文章2课:8051单片机引脚介绍
普通文章3课:单片机存储器结构结构分析
普通文章4课:第一个单片机小程序
普通文章5课:单片机延时程序分析
普通文章6课:单片机并行口结构
普通文章7课:单片机的特殊功能寄存器
普通文章8课:单片机寻址方式与指令系统
普通文章9课:单片机数据传递类指令
普通文章10课:单片机数据传送类指令
普通文章11课:单片机算术运算指令
普通文章12课:单片机逻辑运算类指令
普通文章13课:单片机逻辑与或异或指令祥解
普通文章14课:单片机条件转移指令
普通文章15课:单片机位操作指令
普通文章16课:单片机定时器与计数器
普通文章17课:单片机定时器/计数器的方式控制
普通文章18课:单片机的中断系统
普通文章19课:单片机定时器、中断试验
普通文章20课:单片机定时/计数器实验
普通文章21课:单片机串行口介绍

  • 上一篇文章:
  • 下一篇文章:
  • 更多精彩内容>>>

    普通文章电流单位
    普通文章功率单位
    普通文章什么是达林顿管?
    普通文章晶闸管的作用与原理

    学习单片机的必经之路——单片机之路,永久域名:www.mcuway.com