Sample Code in C (unknown source):
//================================================================
int16 man_encode(int unenc) {
//================================================================
int odd_byte,even_byte,temp;
odd_byte=unenc&0xAA;
temp=(~unenc&0xAA)>>1;
odd_byte=odd_byte|temp;
even_byte=unenc&0x55;
temp=(~unenc&0x55)<<1;
even_byte=even_byte|temp;
return((int16)odd_byte<<8)|even_byte;
}
//================================================================
int man_decode(int16 enc) {
//================================================================
int odd_byte,even_byte;
odd_byte=(int)(enc>>8);
if((odd_byte&0xAA)^((~odd_byte&0x55)<<1)) {
receive_error=1;
return(0);
} else odd_byte&=0xAA;
even_byte=(int)enc;
if((even_byte&0x55)^((~even_byte&0xAA)>>1)) {
receive_error=1;
return(0);
} else even_byte&=0x55;
receive_error=0;
return(odd_byte|even_byte);
}
And the same in PIC16 assembly (by me):
;***** man_encode *********************************************************
;* manchester encode
;* Input : unenc ; byte to encode
;* Output: manch16 ; coded word
;* Clock : ?
;**************************************************************************
man_encode movf unenc,w ; odd_byte=unenc&0xAA;
andlw 0AAh
movwf odd_byte
comf unenc,w ; temp=(~unenc&0xAA)>>1;
andlw 0AAh
movwf TEMP
clrc
rrf TEMP,f
movf TEMP,w ; odd_byte=odd_byte|temp;
iorwf odd_byte,f
movf unenc,w ; even_byte=unenc&0x55;
andlw 055h
movwf even_byte
comf unenc,w ; temp=(~unenc&0x55)<<1;
andlw 055h
movwf TEMP
clrc
rlf TEMP,f
movf TEMP,w ; even_byte=even_byte|temp;
iorwf even_byte,f
return
;***** man_decode *********************************************************
;* manchester decode
;* Input : manch16 ; coded word
;* Output: unenc ; decoded byte
;* Clock : ?
;**************************************************************************
man_decode comf odd_byte,w ; TEMP = (~odd_byte&0x55)<<1)
andlw 055h
movwf TEMP
clrc
rlf TEMP,f
movf odd_byte,w ; W = (odd_byte&0xAA)
andlw 0AAh
xorwf TEMP,w ; W = (odd_byte&0xAA)^(~odd_byte&0x55)<<1)
jz md_input_ok1 ; if((odd_byte&0xAA)^((~odd_byte&0x55)<<1)) {
movlw 1 ; receive_error=1;
movwf receive_error
clrf unenc ; return(0);
return
; }
md_input_ok1 movlw 0AAh ; else odd_byte&=0xAA;
andwf odd_byte,f
comf even_byte,w ; TEMP = (~even_byte&0xAA)>>1)
andlw 0AAh
movwf TEMP
clrc
rrf TEMP,f
movf even_byte,w ; W = (even_byte&0x55)
andlw 055h
xorwf TEMP,w ; W = (even_byte&0x55)^((~even_byte&0xAA)>>1)
jz md_input_ok2 ; if((even_byte&0x55)^((~even_byte&0xAA)>>1)) {
movlw 1 ; receive_error=1;
movwf receive_error
clrf unenc ; return(0);
return
; }
md_input_ok2 movlw 055h ; else even_byte&=0x55;
andwf even_byte,f
clrf receive_error ; receive_error=0;
movf odd_byte,w ; return(odd_byte|even_byte);
iorwf even_byte,w
movwf unenc
return