Skip to main content
Interfacing DS1822 Digital Thermometer with the P89v51RD2FN Micro Controller


Description

                    Interfacing the DS1822 Digital Thermometer with the P89v51rd2 Micro controller,  The Source code and Simulator Model of this Communication is listed Below.

 It has the Two Modules

1. Temperature.c

#include <intrins.h>
#include<stdio.h>
#include "Temperature.h"

volatile unsigned char xdata T_cRet; 
volatile unsigned char xdata T_cData; 
volatile unsigned int xdata T_iM;
volatile unsigned char xdata T_cDataROM[90];
volatile unsigned char xdata T_iL;
volatile unsigned char xdata T_j ;
//volatile unsigned char xdata T_j;
volatile unsigned char xdata T_iK ;

volatile unsigned char xdata T_cROMID[10];
volatile unsigned char xdata DOWCRC;
//volatile unsigned char xdata DOWCRC _at_ 0x5060;
volatile unsigned int xdata T_nTemperature;

volatile unsigned int xdata T_nTemp;
//Max -Min +125 to -55
volatile char xdata cTemperature[7];
volatile unsigned char xdata T_cTemp;
volatile unsigned char xdata t[4];
sbit P33=0xB3;

Comm1_WriteChar(unsigned char u)
{;}

//One wire reset.
//Function returns 0 -presence pulse detected
//Function returns 1 -presence pulse not detected    
unsigned char OneWire_Reset()
{

    T_cData=1;
    P33=0;
    //480 us low
    Delay(36);
   
    //Master releases bus & goes into receive mode
    P33=1;
    //DS1822 waits 15-60 us
    Delay(4);
    //Presence pulse is a low for 60-240us
    T_cData=P33;
   
    //Remaining of the 480us high
    Delay(31);
   
    return T_cData;


}



//Delay - ??
void Delay(unsigned int iK)
{
    for(T_iM=0;T_iM<iK;T_iM++)
    {
        _nop_();
    }
    return;
}





//Write a Bit to the 1Wire Device
void WriteBit(unsigned char cWriteBit)
{

    //Both types of Write time slots are initiated by the master pulling the bus low

    P33=0;
    //To write a 1, after pulling the bus low,
    //the bus master must release the 1 wire bus within 15us


    //To write a 0, after pulling the bus low,
    //the bus master must continue to hold the bus low
    //for a duration of atleast 60us

    if(cWriteBit == 1)
    {
        //0.5us
        _nop_();

        
        P33=1;
        //50us to complete the time slot
        Delay(3);
    }
    else
    {
        Delay(3);        
        //5us
        _nop_();                
        _nop_();    
        _nop_();                
        _nop_();    
        _nop_();                
        _nop_();    
        _nop_();                
        _nop_();    
        _nop_();                
        _nop_();    

        P33=1;
    }
   
    return ;
}





//Temperature Convert Command-
//LSB first
void ConvertTemperatureCommand()
{
    //0x44 h
    //0100 0100
    WriteBit(0);    
    WriteBit(0);    
    WriteBit(1);    
    WriteBit(0);    
    WriteBit(0);    
    WriteBit(0);    
    WriteBit(1);    
    WriteBit(0);    

}




//Read Time slots must be minimum 60us in duration
//minimum 1 us recovery time b/w slots.

void ReadTimeSlots()
{

    //Read Time slot is initiated by master pulling the bus low for 1us
    P33=0;
    _nop_();
    //Release the bus
    P33=1;

    //Data valid for 15 us  after the falling edge
    T_cDataROM[T_iL]=P33;


    //Complete the slot
    //50us
    Delay(2);        
    //0.5us
    _nop_();                
    _nop_();    
    _nop_();                
    _nop_();    
    _nop_();                
    _nop_();    
    _nop_();                
    _nop_();    
    _nop_();                
    _nop_();
    _nop_();                
    _nop_();
    _nop_();    
    _nop_();                
    _nop_();
    _nop_();                
    _nop_();
    //Increment the counter for the array storing the data
    T_iL=T_iL+1;
    return;

}






