Re: [bascom] Bascomavr Manchester encoding/decoding


From Satz <pillai@singnet.com.sg>
Date Sun, 14 Dec 2003 07:34:22 +0800

Georg,

I've simplified the code I posted earlier and have been using it very
successfully in a radio packet controller using a 2313. 
I hope this is much clearer:

'[ Variables ]

Dim EncodedWord As Word At $60  'Holds transmitted or received encoded
"byte"
Dim EncodedWordLow As Byte At $60 Overlay  'Low byte of EncodedWord
Dim EncodedWordHigh As Byte At $61 Overlay 'High byte of EncodedWord

Dim TempWord As Word At $62        'Temporary word variable
Dim TempWordLow As Byte At $62 Overlay    'Low byte of TempWord
Dim TempWordHigh As Byte At $63 Overlay   'High byte of TempWord

Dim RxBuffer(IOBufferSize) As Byte  'Receive Buffer (holds Rx packet)
Dim TxBuffer(IOBufferSize) As Byte  'Transmit Buffer (holds Tx packet)

Dim I As Byte

'--------[ Manchester-Encode ]---------------------------------
Encode:  'Encodes TxBuffer(I) into Manchester-encoded word for
transmission                                     
  EncodedWordLow = TxBuffer(I) Or &H55    'EncodedWordLow contains odd
bits of TxBuffer(I); even bits =1
  EncodedWordHigh = TxBuffer(I) Or &HAA   'EncodedWordHigh contains even
bits of TxBuffer(I); odd bits=1
  TempWord = EncodedWord And &H55AA
  Shift TempWordLow , Right                   'Shift TempWordLow right
  TempWordHigh = TempWordHigh + TempWordHigh  'Shift TempWordHigh left
  EncodedWord = EncodedWord Xor TempWord      'Resulting
Manchester-encoded word
  Return

'--------[ Manchester-Decode ]---------------------------------
Decode:  'Decodes received Manchester-encoded word into
RxBuffer(I)                                     
  EncodedWord = EncodedWord Or &HAA55              
  RxBuffer(I) = EncodedWordLow And EncodedWordHigh  'Resulting decoded
byte


Georg Pistorius wrote:
> 
> Hi
> Any ideas on Manchester encoding/decoding. I want to send some information
> with a RF device. I don't want to use an additional encoder due to the size.
> Is there maybe some algorithm (I looked at the code Satz posted but I don't
> get the right results)
> My code seems a bit bulky.
> *debugging code with lots of lcd delays
> Dim Encodedword As Word At $60                              '
> Dim Rcs As String * 8
> Dim Tempstr As String * 1
> ' $sim
> Dim Tempword As Word                                        'Temporary
> variable
> Dim I As Byte
> Dim K As Byte
> Dim S As Byte
> '$sim
> Config Lcd = 20 * 4
>  Lcd "Hello"
>  Wait 1
>  Cls
>  K = 1
> 
>  Do
>   Inputhex Encodedbyte(1)
>  Waitms 50
>  Rcs = Bin(encodedbyte(1))
>  Lcd Rcs
>  Lowerline
>  For I = 0 To 7
>  K = I + 1
>   Tempstr = Mid(rcs , K , 1)
>  If Tempstr = "0" Then
>  I = I * 2
>    Reset Encodedword.i
>    I = I + 1
>  Set Encodedword.i
>    I = I - 1
>    I = I / 2
>   Else
>   I = I * 2
>  Set Encodedword.i
>  I = I + 1
>     Reset Encodedword.i
>  I = I - 1
>    I = I / 2
> 
>  End If
>     Next
>   I = 0
>   Thirdline
>  Lcd Bin(encodedword)
>   Wait 5
>   Rcs = Bin(encodedword)
>   Wait 5
>  Cls
>  For I = 1 To 16
>   'Wait 2
>    Tempstr = Mid(rcs , K , 2)
>    Lcd Tempstr
>    'Lcd " "
>   If Tempstr = "01" Then
>   K = I * 2                                                 '
>  Set Encodedbyte(2).i
>   Else
>  Reset Encodedbyte(2).i
> 
>   End If
> 
>   Next
>   Thirdline
>   Lcd Encodedbyte(2)
>   Lowerline
>   Lcd Bin(encodedbyte(2))
>   Wait 5
> Loop
> Thanks
> Georg