Using AVR to drive MAXIN's DS18B20

Publisher:NanoScribeLatest update time:2019-11-28 Source: 51heiKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I am currently studying how to use AVR to drive MAXIN's DS18B20. After a few days of thinking, I finally wrote its driver.

Using IAR C++ Compiler


#ifndef DS18B20_H
#define DS18B20_H
/****************************************Just define the following macros in the source program
#define seDDS() (seDB0())
#define clDDS() (clDB0())
#define seDS() (sePB0())
#define clDS() (clPB0())
#define GetDS() (GetPB0())
*********************************************/
/*********************************** Function Description *********************
void DS18B20::DS18B20(void) Constructor
void DS18B20::init() Initialize DS18B20
char DS18B20::GetID() Read the device ID. Return 1 if successful, and the 64-bit ID number of the device is saved in DS18B20.id[4]. Return 0 if failed.
char DS18B20::convter() reads the temperature and returns 1 if successful, and the temperature value is saved in DS18B20.temp. If not ready, it returns 0. This program needs to be called several times to convert correctly.
                       The calling cycle must not be less than 2ms.
char DS18B20::GetPower() Reads the bus power mode. Normally it returns 1, and parasitic returns 0.
char DS18B20::SearchAlarm() Reads the alarm. It returns 1 if there is an alarm, and 0 if there is no alarm.

****************************************************/
#define DS_ReadROM 0x33
#define DS_CopyROM 0x48
#define DS_MatchROM 0x55
#define DS_SkipROM 0xCC
#define DS_SearchROM 0xF0
#define DS_AlarmSearch 0xEC
#define DS_Convert 0x44
#define DS_ReadRAM 0xBE
#define DS_WriteRAM 0x4E #define DS_ReadPower
0xB4
#define DS_9BIT 0
#define DS_10BIT 1
#define DS_11BIT 2
#define DS_12BIT 3
#include "c:incstatus.h"
#include "c:inccrc8.c"
#include "c:incdelay.c"
#include "c:incformat.h"
#include

class DS18B20
{
public:

  float temp;// temperature  
  int rel;// temperature offset
  char id[8];// device ID
  char mode;// conversion mode 9-12bit
  unsigned char TH;// warning temperature upper limit
  unsigned char TL;// warning temperature lower limit
  char power;// 0: parasitic power supply 1: power supply
  char status;// single-wire bus status 0: not connected 1: connected
  char sbuf[10];

private:

  char setup;
/********************************************Reset bus***********************/
  char Reset_DS()
  {
    unsigned int x;
    seDS();
    seDDS();
    DELAY_80us();
    if(GetDS())
    {
      clDS();
      DELAY_600us();
      seDS();
      clDDS();
      for(x=0;x<30;)
      {
        if(GetDS())x++;
        else
        {
          for(x=0;x<30;)
          {
     if(GetDS())
            {
        seDDS();
        DELAY_600us();//
        return 1;
     }
     else
       x++;
            DELAY_10us();
   }
   clDDS();
   return 0;
        }
        DELAY_10us();//
      }
    }
    clDDS();
    return 0;
  }
/******************************************************Write bus*******************/
  void Write_DS(unsigned dat,unsigned char wide)// dat: data wide: data width (number of bits)
  {
    unsigned char i,sreg;
    seDDS();
    for(i=0;i    {
      sreg=SREG;
      SREG&=0x7F;// turn off interrupt
      if(dat&0x01)
      {
        clDS();
        DELAY_10us();
seDS();
        DELAY_80us();
      }
      else
      {
        clDS();
        DELAY_80us();
seDS();
        DELAY_10us();
      }
      SREG=sreg;// Resume interrupt The interrupt that occurred will be executed in sequence
      dat>>=1;
    }
  }
/*******************************************************Read bus*******************/
  unsigned char Read_DS(unsigned char wide)
  {
    unsigned char dat,i,sreg;
    for(i=dat=0;i    {
      if(GetDS())
      {
        sreg=SREG;
        SREG&=0x7F;
        clDS();
        seDDS();
        DELAY_10us();
        seDS();
        clDDS();
        dat>>=1;
        DELAY_10us();
        if(GetDS())dat|=0x80;
        seDDS();
        SREG=sreg;
        DELAY_80us();
      }
      else
      {
        setup=0;
      }
    }
    return dat;
  }

  char getid()
  {
    char i;
    for(i=0;i      if(id)
        return 1;
    return 0;
  }
public:
  char convert()// The calling cycle of this program is at least 2ms or above
  {
    static unsigned char sbuf[9];
    static unsigned int i;
    switch(setup)
    {
    case 0:
      if(Reset_DS())// Start
      {
        status=1;
        setup++;
      }
      else
status=0;
      break;
    case 1:
      if(getid())
      {
        Write_DS(DS_MatchROM,8);// Match ROM
        for(i=0;i<8;i++)
          Write_DS(id,8);
      }
      else
      {
        Write_DS(DS_SkipROM,8);// Skip match
      }
      Write_DS(DS_Convert,8);// Start conversion
      i=power?0:500;// Select power mode to achieve the fastest speed
      setup++;
      break;
    case 2:
      if(i)// Delay
      {
        i--;
      }
      else if(Read_DS(8)==0xFF)// Wait for conversion to end
      {
        Reset_DS();
        if(getid())
        {
          Write_DS(DS_MatchROM,8);// Match ROM
          for(i=0;i<8;i++)
            Write_DS(id,8);
        }
        else
          Write_DS(DS_SkipROM,8);
        Write_DS(DS_ReadRAM,8);
        i=0;
        setup++;
      }
      break;
    case 3:
      sbuf=Read_DS(8);
      if(i>=9)
      {
        Reset_DS();
        if(GetCRC8(sbuf,8)==sbuf[8])// Check CRC
        {
          int i;
          i=sbuf[1]*0x100+sbuf[0];
          i+=rel;
          temp=i/16+(i%16)*0.125;
          setup=0;
          status=1;
          return 1;
        }
        setup=0;
      }
      break;
    }
    return 0;
  }
  char GetPower()
  {
    unsigned char x;
    if(Reset_DS())
    {
      Write_DS(DS_SkipROM,8);
      Write_DS(DS_ReadPower,8);
      x=Read_DS(8);
      Reset_DS();
      if(x)
        return 1;
    }
    return 0;
  }
/*********************************************Alarm search****************************/
  char SearchAlarm()
  {
    if(Reset_DS())
    {
      Write_DS(DS_AlarmSearch,8);
      if(Read_DS(2)==0x80)// Read two bits
      {
        Reset_DS();
return 1;
      }
    }
    Reset_DS();
    return 0;
  }
//TH: high temperature limit TL: low temperature limit    
//mode: conversion mode 0:9bit(93.75ms) 1:10bit(187.5ms) 2:11bit(375ms) 3:12bit(750ms)  
//power: bus power mode
//ID: device ID number
  char init()
  {
    unsigned char i,sbuf[9];
    if(Reset_DS())
    {
      if(getid())
      {
Write_DS(DS_MatchROM,8);
for(i=0;i<8;i++)
   Write_DS(id,8);
      }
      else
        Write_DS(DS_SkipROM,8);
      Write_DS(DS_WriteRAM,8);
      Write_DS(TH,8);
      Write_DS(TL,8);
      Write_DS(((mode&0x03)<<5)|0x1F,8);
      Reset_DS();
      if(getid())
      {
Write_DS(DS_MatchROM,8);
        for(i=0;i<8;i++)
   Write_DS(id,8);
      }
      else
        Write_DS(DS_SkipROM,8);
      Write_DS(DS_ReadRAM,8);
      for(i=0;i<9;i++)
        sbuf=Read_DS(8);
      if(GetCRC8(sbuf,8)==sbuf[8])//  校验CRC
      {
        if(sbuf[2]==TH&&sbuf[3]==TL)
        {
          Reset_DS();
          if(getid())
          {
            Write_DS(DS_MatchROM,8);
            for(i=0;i<8;i++)
                Write_DS(id,8);
          }
          else
            Write_DS(DS_SkipROM,8);
          Write_DS(DS_CopyROM,8);
          DELAY_200ms();
          Reset_DS();
          power=GetPower();
          Reset_DS();
          return 1;//                 完成
        }
      }
    }
    return 0;
  }
/*********************************************读取DS18B20的64位ROM******************/
  char GetID()
  {
    unsigned char i;
    unsigned char sbuf[8];
   if(Reset_DS())
   {
     Write_DS(DS_ReadROM,8);
     for(i=0;i<8;i++)
     {
       sbuf=Read_DS(8);
     }
     Reset_DS();
     if(GetCRC8(sbuf,7)==sbuf[7])//   校验CRC
     {
       for(i=0;i<8;i++)
         id=sbuf;//    拷贝数据到ROM
       return 1;
     }
   }
   return 0;
  }

  char* disp(char width=0)
  {
    int i=(int)temp;
    int j=(int)(temp*10);
    j%=10;
    if(temp<0)
      j=0-j;
    switch(width)
    {
    case 2:
      sprintf(sbuf,"%2d.%d",i,j);
      break;
    case 3:
      sprintf(sbuf,"%3d.%d",i,j);
      break;
    default:
      sprintf(sbuf,"%d.%d",i,j);
      break;
    }
    return sbuf;
  }

  DS18B20()
  {
    format(id,sizeof(id),0);
    mode=DS_12BIT;
    TH=100;
    TL=0;
    power=0;
    rel=0;
    status=0;
    setup=0;
    seDDS();
    seDS();
  }
};
#endif

Keywords:AVR Reference address:Using AVR to drive MAXIN's DS18B20

Previous article:Detailed explanation of ds1302 clock program for avr microcontroller
Next article:Phase Corrected PWM Mode Setting for AVR Timer 1

Recommended ReadingLatest update time:2024-11-16 09:18

5G paints a vision for the future of smart manufacturing, and TSN drives the development of industrial intelligence
Roland Berger, a world-renowned consulting firm, pointed out in its industry report "Embracing the New World of 5G": 5G, through powerful wireless connections, edge computing and network slicing technologies, will help wireless automation control, industrial cloud robots, predictive maintenance, flexible production an
[Internet of Things]
5G paints a vision for the future of smart manufacturing, and TSN drives the development of industrial intelligence
2 ds18b20 sensors are connected to the single bus of the microcontroller, and 8-bit digital display is displayed simultaneously
I plan to make a thermometer that can detect indoor and outdoor temperatures at the same time and display them with a digital tube. I found a program on the Internet to get the ds18b20 serial code, and used the development board 1602 to display it, and recorded it for later use. The array str1,2 below is the 64-bit se
[Microcontroller]
Diodes Incorporated Introduces 22V/31V Peak Selectable Output Piezo Sounder Driver
Diodes Incorporated Launches 22V/31V Peak Selectable Output Piezoelectric Sounder Driver to Drive Higher SPLs and Operate Longer Plano, Texas, USA, August 11, 2021 - Diodes Incorporated introduces the PAM8907 piezoelectric sounder driver, which maximizes sound pressure level through ceramic/piezoelect
[Power Management]
Diodes Incorporated Introduces 22V/31V Peak Selectable Output Piezo Sounder Driver
Cooling solutions for automotive motor drive power modules
Hybrid electric vehicles (HEV) or plug-in electric vehicles (PEV) usually use motor drive power modules, such as IGBT modules, which generate very large heat, usually about 1,000W to 2,000W. The only effective way to cool these automotive electronic power modules is water cooling, not air cooling, because the therma
[Embedded]
Practical Analysis of 8051 MCU (Taking STC89C52RC as an Example) | 04 - Buzzer Driver
The principle of buzzer sound is that the current passes through the electromagnetic coil, which makes the electromagnetic coil generate a magnetic field to drive the vibration membrane to make sound. Therefore, a certain current is required to drive it. The current output by the IO pin of the microcontroller is small
[Microcontroller]
Practical Analysis of 8051 MCU (Taking STC89C52RC as an Example) | 04 - Buzzer Driver
Silicon Labs Expands Isolated Gate Driver Portfolio with New Si828x Version 2
Silicon Labs Expands Isolated Gate Driver Portfolio with New Si828x Version 2 New Si828x features optimized for electric vehicles and a variety of industrial applications Beijing, China – May 18, 2021 – Silicon Labs (NASDAQ: SLAB), a leading provider of silicon, software and solutions for a s
[Power Management]
Silicon Labs Expands Isolated Gate Driver Portfolio with New Si828x Version 2
Introduction to the structure, working principle, performance advantages and disadvantages of new energy vehicle drive motors
introduction Based on the basic performance requirements of new energy vehicle drive motors, the commonly used drive motor types currently include three main categories, namely AC asynchronous motors, permanent magnet synchronous motors and switched reluctance motors. At present, in the statistics of supporting
[Embedded]
Introduction to the structure, working principle, performance advantages and disadvantages of new energy vehicle drive motors
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号