//Command to Read Scratch Pad
void ReadScratchPadCommand()
{
    //0xBE h
    //1011 1110
    WriteBit(0);    
    WriteBit(1);    
    WriteBit(1);    
    WriteBit(1);    
    WriteBit(1);    
    WriteBit(1);    
    WriteBit(0);    
    WriteBit(1);    

}


//Send the command & Read the Scratch Pad
unsigned char ReadScratchPad()
{

    //Clear the Memory
    for(T_iL=0;T_iL<90;T_iL++)
    {
        T_cDataROM[T_iL]=0;    
    }
    //Clear the array
    for(T_iL=0;T_iL<10;T_iL++)
    {
        T_cROMID[T_iL]=0;    
    }
    //Clear the counter
    T_iL=0;
   

    //Send the command
    ReadScratchPadCommand();
   


    //Read 9 bytes i.e 72 bits
    //ReadTimeSlots - 72 times
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();

    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();

    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();


    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();


    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();



    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();

    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();



    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();
    ReadTimeSlots();


    //Comm1_WriteChar('[');
    //Store the 72 bits into 9 bytes
    T_j=0;
    //For 9 bytes
    for(T_iK=0;T_iK<9;T_iK++)
    {
        //1 byte
        for(T_iL=0;T_iL<8;T_iL++)
        {
            //concatenate the bit
            //If byte is 1
            if(T_cDataROM[T_j]==1)
            {
                //Left shift the bit into the byte            
                T_cROMID[T_iK]|=0x01 <<T_iL;
            }
            else
            {
                //Left shift the bit into the byte            
                T_cROMID[T_iK]|=0x00 <<T_iL;
            }
            //Increment the byte count
            T_j++;
   
        }
        //Comm1_WriteChar(T_cROMID[T_iK]);
    }
    //Comm1_WriteChar(']');


    //Calculate the CRC for the 0-6 bytes
    DOWCRC=0;
    for (T_j = 0; T_j <= 7; T_j++)
    {
        dowcrc(T_cROMID[T_j]);
    }        
    //XOR of CRC Received & CRC Calculated    should be 0
    if(T_cROMID[8] ^ DOWCRC == 0)
    {
        
        Comm1_WriteChar('{');
        Comm1_WriteChar(T_cROMID[8]);
        Comm1_WriteChar(DOWCRC);
        Comm1_WriteChar('}');
        //iButton ROM Read Successfully    
        
    }
    else
    {
        return 2;
    }





    //T_cROMID[0] (LSB) & T_cROMID[1] (MSB) contains the temperature

    T_nTemperature=0;
    //
    if(T_cROMID[1]<0x80)
    {
        T_nTemperature=((T_cROMID[1]&0x07)<<4);
        T_nTemperature+=((T_cROMID[0]&0xF0)>>4);
        
        cTemperature[0]='+';

   
    }
    else
    {    
        T_nTemp=0;
        T_nTemp=(T_cROMID[1]<<8)|T_cROMID[0];
        T_nTemp=~(T_nTemp);
        T_nTemp=T_nTemp+1;
        
        T_nTemperature=(T_nTemp>>4)&0x007F;
            
        cTemperature[0]='-';

        
    }


    cTemperature[1]=((T_nTemperature/100)+0x30);
    cTemperature[2]=(((T_nTemperature%100)/10)+0x30);
    cTemperature[3]=(((T_nTemperature%100)%10)+0x30);
    cTemperature[4]='\0';



    Comm1_WriteChar('{');
    Comm1_WriteChar(cTemperature[0]);t[0]=cTemperature[0];
    Comm1_WriteChar(cTemperature[1]);t[1]=cTemperature[1];
    Comm1_WriteChar(cTemperature[2]);t[2]=cTemperature[2];
    Comm1_WriteChar(cTemperature[3]);t[3]=cTemperature[3];
    Comm1_WriteChar('}');
     
    printf("%c",cTemperature[0]);
    printf("%c",cTemperature[1]);
    printf("%c",cTemperature[2]);
    printf("%c",cTemperature[3]);

   


    return 0;





}


