Temperature measurement using pic 16f877a and DS18S20

Status
Not open for further replies.

rubaan

Newbie level 2
Joined
Oct 22, 2013
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
14
I am using DS18S20 sensor and pic 16f877a for temperature measurement. I have c code that's for only one sensor. That works OK.
now i want to add four more sensors (same type) to get temperature reading from different points. Anyone could help me please? This is the code for only one sensor.

Code:
#include<stdio.h>
#include<pic.h>
#include "delay.c" __CONFIG(0x3f72);
#define DQ         RC0
#define DQ_DIR     TRISC0
#define FOSC       10000
#define BAUD_RATE  9.6      // 9600 Baudrate
#define BAUD_VAL   (char)(FOSC/ (16 * BAUD_RATE )) - 1;     
// Calculation For 9600 Baudrate @10Mhz
unsigned char i=0;
float Temerature=0;
void ds1820_init();
void Reset(void);
void write(unsigned char);
unsigned char Read(void);
void Serial_init();
void main()
{
   unsigned char Temp[9];
   DelayMs(100);
   Serial_init();
   ds1820_init();
   DelayMs(100);
   printf("\033[2J");   // Clear the Hyper terminal;
   while(1)
   {
      Reset();
      write(0xcc);
      write(0x44);
      DQ_DIR    = 1;
      DelayUs(10);
      while(!DQ);
  // this will be raised after finishing conversion
      Reset();
      write(0xcc);
      write(0xbe); 
      for(i=0;i<9;i++)
      Temp[i] = Read();      // Read 9 bytes 
      for(i=0;i<9;i++)
      printf("%d ",Temp[i]);    
      printf("Temperature:%3.1f%cC",(float)Temp[0]/2,0xf8);
      printf("\r");
      DelayMs(250);
      DelayMs(250);
      DelayMs(250);
      DelayMs(250);
   }
}
 
void ds1820_init()
{
      DQ    = 1;
      DQ_DIR = 1;         // pull up
}
 
void Reset(void)
{
      DQ_DIR    = 0;
      DQ     = 0;
      DelayMs(1);
      DelayUs(250);      // 500us
      DQ_DIR    = 1;
      DelayUs(100);      // 40us
      while(DQ==1);      // wait until presence pulse
      DelayMs(1);
      DelayUs(250);      // 500us
}
 
void write(unsigned char cmd)
{
      for(i=0;i<8;i++){
      DQ_DIR   = 0;                // pull down
      DQ     = 0;
      DelayUs(25);                 // 10us
      DQ       = (cmd & 0x01)?1:0; // Send bit
      cmd      = cmd >> 1;
      DelayUs(120);                // >45us
      DQ_DIR   = 1;                // release
      }     
}
 
unsigned char Read(void)
{
      unsigned char temp=0,RecDat=0;
      for(i=0;i<8;i++)
      {
         DQ_DIR   = 0;
         DQ     = 0;        // pull down
    DelayUs(25);       // 10us
    DQ_DIR   = 1;      // release
        DelayUs(25);       // 10us
        temp   = DQ;       // read bit
        temp   = temp<<i;
       RecDat |= temp;
        DelayUs(70);       // 30us
      }
      return RecDat;     
}
void Serial_init()
{
      TRISC=0xc0;        // RC7,RC6 set to usart mode(INPUT)
      TXSTA=0x24;        // Transmit Enable
      SPBRG=BAUD_VAL;    // 9600 baud at 10Mhz
      RCSTA=0x90;  // USART Enable, Continuous receive enable
      TXIF=1;            // Start Transmit
}
 
void putch(unsigned char Data) // data TX required for printf
{
         while(TXIF==0);
         TXREG = Data;
}
 
Last edited by a moderator:

if u have any circuit di
agram or Proteus file then post.

and by the way if your program is working then try to same configuration using different pin of microcontroller and write code after post query if any..
 

Hi,

You can use multiple sensors in two ways, either each sensor on its own input port or all the sensors on one input port; hence those sensors being called 'one wire'

However to use all the sensors on one wire you need to individually address each sensor which complicates the code a bit more.

Also be aware that the DS18S20 while not obselete is considered an old part and the new DS18B20 is prefered for new designs
 
Just modify the code so the 'skip ROM' command is changed to 'Match ROM' and supply it with the address of the sensor you want to use. You can wire several sensors in parallel and only the one with matching address will reply.

Brian.
 

Thanks a lot. First, does this code works for DS18B20. Then, does the same procedure you mentioned is ok for DS18B20 ?
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…