In Part 1 ofthis series, I introduced the use of Manchester Encoding as a novelapproach to designing a low-cost serial wired or wirelesscommunications interface. In this second part, I will leverage uponthis concept and provide a realistic example that will illustrate thesimplicity of implementing Manchester Encoding into a real-worldembedded design.
Albert Einstein once said that everything in life should be as simpleas possible, but no simpler. One of the benefits of Manchester Encodingis how simple it is to implement. All that is needed is a tinymicrocontroller, a couple resistors, a MOSFET, and a few lines of code.
The circuit in Figure 1 below isa simplified, yet highly functional design that can be used as afoundation for communicating via Manchester Encoding. At the heart ofthis design is a Microchip PIC microcontroller; however anymicrocontroller with similar functionality will work equally as well.
One of the fundamental ingredients for an efficient implementationof Manchester Encoding is an analog comparator feature found on manymicrocontrollers. Exploiting this feature will provide more efficiencywhile simplifying and reducing the software required to implementManchester Encoding.
Analyzing the Details
Let us take a closer look at the circuitry in Figure 1 . The microcontrollerchosen for this example is a Microchip PIC 12F683. Thisversatile, low-cost microcontroller comes in an 8-pin SOIC and containsenough on-board peripherals to support a variety of applications.
For Manchester Encoding only two general purpose I/O pins arerequired. One pin is configured as an output and provides the gatedrive voltage for controlling the MOSFET Q1. The other pin is used asan input to receive incoming Manchester Encoded data from anotherdevice 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 CommunicationsLine. The pull-down resistor R2 will ensure a low voltage on thecommunication line when the MOSFET is turned OFF.
The input pin labeled GP1 will be configured with the analogcomparator function. This pin will basically “listen” to thecommunications line and provide the timing requirements necessary toproperly decode the Manchester Encoded bit stream.
If the microcontroller supports an open-drain output, the circuit in Figure 1 can be simplified even more(See Figure 2 below ).
With an open-drain output, the pin from the microcontroller can betied directly to the communication line. In doing this, the pull-downresistor in Figure 1 ischanged to a pull-up, and the MOSFET can be removed completely from thecircuit.
It is worth noting that care should be taken when using open-drainoutputs. Setting and resetting outputs may not be as straight forwardas 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 levelof the pin itself, rather than the output latch, when trying todetermine the current state of an output. This voltage level on the pinis used when writing back to the output latches. Many developers havespent hours debugging designs because of this one crucial issue.
As mentioned earlier, the analog comparator feature is used to enhancethe efficiency of receiving Manchester Encoded data. Let's take acloser look into why this feature is so important.
Recall that Manchester Encoding is unique to other forms of serialdata transmission because it uses bit transitions rather than logiclevels to identify a logic “1” or logic “0”. In Figure 3 below therising edge signal represents logic “0”, while the trailing edge signalrepresents logic “1”.
The analog comparator circuitry within the PIC microcontroller canbe configured to generate an interrupt on any transition that occurs onits 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 microcontrollerto decode the Manchester Encoded signal into the proper logic “1” orlogic “0” value.
1) Initialization . Beforethe PIC microcontroller can begin receiving Manchester Encoded data itneeds to be properly configured. The microcontroller initializationprocess comprises setting up the on-board analog comparator circuitry,the two free-running timers, and the two pins as an output and aninput.
2) Analog Comparator . For theanalog comparator circuitry this design will use the “Multiplexed withInternal Reference” configuration (see PIC datasheet for details). Thisis accomplished by writing 0x0E to the CMCON0 register. Within thisconfiguration register the CIS bit is set to logic “1”, which willchannel the input signal through the CIN+ pin.
The next register to setup is VRCON, which is responsible forestablishing the internal voltage reference to the input of thecomparator. For this design a reference voltage will be set to half ofthe supply voltage. This is accomplished by writing 0xAC to the VRCONregister.
Next, the Peripheral Interrupt Register (PIE1) is configured, whichwill enable the Comparator interrupts to function. This is accomplishedby writing a 0x40 to the PIE1 register.
Lastly, the Comparator Interrupt Flag bit needs to be cleared inorder to allow further interrupts to occur. This is done by setting theCMIF bit to zero.
3) Free-Running Timers . Twofree-running timers will be used to assist in constructing and decodingManchester Encoded data. Timer 0 will be used in the construction(encoding) of the data stream.
The configuration register TMR0 is setup to generate an interruptperiod equal to twice the desired Manchester Encoded bit rate. So forexample, if you desire a bit rate of 5 KHz (200us), the interrupt rateshould be set to 100us.
Timer 1 is used as part of the decoding procedure. This timer willbe used by the analog comparator interrupt service routine to determinewhether the signal transition received is a SETUP or DECODE transition.
Interrupt Service Routines
1) Transmitting ManchesterEncoded Data. Timer 0's interrupt service routine is responsiblefor transmitting Manchester Encoded data. In this example the routineruns every 100us and will generate a Manchester Encoded bit stream at a200us period. Also, 13-bits are transmitted, 12-bits of data and 1-bitfor parity.
The code sample in Listing 1 below starts the transmission process. This function takes the 12 bits ofdata passed to it and calculates the parity. After setting the paritybit to the appropriate level it places the 13 bits of data intoEncode_Val and sets the Encode_State variable to SYNCH_START.
Listing 2 below shows theinterrupt service routine responsible for transmitting ManchesterEncoded data. Within this routine there are six possible states to bein. When the routine is not sending data it is in the IDLE state.
Once data is ready to be sent, Start_Transmission is called. Thisfunction sets the Encode_State to SYNCH_START. This initiates thetransmission process to occur.
This particular design requires each Manchester Encoded transmissionto send a one-bit “synch” pulse at the beginning of each transmission.This pulse, also known as a preamble, is used to initiate andsynchronize the receiver.
In Ethernet, an 8-byte preamble is transmitted to allow the PhasedLocked Loop circuitry on the receiver to “lock” before the transmittersends the remainder of its data.
In this example, the synch pulse is simply used to initiate thereceiver. However, it is entirely possible to use the synch pulse tocheck the clock frequency of the transmitter and determine if it iswithin expectations.
Further refinement of the receiver's software could also support adynamic adjustment of its timer to closely match the clock frequency ofthe transmitter. This would provide an additional feature where thereceiver could synchronize its free-running timer to match thetransmitter's clock frequency.
Once the synch pulse has been transmitted, the Timer 0 interruptservice routine proceeds with sending all 13 bits of data. The serviceroutine will toggle between SETUP and TRANSITION states until all thebits have been transmitted.
It is important to note that there is a significant increase inmicrocontroller processing if the analog comparator interrupt is notdisabled while transmitting Manchester Encoded data. This is due to theoutput and input pins being tied together. If it is not necessary toreceive simultaneously transmitted data, it is suggested that thecomparator interrupt is disabled while transmitting data.
2. Receiving (Decoding)Manchester Encoded Data. Listing3 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 bittransitions that occur on the GP1 input pin. In addition, this routinewill provide a certain level of error-proofing, which will preventfalse decoding on a noisy communication line.
Initially the routine is in the IDLE state and is waiting for thesynch pulse to be received. Once the synch pulse's negative edgetransition has occurred the routine is placed in the SYNCH_START state.When the rising edge of the synch pulse has been received the serviceroutine checks the elapsed time from Timer 1 to see if the synch pulseis within the maximum width requirements.
If it is not, the service routine is placed back into the IDLEstate. If the synch pulse width is deemed valid, the service routine isplaced into the DECODE state.
Within the DECODE state the service routine checks to see if thereceived bit transition occurred within the acceptable time boundaryfor a valid pulse width. Any edge transition occurring outside theacceptable time boundary is flagged as an error. If this occurs theremainder of bit transitions received is ignored and the accumulateddata is thrown out.
There are a number of ways to determine that all bits have beenreceived. One way is to allow Timer 1 to continue counting up until itoverflows. This condition occurs when the communication line is silentfor a lengthy period of time.
Once the timer overflows an interrupt is generated. This interruptshould only occur when the communication line has been silent for atime period guaranteed to be longer than any possible transmissiondelays.
Another method is to count the number of bits received and todisable the receive interrupts when all the bits have been processed.There are other possible methods as well; the specific choice isentirely up to the designer.
The design presented in this article represents a simple, low costsolution to low bit-rate serial communications. By implementingManchester Encoding a robust, easy to implement data protocol can beachieved.
It also offers a fair amount of noise immunity and data reliability.In addition, by establishing a simple Master / Slave communicationsprotocol on top of Manchester Encoding you can provide a highlyreliable system in a single point or multi-drop configuration.
To read Part 1, go to “The basics ofManchester Encoding.”
Robert Guastella is currently a senior controls engineer forTennant Company in Minneapolis, Minnesota. He has over 22 years ofexperience in hardware and software design on products ranging fromindustrial controls, to digital servo drives, to automotiveelectronics. Guastella holds a BSEE from Lawrence TechnologicalUniversity, as well as an MBA from Oakland University in Detroit,Michigan. He can be reached at email@example.com.