Working Example: Digital Temp Sensor DS18B20 and PIC16FXXX

Discussion in 'Embedded Systems and Microcontrollers' started by Litch, Mar 23, 2013.

  1. Litch

    Thread Starter Member

    Jan 25, 2013
    86
    7
    Thanks to some help on this forum my first PIC project is up and running!

    Time to pay the piper; here's a schematic and source code with routines for RS232 and the Maxim 1-wire protocol for the DS18B20.

    The project has the ability to support 1 or more DS18B20 digital temp ICs on a single 1-wire bus, with output to PC on RS232 (via MAX232IN).

    Hope this helps someone :)

    Specific Components I Used:
    * PIC16F690
    * DS18B20
    * MAX232IN

    PS: CRC8 Routine for Maxim iButton in C:

    Code ( (Unknown Language)):
    1. // Running CRC routine
    2. uint8_t crc8(uint8_t crc, uint8_t data)
    3. {
    4.     uint8_t i;
    5.     crc = crc ^ data;
    6.     for (i = 0; i < 8; i++)  {
    7.         if (crc & 0x01) {
    8.             crc = (crc >> 1) ^ 0x8C;
    9.         } else {
    10.             crc >>= 1;
    11.         }
    12.     }
    13.     return crc;
    14. }
    15.  
    16. // Parent function
    17. uint8_t crc_calc(uint8_t *p, int length)
    18. {
    19.     uint8_t crc = 0;
    20.     while (length--) {
    21.         crc = crc8(crc, *p++);
    22.     }
    23.     return crc;
    24. }
     
    absf likes this.
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    Nice job but you should consider posting this in the Completed Projects section. It can be found more easily there.
     
  3. Litch

    Thread Starter Member

    Jan 25, 2013
    86
    7
    Ah, didn't realise there was a "Completed Projects" section... Will know for next time.
     
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    Not too late. You can copy it there. Many people do. If I remember correctly, you can't. edit your op in that forum so what people do is post elsewhere, get their post together then copy to the forum.
     
  5. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    (1) You didn't mention what version of C you're using...

    (2) While you have a searchrom() routine, you don't seem to be using it in your example program...

    (3) Please show an example of the output... It seems like you're just sending the "raw" 8 byte scratchpad data...
     
    Last edited: Mar 23, 2013
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Good job, congratulations on getting this all to work.

    Did you happen to take any pictures of the build itself?
     
  7. Litch

    Thread Starter Member

    Jan 25, 2013
    86
    7
    Any recent HI-TECH C should do the job, though the specific version I used was:

    HI-TECH C Compiler for PIC10/12/16 MCUs (PRO Mode) V9.83

    The ds18b20_searchrom() function is by example how one could evaluate each device on the 1-wire bus.

    It is used, Line 25, tempmon.c - it would simply dump some text and the ROM codes to serial - if one was to connect the project to a PC, they could easily use putty or minicom to view the serial data received.

    I think it's fairly obvious what it does and what it can do, if you follow the source and the comments you'll see that ds18b20_searchrom() will spit out "SEARCHING...\r\n" to serial. Then it uses rs232_puthex() to convert each byte of every device's ROM code to textual hex, ie. a value of 254 will yield the _characters_ "FE" on the serial bus. It will finish each 8-byte rom code with "\r\n" and then spits out "DONE\r\n" once it cannot find any more devices on the bus.

    I'm not saying it's the be all and end all of PIC+DS18B20 projects (being my first go at PIC) but functionally I believe it has the foundation there to allow you to throw together something by just changing int main().

    Thanks Ernie, as for the "build", well, let's say it involves half a hacked PCB from another project for the 5v PSU, a PICkit2 demo board for the PIC, a faulty breadboard for the 1-wire bus and a few DS18B20's, and a strip-board with salvaged 1uF caps from a blown ATX psu and a MAX232IN I had laying around.

    It's a resourceful effort and I've uploaded the proto if you want a laugh (Apologies if the annotations are too small to read), but the final PCB is currently pending photo paper and some etch solution :)

    -KB
     
  8. Litch

    Thread Starter Member

    Jan 25, 2013
    86
    7
    Example PCB with ICSP, first draft.
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    No laughter from here. It's always nice to see someone use what's available to create just what is needed.
     
Loading...