Thursday, December 26, 2013

Interfacing DS18s20 with AT89s52


In the previous post, we interfaced an analog temperature sensor i,e LM35 .Today, its time to interface DS18s20 with AT89s52 ,a microcontroller from 8051 family which is an digital temperature sensor of 9 bit resolution.

DS18s20 is a digital temperature sensor which uses maxims 1 wire bus protocol for communication which means only data pin of DS18s20 needs to be connected with the processor(master) for both data and control signals.This temperature sensor can measure temperature from -55°C to +125°C with 0.5°C step.The conversion time is 750ms(max) and 500ms(min). Also the device has user definable nonvolatile alarm setting i,e an alarm flag is set if the measured temperature is lower than or equal to TL or higher than TH.For each DS18s20, a unique 64 bit serial code(device address) is stored in on board ROM.This allows multiple devices (DS18s20) to communicate on same one wire bus with the master.In addition, no external power source is required to power up DS18s20. Instead the power required for DS18s20 to operate can be obtained from the data line in case of measurement of temperature below 100°C and this mode of operation is called parasite power mode.

Block diagram of DS18s20:-

ds18s20's block diagram
fig a:- Block diagram of DS18s20
As shown in the block diagram, DS18s20 consists of:-
  • 64 bit ROM that stores the devices unique serial code.
  • The scratchpad register which is shown in the figure b.
  • TH and TL registers which are non volatile and are user definable used for alarm signalling.
  • 8-bit CRC generator for Cyclic Redundancy Check.
