找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1871|回复: 2
打印 上一主题 下一主题
收起左侧

DS18B20的单片机代码,求解释

[复制链接]
跳转到指定楼层
楼主
下面代码红色部分几个,那个数组是做什么用的,为什么显示序列号的时候是一个十六进制显示高位,一个显示低位?还有那个CRC=X8+X5+X4+1是什么意思?X8不是应该第九位二进制为1吗,八位循环码为什么会有第九位呢??看了很久,就剩这两个地方看不懂,求大神解释下,谢谢了。
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint  unsigned int
sbit DQ = P2^2;  //定义DS18B20端口DQ  
sbit BEEP=P2^3 ; //蜂鸣器驱动线
bit  presence ;
sbit LCD_RS = P3^5;            
sbit LCD_RW = P3^6;
sbit LCD_EN = P3^4;
uchar code  cdis1[ ] = {"   DS18B20 OK   "};
uchar code  cdis2[ ] = {"                "};
uchar code  cdis3[ ] = {" DS18B20  ERR0R "};
uchar code  cdis4[ ] = {"  PLEASE CHECK  "};
uchar data  display[2] = {0x00,0x00};
                                    
uchar data  RomCode[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
uchar Temp;
uchar  crc;
void beep();
#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};
/*******************************************************************/
void delay1(int ms)
{
unsigned char y;
  while(ms--)
{
  for(y = 0; y<250; y++)
  {
   _nop_();
   _nop_();
   _nop_();
   _nop_();
  }
}
}
/******************************************************************/
/*                                                                */
/*检查LCD忙状态                                                   */
/*lcd_busy为1时,忙,等待。lcd-busy为0时,闲,可写指令与数据。     */
/*                                                                */
/******************************************************************/
bit lcd_busy()
{                          
    bit result;
    LCD_RS = 0;
//    LCD_RW = 1;
    LCD_EN = 1;
    delayNOP();
    result = (bit)(P0&0x80);
    LCD_EN = 0;
    return(result);
}
/*******************************************************************/
/*                                                                 */
/*写指令数据到LCD                                                  */
/*RS=L,RW=L,E=高脉冲,D0-D7=指令码。                             */
/*                                                                 */
/*******************************************************************/
void lcd_wcmd(uchar cmd)
{                          
//  while(lcd_busy());
    LCD_RS = 0;
//   LCD_RW = 0;
    LCD_EN = 0;
    _nop_();
    _nop_();
    P0 = cmd;
    delayNOP();
    LCD_EN = 1;
    delayNOP();
    LCD_EN = 0;  
delay1(5);
}
/*******************************************************************/
/*                                                                 */
/*写显示数据到LCD                                                  */
/*RS=H,RW=L,E=高脉冲,D0-D7=数据。                               */
/*                                                                 */
/*******************************************************************/
void lcd_wdat(uchar dat)
{                          
//   while(lcd_busy());
    LCD_RS = 1;
//    LCD_RW = 0;
    LCD_EN = 0;
    P0 = dat;
    delayNOP();
    LCD_EN = 1;
    delayNOP();
    LCD_EN = 0;
delay1(5);
}
/*******************************************************************/
/*                                                                 */
/*  LCD初始化设定                                                  */
/*                                                                 */
/*******************************************************************/
void lcd_init()
{
    delay1(15);   
    lcd_wcmd(0x01);      //清除LCD的显示内容           
    lcd_wcmd(0x38);      //16*2显示,5*7点阵,8位数据
    lcd_wcmd(0x0c);      //显示开,关光标
    lcd_wcmd(0x06);      //移动光标
}
/*******************************************************************/
/*                                                                 */
/*  设定显示位置                                                   */
/*                                                                 */
/*******************************************************************/
void lcd_pos(uchar pos)
{                          
  lcd_wcmd(pos | 0x80);  //数据指针=80+地址变量
}
/*******************************************************************/
/*                                                                 */
/*us级延时函数                                                     */
/*                                                                 */
/*******************************************************************/
void Delay(unsigned int num)
{
  while( --num );
}
/*******************************************************************/
/*                                                                 */
/*初始化ds1820                                                     */
/*                                                                 */
/*******************************************************************/
Init_DS18B20(void)
{  
     DQ = 1;      //DQ复位
     Delay(8);    //稍做延时
     DQ = 0;      //将DQ拉低
     Delay(90);   //精确延时 大于 480us
     DQ = 1;       //拉高总线
     Delay(8);
     presence = DQ;    //读取存在信号
     Delay(100);
     DQ = 1;
     
     return(presence); //返回信号,0=presence,1= no presence
}
/*******************************************************************/
/*                                                                 */
/* 读一位(bit)                                                   */
/*                                                                 */
/*******************************************************************/
uchar read_bit(void)
{
uchar i;
DQ = 0;    //将DQ 拉低开始读时间隙
i++;
DQ = 1;    // then return high
for (i=0; i<3; i++);  // 延时15μs
return DQ;    // 返回 DQ 线上的电平值
}
/*******************************************************************/
/*                                                                 */
/* 读一个字节                                                      */
/*                                                                 */
/*******************************************************************/
ReadOneChar(void)
{
uchar i = 0;
uchar dat = 0;
//for (i = 8; i > 0; i--)
//  {
//    read_bit();
//    DQ = 0; // 给脉冲信号
//     dat >>= 1;
//    DQ = 1; // 给脉冲信号
for (i=0;i<8;i++)
{      // 读取字节,每次读取一个字节
if(read_bit()) dat|=0x01<<i;    // 然后将其左移
//    if(DQ)
//     dat |= 0x80;
    Delay(4);
  }
    return (dat);
}
/*******************************************************************/
/*                                                                 */
/* 写一位                                                          */
/*                                                                 */
/*******************************************************************/
void write_bit(char bitval)
{
DQ = 0;            // 将DQ 拉低开始写时间隙
if(bitval==1) DQ =1;   // 如果写1,DQ 返回高电平
Delay(5);           // 在时间隙内保持电平值,
DQ = 1;               // Delay函数每次循环延时16μs,因此delay(5) = 104μs
}
/*******************************************************************/
/*                                                                 */
/* 写一个字节                                                      */
/*                                                                 */
/*******************************************************************/
WriteOneChar(unsigned char dat)
{
  uchar i = 0;
  uchar temp;
//  for (i = 8; i > 0; i--)
//  {
   for (i=0; i<8; i++)  // 写入字节, 每次写入一位
   {
//    DQ = 0;
//    DQ = dat&0x01;
//    Delay(5);
//    DQ = 1;
   temp = dat>>i;   
   temp &= 0x01;   
   write_bit(temp);
//    dat>>=1;
   
  }
  Delay(5);
}
/*******************************************************************/
/*                                                                 */
/* 读取64位序列码                                                  */
/*                                                                 */
/*******************************************************************/
Read_RomCord(void)
{
     uchar j;
     Init_DS18B20();
  
     WriteOneChar(0x33);  // 读序列码的操作
     for (j = 0; j < 8; j++)
  {
   RomCode[j] = ReadOneChar() ;
  }
}
/*******************************************************************/
/*                                                                 */
/*DS18B20的CRC8校验程序                                            */
/*                                                                 */
/*******************************************************************/
uchar CRC8()
{
   uchar i,x;
   uchar crcbuff;
   
   crc=0;
   for(x = 0; x <8; x++)
   {
     crcbuff=RomCode[x];
     for(i = 0; i < 8; i++)
      {
        if(((crc ^ crcbuff)&0x01)==0)
          crc >>= 1;
        else
     {
             crc ^= 0x18;   //CRC=X8+X5+X4+1
             crc >>= 1;
             crc |= 0x80;
        }         
        crcbuff >>= 1;      
   }
   }
     return crc;
}
/*******************************************************************/
/*                                                                 */
/* 数据转换与显示                                                  */
/*                                                                 */
/*******************************************************************/
Disp_RomCode()
{
   uchar j;
   uchar H_num=0x40;       //LCD第二行初始位置
   for(j=0;j<8;j++)
   {
    Temp = RomCode[j];
    display[0]=((Temp&0xf0)>>4);
    if(display[0]>9)
     { display[0]=display[0]+0x37;}
    else{display[0]=display[0]+0x30;}
    lcd_pos(H_num);            
    lcd_wdat(display[0]);        //高位数显示
    H_num++;
    display[1]=(Temp&0x0f);
    if(display[1]>9)
     {display[1]=display[1]+0x37;}
    else {display[1]=display[1]+0x30;}
    lcd_pos(H_num);            
    lcd_wdat(display[1]);        //低位数显示
    H_num++;
   }
}  


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:111634 发表于 2017-11-4 17:20 | 只看该作者
本帖最后由 zl2168 于 2017-11-4 17:26 编辑

你看了一个不易看懂的程序,所以看不懂。介绍你一个容易看懂的案例。
Proteus仿真一下,确认有效。
实例97 DS18B20测温.rar (51.78 KB, 下载次数: 5)

以上摘自张志良编著《80C51单片机仿真设计实例教程——基于Keil CProteus》清华大学出版社ISBN 978-7-302-41682-1内有常用的单片机应用100案例,用于仿真实验操作,电路与程序真实可靠可信可行。仿真电路和Hex文件能在清华出版社网站免费下载,程序源代码只能到书上看了。到图书馆借,或到新华书店翻阅,或到网上书店打折购买。
回复

使用道具 举报

板凳
ID:240646 发表于 2017-11-6 21:49 | 只看该作者
zl2168 发表于 2017-11-4 17:20
你看了一个不易看懂的程序,所以看不懂。介绍你一个容易看懂的案例。
先Proteus仿真一下,确认有效。
以 ...

可是我看这个代码不是获取温度的,是获取DS18B20的序列号的,看这个代码是想做一个多点测温的东西。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表