Example of reading temperature sensor DS18B20

Source: Internet
Author: User
Tags reset
Configuring the IO Pin
#define DS18B20         BIT4                      //Configure IO pin
#define Ds18b20_high    p2out |= BIT4
#define Ds18b20_low     p2out &= ~BIT4
precise delay macro code
#define Cpu_clock       8000000UL               //mclk is set to 8MHz                
#define DELAY_US (US)    __delay_cycles (cpu_clock/1000000 * (US))
#define Delay_ms (ms)    __delay_cycles (cpu_clock/1000* (MS))
Find a table of binary fractional parts
const int n16tab[16]={      //used for binary fractional parts look up table
    0,
    625,
    1250,
    1875, 2500,
    3125,
    3750,
    4375,
    5625,
    6250,
6875, 7500, 8125, 8750, 9375} ;
Reset
/* ********************************************************************** * Name: ds18b20_init () * Function: RESET DS18B20 * Entry parameter: None * Export parameters: The existence of a, 0--does not exist * Description: Need to configure IO, software delay to accurate * Example: no *********************************************************************
        * */char Ds18b20_reset (void) {int i;
        Char e;                       -------Low-----------------------------------p2dir |= Ds18b20;                            configured as output ds18b20_low;          Pull down//-------wait more than 480 microseconds----------------------------Delay_us (540);                      Wait less than 480 microseconds//-------Pull high-----------------------------------p2dir &= ~ds18b20;                        Configured as input P2ren |=ds18b20;                           Enable resistor Ds18b20_high;

        Pull//-------Wait 15 microseconds---------------------------------Delay_us (15);
        -------detection exists-------------------------------------------i=0;
        e=0;
   while (1) {         if ((p2in & Ds18b20) ==0) {e=1; 
            Break
            } delay_us (1);
            i++;   
            if (i>60) {break;
        }}//------------delay more than 480 microseconds---------------------------------Delay_us (500);
return e;  }
Read bytes
/* ********************************************************************** * Name: ds18b20_readbyte () * Function: Read bit * entry parameter: none * Exit parameters: Returns a byte read back from Ds18b20 * Description: Need to configure IO, software delay to accurate * Example: no *****************************************************************
    */unsigned char ds18b20_readbyte (void) {unsigned char i,retd=0;               for (i=0;i<8;i++)//bit-count value {retd>>=1;        Move right, ready to accept the new data bit//-------pull down bus 5 microseconds--------------------------P2dir |=ds18b20;            configured as output ds18b20_low;        Pull low, start reading data bit Delay_us (5);      Wait 5 microseconds//-------release bus-----------------------------------p2dir &= ~ds18b20;        Configured as input P2ren |=ds18b20;           Enable resistor Ds18b20_high;       Pull//-------Wait 15 microseconds-----------------------------Delay_us (15);
            Wait 15 microseconds//-------Check if the bit is high-----------------------if (P2IN&AMP;DS18B20)//The bit is high {     retd|=0x80; is to have this locationHigh}//-------Wait 42 microseconds-----------------------------Delay_us (42);                Wait 42 microseconds} return retd; Returns a byte read to}
Write Bytes
/* ********************************************************************** * Name: ds18b20_writebyte () * Function: Writing section * Entry parameter: none * Export parameters: No * Description: Need to configure IO, software delay to accurate * Example: no ********************************************************************** */void DS18

    B20_writebyte (unsigned char wrd) {unsigned char i; for (i=0;i<8;i++)//bit count value {if (WRD&AMP;0X01)///This bit data is high {//-----        --Pull down bus 5 μs--------------------------P2dir |=ds18b20;  
              configured as output ds18b20_low;

              Delay_us (5); -------Pull-up bus 60 μs----------------------------------p2dir &= ~ds18b20;//configured as input P2ren |=DS18  B20;     Enable resistor Ds18b20_high;
        Pull-up Delay_us (60);  } else {//-------pull down bus 60 microseconds----------------------------------P2dir |=ds18b20;      configured as output ds18b20_low;

        is low, the single bus is pulled low delay_us (60);      -------Pull-up bus 5 μs-----------------------------------p2dir &= ~ds18b20;//configured as input P2ren  |=ds18b20;     Enable resistor Ds18b20_high;
        Pull-up Delay_us (5);

        }//-------wait more than 1 microseconds-------------------------------Delay_us (2);                -------Move right 1-bit-------------------------------------wrd>>=1; Move right to prepare for writing new bits of Data}}
Reading temperature

A complete read temperature flow.

/* ********************************************************************** * Name: ds18b20_readtemp () * Function: Read temperature * entry parameter: none * Exit Parameters: Return Temperature Value * Description: Need to configure IO, software delay to accurate * Example: no ********************************************************************** */char E
;
    int ds18b20_readtemp () {unsigned char buff[9];
    unsigned char i;

    unsigned char a,b,c;
    ------MCLK is set to 8MHz------------Dcoctl=caldco_8mhz;

    Bcsctl1=calbc1_8mhz;          E=ds18b20_reset ();        initialization, each write command starts from initialization ds18b20_writebyte (0XCC);        Skip ROM Ds18b20_writebyte (0x44);                  Starting temperature Conversion Delay_ms (750);              Delay 750us E=ds18b20_reset ();        initialization, each write command starts from initialization ds18b20_writebyte (0XCC);        Skip ROM Ds18b20_writebyte (0xbe);   Read-in register command//-----Read 9 consecutive-----------------for (i=0;i<9;i++) {buff[i]=ds18b20_readbyte ();
    Read Register}/*/---------Calculate temperature----------------------------------------------------a=buff[0]&0x0f; if ((buff[1]&AMP;0X80)! = 0) {a=~a;
            a=a&0x0f;
    A+=1;      } *fdat=n16tab[a];
    Get the b=buff[0]>>4; of the decimal part
    c=buff[1]<<4;
    C|=b;              *idat=c;
    Get the integer part *//-----MCLK set to 1MHz-----------Dcoctl=caldco_1mhz;

    Bcsctl1=calbc1_1mhz;
Return ((int*) buff) [0]; }
calculate the temperature value

By binary data, get the integer part Idat, fractional part fdat.

void calc_temp (int hex,char *idat,int *fdat)
{
    unsigned char a,b,c;
    char Hi,lo;
    Lo=hex;
    hi=hex>>8;
    ---------Calculate temperature----------------------------------------------------
    a=lo&0x0f;
    if ((hi&0x80)! = 0)
    {
        a=~a;
        a=a&0x0f;
        a+=1;
    }
    *fdat=n16tab[a];      Get the

    b=lo>>4;
    of the decimal part c=hi<<4;
    c|=b;
    *idat=c;              Get the integer part
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.