sscratchpad register of ds18s20
fig b:- scratchpad register

    Measuring temperature with 9-bit resolution:-

    With 9-bit resolution, the temperature is measured with 0.5°C steps.After powering up the device to initiate a temperature measurement and analog to digital conversion, the controller(master) must issue a convert T command. After the completion of conversion the resulting thermal data is stored in 2-byte temperature register in the scratchpad memory. For the identification of completion of conversion when external power source is used, the controller(master) can issue read time slot after convert T command and the DS18s20 will respond by transmitting 0 while the conversion is in progress and 1 when the conversion is completed but if parasitic mode is used, we will need to wait for maximum conversion time i,e 750ms after issuing the convert T command.


    Measuring temperature with greater than 9-bit resolution:-

    For measurement of temperature upto 12-bit resolution, the data from 2-byte temperature register, count_remain, count_per_°C from scratchpad register is required. Then we use the following formula to calculate extended resolution temperature.

             Temperature = Temp_read-0.25 + (Count_per_°C-Count_remain) / (Count_per_°C)

     The data from 2-byte temperature register is read and its Least significant bit is removed. This value is Temp_read.

    The temperature data is stored in 16-bit sign extended two's complement number in the temperature register as shown in the figure c. From this figure we can see that the most significant (sign) bit is duplicated into all of its bits in the upper MSB of 2-byte temperature register in scratchpad memory.

    2-byte Temperature register from scratchpad of DS18s20
    fig c: 2-byte temperature register

    1-wire bus system:-

    1-wire bus system is broken into:-
    • Hardware configuration
    • Transaction sequence
    • 1-wire signalling

    Hardware sonfiguration:-

    DS18s20 consists of 3 pins which are GND, DQ and Vdd respectively. DQ is single data line available in DS18s20 and is open drain. Thus,1-wire bus in DS18s20 requires pull up resistor of 5K and hence the idle state for 1-wire bus is high.This DQ line is connected with any one GENERAL PURPOSE INPUT OUTPUT PIN of 8051.GND and Vdd are connected to ground and external power source ( if device not operated in parasitic mode ) respectively.
    hardware connection of DS18s20
    fig d:- hardware configuration
    The schematic used in interfacing DS18s20 with 8051 microcontroller is shown in figure below.
    Schematic for interfacing DS18s20 with 8051
    fig:- Schmatic for interfacing DS18s20 with 8051

    Transaction sequence:-

    The protocol for accessing DS18s20 via 1-wire port is as follows.
    • Initialization 
    • ROM function command
    • Memory (DS18s20) function command

    Initialization:-

    All transaction on 1-wire bus begins with an initialization sequence which consists of reset pulse transmitted by controller (master) followed by the presence pulse transmitted by DS18s20's(slaves). Presence pulse lets the controller(master) know that DS18s20's (slaves) are on bus and are ready to operate.

    ROM function command:-

    table of ROM command
    fig e:-ROM command
    After detection of presence pulse by the controller, it can issue ROM commands which are 8-bit long. There are five ROM commands and these commands allow the master to determine how many and what type of devices are present on the bus and if any device has experienced an alarm condition.In the table alongside, we can see the ROM commads.

    Memory(DS18s20) function command:-

    DS18s20 function commands allows the master to configure and communicate with sensors. The DS18s20 function command set with the description of each is shown in the figure below.
    Function commands of DS18s20
    fig f:- DS18s20 function command

    1-wire signalling:-

    Reset pulse, presence pulse, write 0, read 0, write 1, write 0 are several type of signalling defined by this protocol. Except presence pulse, all signals are initiated by the master.

    Initialization sequence:-

    As shown in figure g, the bus master transmits reset pulse by pulling the 1-wire bus low for minimum of 480µs. The bus master then releases the bus and goes in receiving mode. When the bus is released the pull up register pulls the 1-wire bus high. When DS18s20 detects this rising edge, it waits for 15µs to 60µs and then transmits the presence pulse by pulling the  1-wire bus low for 60µs to 240µs.

    timing diagram of initialization sequence of DS18s20
    fig g:- Initialization sequence

    The routine to implement initialization sequence is below.


    bit ds1820init()

    {

    unsigned char presence;

    DQ=1;

    _nop_();

    DQ=0;
    delay(60);
    DQ=1;
    delay(2);
    presence = DQ;
    delay(50);
    return presence;
    }

    Write time slot:-

    DS18s20 initiates the Write time slot by pulling the data line from high logic level to low logic level. Write 1 and Write 0 are two Write time slot in DS18s20. It must be minimum of 60µs duration with minimum of 1µs recovery time between individual write time slots.As shown in figure h, DS18s20 samples the 1-Wire bus during a window that lasts from 15μs to 60μs after the master initiates the write time slot. If the bus is high during the sampling window, a 1 is written to the DS18S20. If the line is low, a 0 is written to the DS18S20.

    timing diagram of write time slot
    fig h:- Write time slot
    To generate write 0 time slot, the master must release 1-wire bus within 15µs of pulling it low. When bus is released, external pull up resistor pulls the bus high.
    To generate write  time slot master must continue to remain low for 60µs.
    Here is the routine for its implementation.

    void writeslot(bit x)
    {
    DQ=0;
    if(x)
    DQ=1;
    delay(6);
    DQ=1;
    }

    To write a single byte through 1-wire, the routine is shown below.

    void writebyte(unsigned char dat)
    {
    unsigned char i;
    for(i=0;i<8;i++)
    {
    writeslot(dat & 0x01);
    dat>>=1;
    }
    }

    Read time slot:-

    Read time slot is generated by master if data is to be read from scratchpad of DS18s20. It is initiated by master by pulling 1-wire bus low for minimum of 1µs and then releasing the bus. After initiating read time slot, DS18s20 transmits 1 by leaving the bus high and transmits 0 by pulling the bus low.The Read time slot timing diagram is shown in figure i.
    timing diagram of Read time slot
    fig i:- Read time slot
    The routine for implementation of read time slot is shown below

    bit readslot()
    {
    DQ=0;
    _nop_();
    DQ=1;
    return(DQ);
    }

    To read a byte from DS18s20 following routine can be used.

    unsigned char readbyte()
    {
    unsigned char i=0,dat=0;//variable to count and store data
    for(i=0;i<8;i++)
    {
    dat>>=1;
    if(readslot())
    {
    dat|=0x80;
    }
    delay(6);
    }
    return dat;
    }
    .

    The step by step process for temperature conversion and reading scratchpad register when 1 DS18s20 and external power supply is used is shown in the table below.
    algorithm for conversion and reading scratchpad memory
    fig j:- Algorithm for conversion and reading scratchpad registers
    The routine to implement the table shown in figure j is below.

    void readtemp()
    {
    ds1820init();
    writebyte(skip_rom);
    writebyte(convert_temp);
    while(readbyte()!=0xff);
    ds1820init();
    writebyte(skip_rom);
    writebyte(read_scratchpad);
    temp[0]=readbyte();
    temp[1]=readbyte();
    }

    After reading the first two scratchpad register as shown in above routine, it is required to convert the binary value stored in those registers(here temp[0] and temp[1]) into suitable form such that it can be displayed in LCD.
    NOTE:- The routine presented above is written in keil for 8051 microcontroller. 11.0592 MHZ crystal is used to provide required clock for operation.

    The complete source code written in C and proteus simulation file can be downloaded by clicking here.

      No comments: