I have a small 1-wire network running different sensors. The network is connected to a linux box. I needed to add a humidity sensor to the network. There are plenty of different solutions but I found this project that was using PIC microcontroller as a bridge between 1-wire network and Sensirion SHT11 sensor. I thought it was a great idea and also, as I had no experience with μC, a great way to learn something new. My idea was also that this 1-wire slave could serve for communication with other devices using different protocols.
The project that I found was a closed source one, so I decided I should write my own program and make my first μC device. I chose PIC16F84A for my design, as it was cheap, easy to get and easy to program. As I had 10 MHz crystal I decided that I will try to make it work at that frequency, although this chip allows crystals up to 20 MHz. I bought a few needed parts according to PIC datasheet and built a very simple device.
SCHEMATIC
PIC works in its basic environment: X1 is 10 MHz quartz, C1 and C2 are 27 pF capacitors; MCLR is connected to VDD through R1=10 kΩ resistor; additionally PIC is decoupled with C3=100 nF. Connecting sensors requires a few additional components: two pullup resistors R2=R3=10 kΩ (R2 is optional, required only with SHT2x) and decoupling capacitor C4=100 nF. Operating voltage for SHT1x is 2.4–5.5 V, while for SHT2x it is 2.1–3.6 V (with 5 V described in datasheet as an absolute maximum). I am powering both SHT11 and SHT21 with +5 V and it works ok, but it is probably too much for the latter. I plan to lower supply voltage of SHT21 to 3.3 V. I connect sensor to pins RB4 and RB5 but it may be changed in source file to any other I/O port except RB0/INT which must be reserved for 1-wire data line.
ASSEMBLED BOARD
I decided I should try to program it in C (although it would probably be much more efficient in assembler). I needed to find a free tool and after some digging I found Hitech PICC compiler which even offered me full code optimization during evaluation period. As I have limited access to machines running MS Windows I was using rather weird set of tools. I was running PICC compiler on OSX writing code in Xcode. I programmed my chip using very cheap programmer JDM 2. It was connected to serial port of a linux machine and driven by WinPic program under wine.
The biggest challenge was to keep up with the master. Each 1-wire operation is initiated by master device pulling the bus low and timings are very tight. If I am not wrong in my calculations my microcontroller running at 10MHz performs 2.5 instructions per microsecond. For example while master reads byte from a slave, the time between master initiates the operation by pulling bus low and the time when master is probing line for 0 or 1 state is 15 μs. We are thus limited to a little more then 30 instructions which should be ok, but as there are many conditions to check on the way and we deal with a poorly optimized C code… Anyway, after lots of debugging it started working and here are results of my work.
Owslave supports following 1-wire commands
ROM commands:
Implemented ROM commands should make it compatible with any 1-wire software. I was testing it with owfs and digitemp on linux and osx as well as with Maxim 1-wire java tools. I use DS9097U compatible interface between PC and 1-wire network. Regarding humidity sensors I tested it with SHT11 and SHT21, as these are the only ones I have. UPDATE: A user reported that he uses owslave sucessfully with SHT71.
The algorithm is very simple: 1-wire master on PC detects owslave (through search ROM procedure) and sends it "convert" command. This makes owslave send analogous command to sensor and read back measurements into its scratchpad. After a while 1-wire master sends "read scratchpad" command and fetches measurments from the slave. It gets raw results from the sensor and all the calculations and crc checking must be done in master software. Owslave returns 7 bytes:
I patched digitemp so that it natively recognizes 1-wire slave emulator by its family code (I chose 0xBF), gets results from sensors, do all the crc checking and calculations. I also patched owfs to support the device, although it was just for testing, as I don't use this soft.
Currently owslave supports two types of sensors:
The code for contacting SHT sensors was adopted from samples published on Sensirion webpage. It should be easy to adapt owslave to any other devices communicating with I2C protocol as all the basic functions are already included. Owslave design also facilitates adding support for other devices.
While returning readings from attached sensors to 1-wire master owslave sends also sensor code so that 1-wire master software knows what kind of data to expect. Due to limited program space on PIC16F84A (1024 words) there may be only one type of supported sensor compiled into hex file. That means that one must take a decision which sensor will be be used with given device at the stage of compilation.
Digitemp output:
OWFS output (also gives info on type of connected sensor):
Compiling under picc compiler:
Changelog
Downloads
Disclaimer