Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

manchester code

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
mcguinn

Bascom Member



Joined: 22 Jan 2017
Posts: 3

PostPosted: Mon Feb 13, 2017 9:14 pm    Post subject: manchester code Reply with quote

when programming attiny13 any byte saved helps. I written this code to encode or decode a radiolink in Manchester code. Perhaps of use to others. ps programming an attiny13 is not inspired by practical or economical reasons; it's a challenge Smile
Code:

'variables used for Manchester encoding
Dim B0 As Byte
Dim Man_word As Dword
Dim B4 As Byte At Man_word + 0 Overlay
Dim B3 As Byte At Man_word + 1 Overlay
Dim B2 As Byte At Man_word + 2 Overlay
Dim B1 As Byte At Man_word + 3 Overlay



Manchester_encode:
'B0 has the byte to encode
'b1..b4 overlay over man_word
'when man_word has been shifted left 16 times then B1=0, loop is done
'and B0's complement and B0 have been shifted interleaved into B2 and B3
'later decoding is done in a similar way
'                 B1       B2       B3       B4
   Man_word = &B11111111_11111111_00000000_00000000
   B4 = B0 Xor &B11111111               'C's complement
   Do
      Shift Man_word , Left , 1         'shift in a bit of B0's complement
      Swap B0 , B4
      Shift Man_word , Left , 1         'shift in a bit of B0
      Swap B0 , B4
   Loop Until B1 = 0                    'Loop Until B1 = 0
Return

Manchester_decode:
'b1..b4 overlay over man_word
'manchester bytes placed in b2 and b3
'when man_word has been shifted right 16 times B1=0,b2=0
'B4 will have the original byte, B0 has it's complement
'After XOR B4 B0, If there were illegal bits, B1 will be <> 255
   B1 = &B11111111
   'B0 and B4 do not need initialising
   Do
      Shift Man_word , Right , 1        'shift 1 oiginal bit into B4
      Swap B0 , B4
      Shift Man_word , Right , 1        'shift 1 complement bit into B0
      Swap B0 , B4
   Loop Until B1 = 0 And B2 = 0
   B1 = B4 Xor B0                       'if errorbits, b1<>255
Return
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4970
Location: Holland

blank.gif
PostPosted: Tue Feb 14, 2017 10:21 pm    Post subject: Reply with quote

That is what i call clever programming!
Thank you for sharing.

_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum