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