PIC FunctionsThe PIC 16F88 I selected for this project has several features that were an excellent fit for the functions needed for the OpenTherm gateway.
ComparatorsThe 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.
Voltage ReferenceAnother fortunate coincidence is that the output of the voltage reference module of the PIC 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 6 results in 1.25V, 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.25V threshold will do just nicely there too.
Timer 2The 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 InterfaceThe 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 MemoryThe 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.