anyone got any good examples of using a ds1820?

Discussion in 'Embedded Systems and Microcontrollers' started by bluebrakes, Jun 18, 2010.

  1. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    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.
     
  2. coldpenguin

    Active Member

    Apr 18, 2010
    165
    9
    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.
    Code ( (Unknown Language)):
    1.  
    2.  #define TA 7
    3.  #define TB 70
    4.  #define TC 65
    5.  #define TD 12
    6.  #define TE 11
    7.  #define TF 60
    8.  #define TG 0
    9.  #define TH 500
    10.  #define TI 77
    11.  #define TJ 450
    12.  
    13.  #define TPIN TRISD7
    14.  #define DPIN RD7
    15.  bit prescence = 0;
    16.  
    17.  void One_Wire_High(){
    18.  DPIN=1;
    19.  TPIN=1;
    20.  }
    21.  
    22.  void One_Wire_Low(){
    23.  TPIN=0;
    24.  DPIN=0;
    25.  }
    26.  bit Get_One_Wire_Bit(){
    27.  TPIN=0;
    28.  char a=DPIN;
    29.  TPIN=1;
    30.  return a;
    31.  }
    32.  
    33.  int One_Wire_Get_Byte()
    34.  {
    35.     unsigned char n, i_byte, temp, mask;
    36.     for (n=0; n<8; n++)
    37.     {
    38.        TPIN=0;
    39.        DPIN=0;
    40.    __delay_us(TA);
    41.   TPIN=1;
    42.   __delay_us(TE);
    43.  
    44.        temp=DPIN;
    45.        if (temp==1)
    46.        {
    47.          i_byte=(i_byte>>1) | 0x80; // least sig bit first
    48.        }
    49.        else
    50.        {
    51.          i_byte=i_byte >> 1;
    52.        }
    53.        __delay_us(TF);
    54.     }
    55.     return(i_byte);
    56.  }
    57.  
    58.  
    59.  bit One_Wire_RS(void){  
    60.  One_Wire_High();
    61.  __delay_us(TG);            
    62.    prescence = 0;                    
    63.  One_Wire_Low();
    64.    __delay_us(TH);              
    65.  One_Wire_High();
    66.  __delay_us(TI);          
    67.  if (Get_One_Wire_Bit() == 0 ){
    68.  prescence=1;
    69.  };
    70.  
    71.   __delay_us(TJ);
    72.  return prescence;
    73.  }
    74.  void One_Wire_Out_Bit(int a){
    75.  if(a==1){
    76.  One_Wire_Low();
    77.  __delay_us(TA);
    78.  One_Wire_High();
    79.  __delay_us(TB);
    80.  }else{
    81.  One_Wire_Low();
    82.  __delay_us(TC);
    83.  One_Wire_High();
    84.  __delay_us(TD);
    85.  }
    86.  }
    87.  
    88.  
    89. void ()main code{
    90.  
    91.  One_Wire_RS();
    92.  
    93.  if(1 == One_Wire_RS()){
    94.  One_Wire_Out_Byte(0x33);
    95.  for(n=0; n<8; n++)
    96.     {
    97.        adr[n]=One_Wire_Get_Byte();
    98.     }
    99.  char crc=One_Wire_Get_Byte();
    100.  
    101.  
    102.  One_Wire_RS();
    103.  
    104.  One_Wire_Out_Byte(0x55);
    105.  for(n=0; n<8; n++) // followed by the 8-byte ROM address
    106.     {
    107.        One_Wire_Out_Byte(adr[n]);
    108.     }
    109.  One_Wire_Out_Byte(0x44);
    110.  
    111.  __delay_us(90);
    112.  One_Wire_RS();
    113.  One_Wire_Out_Byte(0x55);
    114.  for(n=0; n<8; n++) // followed by the 8-byte ROM address
    115.     {
    116.        One_Wire_Out_Byte(adr[n]);
    117.     }
    118.  One_Wire_Out_Byte(0xbe);
    119.  for(n=0; n<9; n++)
    120.     {
    121.        d[n]=One_Wire_Get_Byte();
    122.     }
    123.  mytemp=d[0];
    124.  d[0]>>=1;
    125.  
    126. //d[0] is now the temperature, and bit0 of mytemp defines whether you add 0.5 to it.
    127. }
    128.  
    129.  
    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
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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 :)
     
  4. coldpenguin

    Active Member

    Apr 18, 2010
    165
    9
    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.
     
  5. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    i'm using mplabs but also a few c compilers should that help...
    sorry it's the ds18b20 i'm using too.
     
  6. coldpenguin

    Active Member

    Apr 18, 2010
    165
    9
    Well, in theory the code I put up should be pretty close to working, the scratchpad is different though I think.
     
  7. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    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
     
    Last edited: Jun 18, 2010
  8. rajbex

    New Member

    Nov 2, 2009
    22
    0
Loading...