PIC Functions

The PIC16F88 I selected for this project has several features that were an excellent fit for the functions needed for the OpenTherm gateway. The PIC16F1847, that was chosen as a replacement when there was a shortage of the PIC16F88, has very similar features.


The information on the OpenTherm connection is communicated using different voltage and current levels. Somehow these analog properties have to be converted to zeroes and ones. On the boiler interface an opto-coupler will be used to switch on when the current exceeds some minimum level. Distinguishing the voltage levels on the thermostat interface can be done using one of the two analog comparators of the PIC. Because the levels used in the OpenTherm protocol are a little too high to use directly, a voltage divider is used to bring the voltage down.

I still decided to use the second comparator to monitor the output signal of the opto-coupler because one of the available comparator modes (CM2:CM0 = 110) connects the outputs of the two comparators to two output pins. By using the output pin of the first comparator to control the output driver for the boiler connection, bits coming in from the thermostat will automatically be forwarded to the boiler. In the reverse direction the same effect can be achieved by using the output pin of the second comparator to control the output driver for the thermostat connection. And if automatic forwarding of the messages is not desired, another comparator mode (CM2:CM0 = 011) disconnects the comparator outputs from the output pins.

The signals from the interface circuitry are connected to the inverting inputs of the comparators. This means that the comparators produce a logical 1 when the level on the line is low and a logical 0 for a high level on the line. This is perfect, because the line drivers connected to the output pins also invert the signal. A logical 1 on an output pin sends a low level onto the line. A logical 0 on an output pin produces a high level on the line. Consequently, the C1INV and C2INV bits in the CMCON register should not be set for the automatic pass-thru function to work correctly.

Reference Voltage

Another fortunate coincidence is that the output of the voltage reference module of the PIC16F88 uses the same pin that the comparators use as their common reference input. This means that the voltage reference module can easily be used to provide the threshold voltage for the comparators. This way the threshold voltage is not fixed in hardware, but can be made configurable through software.

The selected resistor values put the low level on the input of the first comparator below 0.87V and the high level between 1.87V and 2.24V. Using the low range of the voltage reference module the voltage can be specified in steps of around 0.2V. A value of 7 results in 1.46V, which falls nicely within the threshold values suggested in the OpenTherm specification. The threshold value for the second comparator is not very critical. A 1.46V threshold will do just nicely there too.

The PIC16F1847 doesn't have a voltage reference module. In this chip the reference voltage for the comparators is generated using the DAC (digital to analog converter). The DAC creates its output voltage by splitting a fixed input voltage of 2.048V into 32 levels. As a result, the possible levels are slightly different from the PIC16F88. Output level 23 is 1.47V. The output of the DAC can be routed internally to the comparators. To prevent interference, the output has not been made available on AN2 (pin 1).

Timer 2

The OpenTherm protocol specifies that bits have a duration of 1ms. There always is a transition in the middle of the bit interval and there may be transitions at the start/end of a bit. These transition are allowed to be between 100µs early and 150µs late. This means that while transmitting a message, there are alternating periods of 250µs that the line must stay stable at the zero or one level and periods of 250µs when a transition may occur.

It takes four periods of the oscillator to execute one instruction. The timers of the PIC also run at one fourth of the oscillator frequency when no pre-scalers are used. By configuring the internal oscillator of the PIC to 4MHz, an 8-bit wide timer takes a maximum of 256µs to roll over. Timer 2 has a period register that can be used to make the timer skip some counts and roll over to 0 early. This way the timer can be made to very accurately generate an interrupt every 250µs.

Serial Interface

The device also needs some way to communicate with a PC. The simplest way to do that is using a serial interface. As I had a terminal server with 8 serial ports near the intended location of the Opentherm gateway, that would work nicely. To limit the loading on the processor I decided to use a fairly low speed of 9600 baud for the serial communication. With a 4MHz clock rate, this baud rate can be achieved with an error of only 0.16%.

A baud rate of 9600 is also ideal for serial commands that store configuration data in data EEPROM. Data EEPROM typically needs 4ms for an erase/write cycle. The minimal serial command length is 5 chars (4 chars + CR), which takes 5.2ms to transfer over a serial line at 9600 baud. So this baud rate prevents commands coming in faster than the gateway can process them. An exception is the SB command, which stores 2 bytes in Data EEPROM, and may therefor take 8ms.

Flash Program Memory

The firmware of the device is stored in Flash Program Memory of the PIC, which is readable and writable by the code. This allows the device to update its own firmware without the need for a PIC programmer, once the initial program has been loaded into the PIC.