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.
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.
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.
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.
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.