Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

pic16f876 and ds18b20 help.

Status
Not open for further replies.

sinatra2010

Newbie level 1
Joined
Jan 27, 2010
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,292
Hi,

i mesured a temperature using lm335 and i displayed to my 4 7segment digits.

Now i need to include my sencond digital sensor ds18b20 connected to RA0, i searched a help in the internet but i'm not found how to use this sensor but i see many make a fonction initds, readds, writeds, read temp but i'm not understand this fonctions and i found this ds1820 have a register with 9bytes
0 Temperature_LSB
1 Temperature_MSB
2 Temp Alarm High / User Byte 1
3 Temp Alarm Low / User Byte 2
4 Reserved
5 Reserved
6 Count_Remain
7 Count_per_C
8 CRC

for my project i will display only units and tenth, i use mplab and cc5x, please anyone can help me to write my code and understand how this ds18b20 work ?????

Added after 3 hours 7 minutes:

Code:
// bibliotheque de gestion de lecteur DALLAS
// Jean-Pierre MANDON

#define skiprom 0xCC
#define convert 0x44
#define readt 0xBE
#pragma bit dataline @ PORTC.0

void linein(void)
{
TRISC=0x81;
}

void lineout(void)
{
TRISC=0x80;
}

void tempo480(void)                   // temporisation de 480 µS
{
for (compteur=0;compteur<120;compteur++)
    {
    nop();
    nop();
    nop();
    }
}

void tempo65(void)                    // temporisation de 65 µS
{
for (compteur=0;compteur<19;compteur++);      // 21
}

char touchreset(void)                 // reset du bouton DALLAS et test de présence d'un bouton
{
dataline=0;                           // mettre la ligne à 0
tempo480();                           // pendant 480 µS
dataline=1;                           // puis remettre la ligne à 1
tempo65();                            // pendant 65 µS
linein();                             // passer la ligne en entrée
if (dataline==0)                      // si dataline=0, un bouton est la
   {
     tempo480();
     dataline=1;                      // repasser la ligne à 1
     lineout();                       // repasser la ligne en sortie
     return 1;                        // et renvoyer 1
   }
   else                               // sinon pas de bouton
   {
     tempo480();
     dataline=1;                      // repasser la ligne à 1
     lineout();                       // repasser la ligne en sortie
     return 0;                        // et renvoyer 0
   }
}

void touchbyte(char octet)            // envoi d'une commande au bouton DALLAS
{
  char rotation;
  char temporaire;
  char valeur;

for (rotation=1;rotation<9;rotation++)   // serialisation de la commande
    {
      temporaire=rotation-1;
      valeur=octet>>temporaire;          // rotation par la droite
      dataline=0;                        // mise à 0 de la ligne pendant
      nop2();                            // au moins
      nop2();                            // 1 µseconde
      if ((valeur&1)==1) dataline=1;     // test du bit
      else               dataline=0;     // et positionnement de la ligne
      tempo65();
      dataline=1;
      nop2();
      nop2();
    }
}

char touchreceive(void)               // reception d'un octet depuis le bouton DALLAS
{
 char rotation;
 char octet;
 char temporaire;
octet=0;
for (rotation=1;rotation<9;rotation++)
    {
      dataline=0;                     // mise à 0 de la ligne pendant
      nop2();                         // au moins
      nop2();                         // 1 µseconde
      dataline=1;
      linein();                       // passage de la ligne en entrée
      nop2();
      rotation--;
      temporaire=1;
      temporaire=temporaire<<rotation;
      rotation++;
      if (dataline==1) octet=octet|temporaire;
      dataline=1;
      lineout();
      tempo65();
    }
return octet;
}

char crc8(void)                           // vérification de la validité des octets reçus
{
char temp,index,compteur,n;
crc=0;
for (n=1;n<8;n++)
{
  index=dallastemp[n];
  for (compteur=1;compteur<9;compteur++)
    {
      #asm
      movf index,w
      xorwf crc,w
      movwf temp
      rrf temp,w
      movf crc,w
      btfsc STATUS,Carry
      xorlw 0x18
      movwf temp
      rrf temp,w
      movwf crc
      movf index,w
      movwf temp
      bcf STATUS,Carry
      rrf temp,f
      btfsc STATUS,Carry
      bsf temp,7
      movf temp,w
      movwf index
      #endasm
    }
}
if (crc==dallastemp[8]) return crc;
else                    return 0;
}

void convertion(void)
{
 int16 j;
 
if (touchreset()==1)                  // si un bouton est la
  {
    touchbyte(skiprom);               // passer la zone d'identification
    nop2();
    nop2();
    touchbyte(convert);               // démarrer la conversion
    for (j=1;j<200;j++) tempo480();
  }
}