//Command to Skip the ROM 
void SkipROM()
{
    //0xCC
    //1100 1100
    WriteBit(0);    
    WriteBit(0);    
    WriteBit(1);    
    WriteBit(1);    
    WriteBit(0);    
    WriteBit(0);    
    WriteBit(1);    
    WriteBit(1);    

    return;

}






//CRC Table
xdata unsigned char dscrc_table[] = {
        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};


//--------------------------------------------------------------------------
// Update the Dallas Semiconductor One Wire CRC (DOWCRC) from the global
// variable DOWCRC and the argument.  Return the updated DOWCRC.
//
unsigned char dowcrc(unsigned char x)
{
   DOWCRC = dscrc_table[DOWCRC ^ x];
   return DOWCRC;
}



//Send the Command to for Temperature Conversion
unsigned char TemperatureConversion()
{
    //Reset Command
    T_cRet=OneWire_Reset();
    //If the Presence Pulse is detected
    if(T_cRet== 0)
    {
        //Clear the memory where data will be stored
        for(T_iL=0;T_iL<90;T_iL++)
        {
            T_cDataROM[T_iL]=0;    
        }
        //Clear the array
        for(T_iL=0;T_iL<10;T_iL++)
        {
            T_cROMID[T_iL]=0;    
        }
        //Clear the counter
        T_iL=0;
   
        //Issue Skip ROM Command
        SkipROM();
        //Issue Temperature Conversion Command
        ConvertTemperatureCommand();    
        //Delay for conversion
        /*Delay(48000);
        //Issue Read Time Slot to get the result of temperature conversion
        ReadTimeSlots();
        //Temperature Conversion not completed
        if(T_cDataROM[0]== 0)
        {
            Comm1_WriteChar('*');
            return 1;
        }
        //Temperature Conversion completed
        else
        {
            Comm1_WriteChar('%');
            return 0;
        }*/
   

    }
    return 2;
}




unsigned char ReadTemperature()
{

   
   
    //Issue One Wire Reset Command
    T_cRet=OneWire_Reset();
    //If Presence Pulse is detected
    if(T_cRet== 0)
    {
        //Clear the memory where data will be stored
        for(T_iL=0;T_iL<90;T_iL++)
        {
            T_cDataROM[T_iL]=0;    
        }
        //Clear the array
        for(T_iL=0;T_iL<10;T_iL++)
        {
            T_cROMID[T_iL]=0;    
        }
        //Clear the counter
        T_iL=0;
   
        //Issue Skip ROM Command
        SkipROM();
        //Issue Read Scratch Pad Command & Read the data
        T_cTemp=ReadScratchPad();


        
        return T_cTemp;
   
        
    }
    return 1;
}

testtra.c

#include <REG51F.H>                /* special function register declarations   */
                                  /* for the intended 8051 derivative         */

#include <stdio.h>                /* prototype declarations for I/O functions */

#include <intrins.h>
#include"Temperature.h"
 
volatile char xdata cTemperature[7];
volatile unsigned char xdata j=0,i=0,k;
void delay();
volatile int flag=0;

void Comm1_WriteString(unsigned char *cString)
{
    while(*cString!='\0')
    {
        SBUF = *cString;   
        while(!TI);
        TI=0;
        cString++;
       
    }
    return;

void delay()
{   
    int i1;
    for(i1=0;i1<750;i1++)_nop_ ();
}

void delay2()
{
    int j;   
    for(j=0;j<40;j++)
    { 
            TMOD=0x21;   
            TL0=0xE0;
            TH0=0xB1;
            TR0=1;
            while(!TF0);
            TF0=0;
    }
}
void main (void)
{

    SCON  = 0x50;                /* SCON: mode 1, 8-bit UART, enable rcvr      */
    TMOD = 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload        */
    TH1   = 0XFD;                /* TH1:  reload value for 1200 baud @ 16MHz   */
    TR1   = 1;                  /* TR1:  timer 1 run                          */
    TI   = 1;                  /* TI:   set TI to send first char of UART    */          
    IT0 |= 0;   // Configure interrupt 0 for falling edge on /INT0 (P3.2)
    EA |= 1;    // Enable Global Interrupt Flag
    IE|=0X90;
//    delay2();
    TemperatureConversion();
    printf("Start\r");
    //Comm1_WriteString("Hello\r");

    while(1)
    {
       
        for(j=0;j<4;j++)
        cTemperature[j]=0;

        TemperatureConversion();
        delay();

        ReadTemperature();
        delay2();
    }
The Schematic Diagram of This Interfacing,


Comments

Popular posts from this blog

P89V51RD2 Interfacing with the UART Serial Communication

P89V51RD2 Interfacing with the UART Serial Communication Description                     Interfacing the UART Serial Communication with the P89v51rd2 Micro controller, with the baud rate of 9600bps. The Source code and Simulator Model of this Communication is listed Below. Source : #include <reg51.h> unsigned char var; unsigned char var1; void Serial_Init() {     TMOD = 0x20;     SCON = 0x50;     TH1  = 0xFD;     TR1   = 1;       } void Transmit_Char(unsigned char x) {     SBUF =  x;     while(!TI)     {     ;        }     TI = 0; } void Transmit_String(unsigned char *s) {     unsigned char i;     while((s[i]) != 0)     {         Transmit_Char(s[i]);         i++;     } } char Rcv() {         while(!RI)     {;}     RI = 0;     return SBUF; } void delay(unsigned int i) {     unsigned int j,k;     for(j=0;j<=i;j++)     {         for(k=0;k<256;k++);     } } void main(void) {     unsigned char r;     Serial_Init();     while(1)     {         Transmit_String("He

Interfacing UART with 8051

The interfacing of serial communication (UART) with 8051 micro controller, with the baud rate of 9600bps.  Code: #include <reg51.h> #include <stdio.h> unsigned char c; void Serial_Init() {     TMOD = 0x20;     SCON = 0x50;     TH1  = 0xFD;     TR1  = 1;     TI   = 1;     RI   = 0;       } void delay(unsigned int i) {     unsigned int j,k;     for(j=0;j<=i;j++)     {         for(k=0;k<256;k++);     } } void main(void) {     Serial_Init();     printf("Hello World \n");     while(1)     {                 delay(5);         c = getchar();         delay(5);         //putchar(c);         delay(5);            }                    } Design Proteus:
P89V51RD2 Interfacing with DS1307 RTC           The DS1307 RTC IC I nterfacing with P89V51RD2 Micro Controller use of the KEIL IDE, and proteus simulator. The source code and thhe screen short of simulator model are attached with this.              This interfacing is done by the I2C Communication between the Micro Controller and DS1307 RTC. Source Code: #include <reg51.h> #include <stdio.h> sbit SCL = 0x90; // SCL for I2C sbit SDA = 0x91; // SDA for I2C volatile unsigned char DS1307_WA=0xD0; volatile unsigned char DS1307_RA=0xD1; unsigned char Rdata; unsigned char sec1,min1,hour1,date1,month1,year1,d,l,m,j,i; unsigned char sec[20],min[2],hour[2],ch1; unsigned char RTCDateTime[15]; unsigned char Buf[20]; void Serial_Init() {     TMOD = 0x20;     SCON = 0x50;     TH1  = 0xFD;     TR1  = 1;     TI   = 1;     RI   = 0;       } void delay(unsigned int i) {     unsigned int j,k;     for(j=0;j<=i;j++)     {         for(k=0;