Design Overview
Albert Einstein once said that everything in life should be as simple
as possible, but no simpler. One of the benefits of Manchester Encoding
is how simple it is to implement. All that is needed is a tiny
microcontroller, a couple resistors, a MOSFET, and a few lines of code.
The circuit in Figure 1 below is a simplified, yet highly functional design that can be used as a foundation for communicating via Manchester Encoding. At the heart of this design is a Microchip PIC microcontroller; however any microcontroller with similar functionality will work equally as well.
Figure 1
One of the fundamental ingredients for an efficient implementation of Manchester Encoding is an analog comparator feature found on many microcontrollers. Exploiting this feature will provide more efficiency while simplifying and reducing the software required to implement Manchester Encoding.
Analyzing the Details
Let us take a closer look at the circuitry in Figure 1. The microcontroller
chosen for this example is a Microchip PIC 12F683. This
versatile, low-cost microcontroller comes in an 8-pin SOIC and contains
enough on-board peripherals to support a variety of applications.
For Manchester Encoding only two general purpose I/O pins are required. One pin is configured as an output and provides the gate drive voltage for controlling the MOSFET Q1. The other pin is used as an input to receive incoming Manchester Encoded data from another device on the communication line.
A high voltage on the output pin GP0 will turn ON the MOSFET, resulting in a high voltage on the Manchester Encoded Communications Line. The pull-down resistor R2 will ensure a low voltage on the communication line when the MOSFET is turned OFF.
The input pin labeled GP1 will be configured with the analog comparator function. This pin will basically "listen" to the communications line and provide the timing requirements necessary to properly decode the Manchester Encoded bit stream.
Open-Drain Output
If the microcontroller supports an open-drain output, the circuit in Figure 1 can be simplified even more
(See Figure 2 below).
Figure 2
With an open-drain output, the pin from the microcontroller can be tied directly to the communication line. In doing this, the pull-down resistor in Figure 1 is changed to a pull-up, and the MOSFET can be removed completely from the circuit.
It is worth noting that care should be taken when using open-drain outputs. Setting and resetting outputs may not be as straight forward as it would seem. Many microcontrollers use "read-modified-write" instructions when setting or resetting bits on various output ports.
The problem is that the microcontroller will read the voltage level of the pin itself, rather than the output latch, when trying to determine the current state of an output. This voltage level on the pin is used when writing back to the output latches. Many developers have spent hours debugging designs because of this one crucial issue.
Analog Comparator
As mentioned earlier, the analog comparator feature is used to enhance
the efficiency of receiving Manchester Encoded data. Let's take a
closer look into why this feature is so important.
Recall that Manchester Encoding is unique to other forms of serial data transmission because it uses bit transitions rather than logic levels to identify a logic "1" or logic "0". In Figure 3 below the rising edge signal represents logic "0", while the trailing edge signal represents logic "1".
Figure 3
The analog comparator circuitry within the PIC microcontroller can be configured to generate an interrupt on any transition that occurs on its input signal line.
This means that a level transition for a logic "0", as well as logic "1" will trigger an interrupt to occur. This allows the microcontroller to decode the Manchester Encoded signal into the proper logic "1" or logic "0" value.
Figure 4
Software Design
1) Initialization. Before
the PIC microcontroller can begin receiving Manchester Encoded data it
needs to be properly configured. The microcontroller initialization
process comprises setting up the on-board analog comparator circuitry,
the two free-running timers, and the two pins as an output and an
input.
2) Analog Comparator. For the analog comparator circuitry this design will use the "Multiplexed with Internal Reference" configuration (see PIC datasheet for details). This is accomplished by writing 0x0E to the CMCON0 register. Within this configuration register the CIS bit is set to logic "1", which will channel the input signal through the CIN+ pin.
The next register to setup is VRCON, which is responsible for establishing the internal voltage reference to the input of the comparator. For this design a reference voltage will be set to half of the supply voltage. This is accomplished by writing 0xAC to the VRCON register.
Next, the Peripheral Interrupt Register (PIE1) is configured, which will enable the Comparator interrupts to function. This is accomplished by writing a 0x40 to the PIE1 register.
Lastly, the Comparator Interrupt Flag bit needs to be cleared in order to allow further interrupts to occur. This is done by setting the CMIF bit to zero.
3) Free-Running Timers. Two free-running timers will be used to assist in constructing and decoding Manchester Encoded data. Timer 0 will be used in the construction (encoding) of the data stream.
The configuration register TMR0 is setup to generate an interrupt period equal to twice the desired Manchester Encoded bit rate. So for example, if you desire a bit rate of 5 KHz (200us), the interrupt rate should be set to 100us.
Timer 1 is used as part of the decoding procedure. This timer will be used by the analog comparator interrupt service routine to determine whether the signal transition received is a SETUP or DECODE transition.
Interrupt Service Routines
1) Transmitting Manchester
Encoded Data. Timer 0's interrupt service routine is responsible
for transmitting Manchester Encoded data. In this example the routine
runs every 100us and will generate a Manchester Encoded bit stream at a
200us period. Also, 13-bits are transmitted, 12-bits of data and 1-bit
for parity.
The code sample in Listing 1 below starts the transmission process. This function takes the 12 bits of data passed to it and calculates the parity. After setting the parity bit to the appropriate level it places the 13 bits of data into Encode_Val and sets the Encode_State variable to SYNCH_START.
Listing 1
Listing 2 below shows the interrupt service routine responsible for transmitting Manchester Encoded data. Within this routine there are six possible states to be in. When the routine is not sending data it is in the IDLE state.
Once data is ready to be sent, Start_Transmission is called. This function sets the Encode_State to SYNCH_START. This initiates the transmission process to occur.
This particular design requires each Manchester Encoded transmission to send a one-bit "synch" pulse at the beginning of each transmission. This pulse, also known as a preamble, is used to initiate and synchronize the receiver.
In Ethernet, an 8-byte preamble is transmitted to allow the Phased Locked Loop circuitry on the receiver to "lock" before the transmitter sends the remainder of its data.
In this example, the synch pulse is simply used to initiate the receiver. However, it is entirely possible to use the synch pulse to check the clock frequency of the transmitter and determine if it is within expectations.
Further refinement of the receiver's software could also support a dynamic adjustment of its timer to closely match the clock frequency of the transmitter. This would provide an additional feature where the receiver could synchronize its free-running timer to match the transmitter's clock frequency.
Once the synch pulse has been transmitted, the Timer 0 interrupt service routine proceeds with sending all 13 bits of data. The service routine will toggle between SETUP and TRANSITION states until all the bits have been transmitted.
Listing 2
It is important to note that there is a significant increase in microcontroller processing if the analog comparator interrupt is not disabled while transmitting Manchester Encoded data. This is due to the output and input pins being tied together. If it is not necessary to receive simultaneously transmitted data, it is suggested that the comparator interrupt is disabled while transmitting data.
2. Receiving (Decoding) Manchester Encoded Data. Listing 3 below shows the analog comparator interrupt service routine, which is responsible for decoding Manchester Encoded data.
This is a state driven routine which will process all data bit transitions that occur on the GP1 input pin. In addition, this routine will provide a certain level of error-proofing, which will prevent false decoding on a noisy communication line.
Initially the routine is in the IDLE state and is waiting for the synch pulse to be received. Once the synch pulse's negative edge transition has occurred the routine is placed in the SYNCH_START state. When the rising edge of the synch pulse has been received the service routine checks the elapsed time from Timer 1 to see if the synch pulse is within the maximum width requirements.
If it is not, the service routine is placed back into the IDLE state. If the synch pulse width is deemed valid, the service routine is placed into the DECODE state.
Within the DECODE state the service routine checks to see if the received bit transition occurred within the acceptable time boundary for a valid pulse width. Any edge transition occurring outside the acceptable time boundary is flagged as an error. If this occurs the remainder of bit transitions received is ignored and the accumulated data is thrown out.
Listing 3
There are a number of ways to determine that all bits have been received. One way is to allow Timer 1 to continue counting up until it overflows. This condition occurs when the communication line is silent for a lengthy period of time.
Once the timer overflows an interrupt is generated. This interrupt should only occur when the communication line has been silent for a time period guaranteed to be longer than any possible transmission delays.
Another method is to count the number of bits received and to disable the receive interrupts when all the bits have been processed. There are other possible methods as well; the specific choice is entirely up to the designer.
The design presented in this article represents a simple, low cost solution to low bit-rate serial communications. By implementing Manchester Encoding a robust, easy to implement data protocol can be achieved.
It also offers a fair amount of noise immunity and data reliability.
In addition, by establishing a simple Master / Slave communications
protocol on top of Manchester Encoding you can provide a highly
reliable system in a single point or multi-drop configuration.
To read Part 1, go to "The basics of
Manchester Encoding."
Robert Guastella is currently a senior controls engineer for
Tennant Company in Minneapolis, Minnesota. He has over 22 years of
experience in hardware and software design on products ranging from
industrial controls, to digital servo drives, to automotive
electronics. Guastella holds a BSEE from Lawrence Technological
University, as well as an MBA from Oakland University in Detroit,
Michigan. He can be reached at robert.guastella@tennantco.com.