So yesterday I wanted to get some temperature measurements from a DS18S20 thermometer to my PIC prototyping board. This thermometer uses the 1-Wire communication protocol so I searched around to find a 1-Wire library for the C18 compiler I am using. Maybe I am wrong but I couldn’t find any. So I created one, hence this post.
I had a post about the 1-Wire protocol a while back so you can read that if you are not familiar with it. To implement this protocol we need to work with precision timing. Ideally this could be written in asm. However, for convenience reasons I wrote this in C. I don’t really mind the minor performance penalty
Just to be clear, this is a library for the 1-Wire protocol, not for any of the supported devices. The library contains 3 main functions:
- 1-Wire Reset
- 1-Wire Write
- 1-Wire Read
Using these three operations we can have full communication with any 1-Wire device. Let me demonstrate first and then I will write about how you can use it in your project.
A brief demonstration
To demonstrate this operation, I connected a DS18S20 thermometer and connected the DQ line on my PIC’s Port C Pin 1. The procedure I will follow is:
- Issue a Reset pulse and observe the Presence of the thermometer
- Issue the Skip Rom command (0xCC)
- Issue the Convert T command (0×44)
- Wait for 1+ second
- Issue a Reset pulse and observe the Presence of the thermometer
- Issue the Skip Rom command (0xCC)
- Issue the Read Scratchpad command (0xBE)
- And read the next two bytes which represent the temperature
Lets see the C code I loaded on my board
#pragma config OSC = HSPLL #pragma config WDT = OFF #pragma config PWRT = ON #pragma config LVP = OFF #pragma config PBADEN = OFF #define OW_LAT LATCbits.LATC1 #define OW_PIN PORTCbits.RC1 #define OW_TRIS TRISCbits.TRISC1 #include <p18cxxx.h> #include <p18f4520.h> #include <delays.h> #include "ow.h" void main(void) { char testdata1,testdata2; TRISCbits.TRISC0=0; LATCbits.LATC0 = ow_reset(); Delay100TCYx(30); ow_write_byte(0xCC); Delay100TCYx(1); ow_write_byte(0x44); Delay10KTCYx(255); Delay10KTCYx(255); ow_reset(); Delay100TCYx(30); ow_write_byte(0xCC); Delay100TCYx(1); ow_write_byte(0xBE); Delay100TCYx(1); testdata1=ow_read_byte(); Delay10TCYx(1); testdata2=ow_read_byte(); ow_reset(); Delay10KTCYx(255); ow_write_byte(testdata1); ow_write_byte(testdata2); while(1){} return; }
Don’t mind the last couple of commands , they are not needed, I will explain later. Notice the required delays between the issuing of commands. To observe what is going on (and to show you) between the PIC and the thermometer I used this little logic analyzer which I fully recommend. It’s cheap and really useful. Lets see the screenshot
The screenshot shows the second part of the demonstration where we read the temperature from the DS18S20. You can see the that all starts with the Reset pulse and the successful response from the thermometer using the Presence pulse. After while the 0xCC (Skip Rom) and 0xBE (Read Scratchpad) commands are issued. Just after that we issue the Reading phase where the thermometer successfully responds. It responds with 0x3D and 0×00 which if I am right converts to 30.5 degrees Celsius (it a bit high because I was holding the thermometer in my palm).
So we proved that Reset and Write commands work !!! Its time to prove that the Read commands also work !!! and that is where the last two commands come in. I just used them there to spit out the temperature that was read earlier just to check that the function read them correctly. See the screenshot
As you can see the PIC write the 0x3D and 0×00 back so we confirm that reading works!
That’s it
How you can use it
Go and download the library. I put it on GitHub. Put the ow.h file in your project folder where you other C files are located. Then include the file using
#include "ow.h"Then you need to define three things:
- The Latch address
- The Pin address
- and the Tris address
See how I defined them in the code above, for the Pin 1 of Port C.
And that’s it. You can use the commands to communicate via 1-Wire
Enjoy and let me know if you need any help.
Related Posts:
Incoming search terms:
- pic onewire library (6)
- c18 ds1820 (5)
- 1-wire c18 (5)
- OW_write_byte (5)
- onewire c18 (5)
- c18 libraries (5)
- ds1820 pic 1 wire c18 (4)
- onewire c (3)
- pic ds18s20 prototype (2)
- pic DS18S20 (2)

