anyone got any good examples of using a ds1820?

Thread Starter

bluebrakes

Joined Oct 17, 2009
252
I'm looking to make a dual thermometer for my car's intercoolers, so i can monitor intake temp, etc...

i would like to use a pic 16f628 for this and I just can't seem to find an example with decent source code, etc...

does anyone have any suggestions?

thanks.
 

coldpenguin

Joined Apr 18, 2010
165
What language are you writing it in? Which compiler?
The basic code you would need for 1 1-wire device is below, you would need to implement a search algorithm if desired to have two on the same bus (read the datasheet for the DS1820). I am afraid that I don't comment my code when I program, but the below should be relatively straight forward. It works with the hitech compiler. The timing defines might need to be changed if you are not using a 48MHz crystal. It will reset the bus, send out a request rom, read the rom address in then reset the bus. It will then perform a match rom, start calculation and then pause for 90us. This should be enough if not using parasitic power. It then performs a reset, match rom, then read scratchpad.
Rich (BB code):
 #define TA 7
 #define TB 70
 #define TC 65
 #define TD 12
 #define TE 11
 #define TF 60
 #define TG 0
 #define TH 500
 #define TI 77
 #define TJ 450
 
 #define TPIN TRISD7
 #define DPIN RD7
 bit prescence = 0; 
 
 void One_Wire_High(){
 DPIN=1;
 TPIN=1;
 }
 
 void One_Wire_Low(){
 TPIN=0;
 DPIN=0;
 }
 bit Get_One_Wire_Bit(){
 TPIN=0;
 char a=DPIN;
 TPIN=1;
 return a;
 }
 
 int One_Wire_Get_Byte()
 {
    unsigned char n, i_byte, temp, mask;
    for (n=0; n<8; n++)
    {
       TPIN=0;
       DPIN=0;
   __delay_us(TA);
  TPIN=1;
  __delay_us(TE);
 
       temp=DPIN;
       if (temp==1)
       {
         i_byte=(i_byte>>1) | 0x80; // least sig bit first
       }
       else
       {
         i_byte=i_byte >> 1;
       }
       __delay_us(TF);
    }
    return(i_byte);
 }
 
 
 bit One_Wire_RS(void){   
 One_Wire_High(); 
 __delay_us(TG);             
   prescence = 0;                     
 One_Wire_Low();
   __delay_us(TH);              
 One_Wire_High();
 __delay_us(TI);           
 if (Get_One_Wire_Bit() == 0 ){
 prescence=1;
 };
 
  __delay_us(TJ);
 return prescence;
 } 
 void One_Wire_Out_Bit(int a){
 if(a==1){
 One_Wire_Low();
 __delay_us(TA);
 One_Wire_High();
 __delay_us(TB);
 }else{
 One_Wire_Low();
 __delay_us(TC);
 One_Wire_High();
 __delay_us(TD);
 }
 }
 
 
void ()main code{
 
 One_Wire_RS();
 
 if(1 == One_Wire_RS()){
 One_Wire_Out_Byte(0x33);
 for(n=0; n<8; n++)
    {
       adr[n]=One_Wire_Get_Byte();
    }
 char crc=One_Wire_Get_Byte();
 
 
 One_Wire_RS();
 
 One_Wire_Out_Byte(0x55);
 for(n=0; n<8; n++) // followed by the 8-byte ROM address
    {
       One_Wire_Out_Byte(adr[n]);
    }
 One_Wire_Out_Byte(0x44);
 
 __delay_us(90);
 One_Wire_RS();
 One_Wire_Out_Byte(0x55);
 for(n=0; n<8; n++) // followed by the 8-byte ROM address
    {
       One_Wire_Out_Byte(adr[n]);
    }
 One_Wire_Out_Byte(0xbe);
 for(n=0; n<9; n++)
    {
       d[n]=One_Wire_Get_Byte();
    }
 mytemp=d[0];
 d[0]>>=1;
 
//d[0] is now the temperature, and bit0 of mytemp defines whether you add 0.5 to it.
}
I should warn you though, for some reason, running One_Wire_RS() in my code, is setting RD6 to be low output. I am using a 16f877a
 

t06afre

Joined May 11, 2009
5,934
The DS1820 is no longer in production, but the DS18S20 is. Just as you know it. I also think you will find something if you Google -> ds18s20 microchip Or something like it. The method used for data transfer is named bit-banging I think.
Good luck :)
 

coldpenguin

Joined Apr 18, 2010
165
Some of us still have a stock of DS1820 to get through though, before we move onto the flashy DS18b20 or the DS18s20 with their extra bits of precision.
 

MMcLaren

Joined Feb 14, 2010
861
Yeah, they're different. The DS1820 and DS18S20 produce a 9-bit temperature result (0.5°C resolution) where the DS18B20 produces a 12-bit temperature result (0.0625°C resolution).

I posted a 16F690 + 2x16 LCD + DS18B20 (assembly language) example on another Forum recently (below). You would have to make hardware and software changes for a '628A and dual sensors.

You should be able to find several examples on the Internet too.

Regards, Mike
 

Attachments

Last edited:
Top