char litcode(void)                    // lecture de la mémoire du bouton
{
  char i;
  int16 j;
  char resultat,retour;
if (touchreset()==1)                  // si un bouton est la
  {
    touchbyte(skiprom);               // passer la zone d'identification
    nop2();
    nop2();
lecture:
    touchbyte(readt);                 // alors aller lire
    for (i=1;i<10;i++)                 // les 2 octets
        {
          resultat=touchreceive();    // composants la zone mémoire
          dallastemp[i]=resultat;     // et les stocker dans le tableau dallas[] temporaire
        }
    resultat=0;                       // si tous les octets
    for (i=1;i<10;i++)                 // du code sont à 0
    resultat=resultat+dallastemp[i];  // alors le code n'est
    if (crc8()!=0)
       {
       for (i=1;i<10;i++)
           {
           resultat=dallastemp[i];
           dallas[i]=resultat;
           }
       retour=1;
       }
     else
       {
       retour=0;
       }
  }
  else
  retour=0;
return retour;
}

i found this code and i included to my programme for display temperture in my 4 digits 7 segment but i see always 00°C in my display

this is the main :
Code:
#pragma chip PIC16F876
#include "int16CXX.H"
static void SevenSegment(int n);
int digits[4];
unsigned char dcurr;
long    cntr ;
#pragma origin 4
interrupt interruption (void)
{
    int_save_registers    
    char sv_FSR = FSR; // save FSR if required
          if(T0IF)                         // timer 0 overflow ?
                {
                cntr++ ;                        // increment counter

                dcurr++ ;                       // next digit
                PORTB = 0 ;                     // turn digit off
               
                 if(dcurr == 4)                  // last digit ?
                        {
                        PORTC = 0b00000001 ;    // light the first digit
                        dcurr = 0 ;
                        }
                else
                        {
                        PORTC <<= 1 ;           // light the next digit
                        }

                SevenSegment(digits[dcurr]) ;           // assign 7 segments value
                FSR = sv_FSR; // restore FSR if saved
                T0IF = 0 ;               // done
                }
        
    
	int_restore_registers 
}
#define SEVEN_SEGMENT_PORT PORTB
#include "define.h"
#include "ds1820.c"
void SevenSegment(int n)
{
/*
n must be less than 9
*/
 if(n<20)
   {
      switch (n)
      {
         case 0:
         SEVEN_SEGMENT_PORT=0b00111111;
         break;

         case 1:
         SEVEN_SEGMENT_PORT=0b00000011;
         break;

         case 2:
         SEVEN_SEGMENT_PORT=0b01101101;
         break;

         case 3:
         SEVEN_SEGMENT_PORT=0b01100111;
         break;

         case 4:
         SEVEN_SEGMENT_PORT=0b01010011;
         break;

         case 5:
         SEVEN_SEGMENT_PORT=0b01110110;
         break;

         case 6:
         SEVEN_SEGMENT_PORT=0b01111110;
         break;

         case 7:
         SEVEN_SEGMENT_PORT=0b00100011;
         break;

         case 8:
         SEVEN_SEGMENT_PORT=0b01111111;
         break;

         case 9:
         SEVEN_SEGMENT_PORT=0b01110111;
         break;

         case 10:
         SEVEN_SEGMENT_PORT=0b01000000;
         break;

         case 11:
         SEVEN_SEGMENT_PORT=0b01110001;
         break;
         case 12:
         SEVEN_SEGMENT_PORT=0b00000000;
         break;
         case 13:
         SEVEN_SEGMENT_PORT=0b00111100;
         break;
      }

   }
}
 
void main(void)
{

     //Setup
 uns8 temperature,bb,cc;
 OPTION_REG = 0x80 ;                     // start timer 0, no prescaler
 INTCON = 0xA0 ;                         // allow timer 0 overflow interrupt
 TRISB = 0b00000000;
 TRISC = 0b00000000;
 PORTB = 0;
 PORTC = 0;
 TRISA=0x80;
 PORTA=0;

       while(1)
{  
         convertion();
          litcode();
         temperature=dallastemp[1];
          temperature/=2;

       bb=((uns8)temperature/10);
       cc=((uns8)temperature%10);

          digits[0]=bb;
          digits[1]=cc;
          digits[2]=11; 
          digits[3]=13;
 }
  
}


please any help.
 

Hi,
You may try looking at the mikroC compiler as it has a library routine made for One-Wire interface, where you just call the function to read, like
Code:
temp = OW_Read(...)
or
Code:
temp = OW_Write(...)
These are just examples. If you are interested, take a look at their site www.mikroe.com
Hope this helped.
Tahmid.
 

hi tahmid can u give me the program with mikrobasic to measure temp by using ds18b20 pls help me
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top