Manchester encoding

Could anyone please give me some idea on implement manchester encoding in programming.

I found quite a lot resource about the theory, but I feel a bit hard to

convert the theory in programming.

I am using AVR UART TxD to transmit 10bit serial data wireless to other AVR UART RxD.

I can complete that transmission with wire. However, I require to do in wireless, therefore encoding /decoding are required,

I read quite a lot infomation about the manchester encoding and S.N.A.P (from www.hth.com), however I get stuck on convert the theory to programming code.

Could you please give me some more idea about serial data encoding.

P.S. 10bits data are required to encode and transmit, complier is BASCOM-AVR

Thank you guys.

i think the pseudo code for manchester encoding will be like this:

int switch = 0

->set timer at double the bit rate:
 read next send_bit
  if switch == 0
                switch=1
                if send_bit==0
                then tx = 1 

                if send_bit==1
                then tx = 0 
  elseif switch == 1
                switch =0
                if send_bit==0
                then tx = 0

                if send_bit==1
                then tx = 1

i am sory if i have caused further confusion

this is for transmission only

yes, the hard part seems to be in rx, where you have to sync up to the bit stream. In hardware, you would do this with a PLL; I don’t know what the simplest software equivalent is.

I see that the MikroElectonika compilers have a Manchester library along with some sample code that might be helpful for you.

I don’t know about AVR’s but with a PIC you could use the Interrupt-On-Change feature on PortB. The interrupt service routine (after detecting it’s this sort of interrupt), would maintain the previous bit value as well as a ‘bit done’ value? Also a timer could be used to look for missing transitions.

For 00 or 11:

  • Mid-bit transition: caught by interrupt/ reset timer

  • End-bit transition: caught by interrupt/ reset timer (returns pre-mid-bit value)

  • Mid-bit transition: caught by interrupt/ reset timer

For 01 or 10:

  • Mid-bit transition: caught by interrupt/ reset timer

  • Timer-Time Out (return pre-mid-bit value and reset timer)

(you could probably use the timer and pin change interrupt to synchronize sending too)

  • Mid-bit transition: caught by interrupt/ reset timer

I saw this on the PicBasic forum. The basic encode/decode routines are in asm, but the sample calls them using PicBasic. You can still get the idea.

Melanie (well known power PIC user) says:

Encoding: 8 bit byte becomes 16 bits to transmit… zero’s become “01” and ones become “10”.

Decoding: 16 bits get deconstructed back to 8 bit byte… “01” gets turned back into a zero and “10” gets turned back into a one.

Example… you are sending byte $00 which is equal to %00000000… this get’s turned into %0101010101010101.

; compiled for an 18F2525. 582 words with MPASMWIN 5.01

X           VAR BYTE
Y           VAR BYTE
BitCount    VAR BYTE bank0 system ' Bank0 system so we don't need an underscore
ByteIn      VAR BYTE bank0 system ' to access BASIC variables from assembler
ByteOut     VAR BYTE bank0 system
Manch       VAR WORD bank0 system ' Holds manchester encoded word
Temp        VAR WORD bank0 system ' Temp var
Enc_Dat     VAR WORD[6]           ' Holds 6 manchester encoded words
    
    GOTO Main   ' Jump over encode/decode routines
    
ASM  ; Note: For 14-bit core just change Rlcf to Rlf
     ; Manchester encode routine
_Encode
	Movlw   8
	Movwf   BitCount
E_Repeat
	Rlcf    ByteIn,F   ; Rotate MSB on ByteIn into carry & test for 1
	Btfss   STATUS,C   ; If bit = 1 then skip to BitSet
	Goto    BitClr     ; If bit = 0 then goto to BitClr
BitSet                 ; Bit was set, so encode 1 as 10
	Rlcf    Manch,F
	Rlcf    Manch+1,F
	bcf     STATUS,C
	Rlcf    Manch,F
	Rlcf    Manch+1,F
	Goto    E_Loop
BitClr                 ; Bit was clear, so encode 0 as 01
	Rlcf    Manch,F
	Rlcf    Manch+1,F
	bsf     STATUS,C
	Rlcf    Manch,F
	Rlcf    Manch+1,F
E_Loop
	Decfsz  BitCount,F
	Goto    E_Repeat
	Return
ENDASM

ASM
    ; Manchester decode routine.
_Decode
	Movf    Manch+1,W
	Movwf   Temp+1
	Movf    Manch,W
	Movwf   Temp
	Movlw   8
	Movwf   BitCount
Repeat
	Rlcf    Temp,F       
	Rlcf    Temp+1,F    
	Rlcf    ByteOut,F    
	Rlcf    Temp,F      
	Rlcf    Temp+1,F    
	Decfsz  BitCount,F
	Goto    Repeat
	Return
ENDASM

Main:
    CLEAR       ' Used for demo to start with all RAM clear
    ' Manchester encode ASCII characters "A" to "F"
    Y = 0       ' Start array index pointer at 0
    FOR X = "A" to "F"
     ByteIn = X
     CALL Encode
     Enc_Dat[Y] = Manch
     HSEROUT ["Encoded ",X," = ",IBIN16 Enc_Dat[Y],13,10]
     Y = Y + 1  ' Increment array index pointer
    NEXT X
    
    ' Decode & print results
    FOR Y = 0 to 5
     Manch = Enc_Dat[Y]
     CALL Decode
     HSEROUT ["Decoded ",IBIN16 Manch," = ",ByteOut,13,10]
    NEXT Y
    PAUSE 10000
    GOTO Main
    
    END

google for Nigel Goodwin winpicprog. Off that website is a tutorial that covers manchester.

Hi,

Have a look at this example of manchester encoding/decoding in Bascom-AVR:

http://www.grote.net/bascom/msg19038.html

Rgds.

Hi,

I’ve used a simple look-up table for encoding. Decoding is a little trickier, but the look-up table method for encoding is small, and pretty quick. As a ‘PIC’ user I use the instruction ‘swapf’ which swaps the nibbles of a byte, which allows you to encode each nibble seperately, meaning you only need 16 entries in the table.

eg:

0000 → 10101010

0001 → 10101001

0110 → 10010110

1111 → 01010101

The pseudo-code is pretty simple too:

Get your byte to be encoded

Store in TEMP

AND with ‘00001111’ (gets rid of the upper nibble)

Call lookup

Store result in MAN1

Get TEMP

Swap nibbles

AND with ‘00001111’ (gets rid of the upper nibble)

Call lookup

store in MAN2

I don’t know about basic, or AVR, but i’m sure most micro’s these days have that swap nibble instruciton.

Transmitting manchester encoded data works quite well with a standard UART, as the start and stop bit make a ‘10’ which is man coding for ‘0’ between nibbles. Great for syncronisation.

Hope this helps,

BuriedCode.