19 June 2011
Author: Giorgos LazaridisManchester and Differential Manchester Code
Manchester coding is a very common data coding method, probably the most common used today. With Manchester coding, we can encode both clock and signal into one, and transmit the signal serially. One distinctive characteristic about this method is that the encoded signal has always an average DC level of 50%. This means that averaging the HIGH and LOW pulse durations of a complete encoded data, will result into 1/2 of the HIGH voltage level. This feature is very helpful in cases where the power output is a function of the data, such as in AM modulation. Using Manchester encoding, the average power is always the same, no matter what data are transmitted. Manchester encoding is also widely used in infrared data transmissions, like for example the Phillips RC protocol.
How Manchester Coding works
Like all other coding methods, Manchester code follows an algorithm to encode data. This algorithm goes like this: The data are represented NOT by logic 1 or 0, but with line transitions. A logic 0 is represented by a transition from HIGH to LOW, and a logic HIGH is represented by a transition from LOW to HIGH. Let's see a very simple 5-bits example:
The data to encode is the binary number 10010, reading from left to right. The coding occurs on every falling edge of the clock. On the first falling edge of the clock, the coded signal has a LOW to HIGH transition, because the data is HIGH. On the second falling edge of the clock, the code has a HIGH to LOW transition because the data is LOW. The same algorithm is applied for the rest of the signal.
Manchester Encoding with a microcontroller
The encoding procedure is very simple and straight forward. The following flow diagram shows how to encode data (click to enlarge):
The encoding algorithm is very simple to follow. The microcontroller software is also short and simple to make. No need for extra explanations.
Manchester Decoding with a microcontroller
This is the point where things turn nasty. There are several ways to decode a Manchester code signal. The fact that you have to follow the clock transitions makes it harder to understand. First of all, when decoding Manchester code, you have to know the clock period. Either the clock frequency is known, or it must be found during decoding. Usually, when transmission starts, a start bit and a synchronization bit are sent first for this reason. So from now on, we assume that the frequency of the clock is known. Moreover, we assume that the line is kept HIGH when no transmission occurs, and the line is pulled LOW to start a transmission, exactly as shown in the timing chart on top of this page.
I will explain two methods to decode Manchester code. The first method applies on microcontrollers which have one input pin with edge change detection interrupt. The uC must be able to detect both LOW to HIGH and HIGH to LOW transitions. For example, a PIC microcontroller has such feature on PORTB pins (PORTB INTCON CHANGE). The second method applies to all other microcontrollers, preferable with at least one timer module with interrupt on overflow (but that is not mandatory).
Manchester Decoding with a microcontroller using the Port Edge Detect Interrupt
Here is the flow diagram for this method (click to enlarge):
The Interrupt Service Routine (ISR) is called on every change of the transmission line. On every full clock period (starting from the first HIGH to LOW transition) a level change occurs, and this change "carries" the data bit. If this change is HIGH to LOW, then the bit is 0, else the bit is 1. It is important to keep the interval between changes, because there are cases where the level change does NOT "carry" the data bit. This happens every time that there are 2 similar bits in row. When this happens, the code changes state during the half period of the clock. Notice for example the second and third bits (starting from left) on the example timing chart on top of this page. These data bits are both 0. So, as said before, the Manchester code line will create an extra level change in the middle of the clock pulse. That is why you must know the full clock period during decoding.
Manchester Decoding with a microcontroller using time delay
I think that this method is easier to understand. Here is the block diagram (click to enlarge):
When the first LOW level is detected on the transmission line (which means that the transmission starts), the microcontroller waits for a full clock period to pass. This can be either done with a timer module or with repetitive loops. Then the uC reads the transmission line. If it is HIGH, then the bit received was 1, otherwise it was 0.
The Differential Manchester Coding
The Differential Manchester Code is a variation of the Manchester code. After searching around the internet for a while to find more info about this method, i sadly discovered that although there are many sites explaining the Differential Manchester Code, most of them do not make it very clear. So i had to seek in my books for more info. I then discovered that it is not that hard to understand the differential coding algorithm after all. Let's see an example:
Like before, the transmission line is kept HIGH when no data is sent (before Start and after End). There are 2 encoding methods: The first is the "Transition on LOW" and the second is the "Transition on HIGH". Both methods work exactly the same. For this example i am going to use the first method, "Transition on LOW".
Here is how it goes. In the middle of each clock pulse, a transition occurs, regardless of the bit that was sent or is about to be sent. A data bit is send during each negative clock transition. If the data bit is 0, then a polarity transition occurs (if was HIGH it goes LOW, and if it was LOW it goes HIGH), otherwise the line remains unchanged.
In our example, the data that is transmitted is the binary '10011101' (starting from left to right). Each data bit is transmitted during negative transition of the clock. I have mark these clock transitions with red lines (except the first and last lines which indicate the start and stop of the transition). Between each bit transmission, the code line changes polarity. This is done to help the receiver recreate the clock signal and synchronize with the transmitter. As i said before, i will use the "Transition on LOW" method. This means that when a bit is transmitted, if this bit is ZERO the data line changes polarity. Otherwise, if the bit is 1 the data line polarity remains unchanged.
Suppose now that we want to transmit this byte (10011101). The code line is HIGH. The transmission is initiated by pulling the code line LOW. After half a pulse, the output is pulled HIGH. This is part of the synchronization transition that occurs every middle of a bit transmission. Half a pulse next, the first bit is transmitted. The first bit is 1 (starting from left), so the code line polarity remains unchanged. After half a pulse, the code line polarity changes state and goes LOW. After one full pulse, the second bit is about to be transmitted. This bit is the 0, so the code line changes polarity and goes HIGH. The same algorithm is used to transmit all 8 bits of the data. Finally, the code line is pulled HIGH and the transmission ends.
The major difference between the Differential Manchester Code, is that the receiver does not need to know the polarity of the signal. The polarity can be figured from the line transitions. In the above example, and NOT from the code line polarity. This is something that makes this method kinda difficult to understand. It does not matter whether a logical 1 or 0 is received, but only whether the polarity is the same or is different from the previous value.