Hello all,
I was looking for some example on Manchester Coding and Decoding for use with RF Modules.
Want to elimate the Holtek ICs
Any help
Thank you.
Hello all,
I was looking for some example on Manchester Coding and Decoding for use with RF Modules.
Want to elimate the Holtek ICs
Any help
Thank you.
Here is the secret of Manchester encoding/decoding in all it's simplicity... (in case any other reader wants to know why - it's to balance the data slicer in the receiver and help cancel out noise).
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.
Melanie
Thank you for the information on Manchester.
But could you please explain me with a small example how this word is constructed for sending and how it is decoded when received.
Basically I am looking at hooking a PIC12C508 to the RF modules and trying to send and receive Mancheseter codes over RF.
Thank you.
Very quick example (Caveat Emptor) since it's two-thirty in the morning and time I was getting my beauty sleep... there's probably better ways of doing this, but with matchsticks propping up my eyelids this is the best you're going to get out of me tonight...
CounterA var Byte
ErrorFlag var Bit
MyData var Byte
ManchesterWord var Word
' Subroutine Encodes Manchester Word
' Enter subroutine with data in MyData
' Exit Subroutine with encoded ManchesterWord
' MyData contents are destroyed on exit
EncodeManchester:
ManchesterWord=0
For CounterA=0 to 7
If MyData.0=0 then
ManchesterWord.14=1
else
ManchesterWord.15=1
endif
If CounterA<7 then ManchesterWord=ManchesterWord>>2
MyData=MyData>>1
Next CounterA
Return
' Subroutine Decodes Manchester Word
' Enter subroutine with encoded data in ManchesterWord
' Exit subroutine with decoded data in MyData
' ManchesterWord contents are destroyed on exit
' Also on exit, ErrorFlag=0 then Data is good
' ErrorFlag=1 then Data is suspect/corrupt
DecodeManchester:
ErrorFlag=0
For CounterA=0 to 7
If ManchesterWord.1=0 then
MyData.7=0
If ManchesterWord.0=0 then ErrorFlag=1
else
MyData.7=1
If ManchesterWord.0=1 then ErrorFlag=1
endif
ManchesterWord=ManchesterWord>>2
If CounterA<7 then MyData=MyData>>1
Return
Melanie
Thank you very much for the same.
Last but not the least. How do I send it and Rx the same.
Thank for all that help.
I am sure this code and your inputs have helped a lot out there, especially me.
I shall try it and get back to you when I rx this information
regards
Hello all / Melanie
I found this on the internet hope this also helps all.
Manchester encoding can be done in PBP nice and easy:
From: Ioannis Kyriakidis (I presume)
For d=0 TO 7
IF v.0[d]=0 Then
encoded.0[d*2]=0
encoded.0[d*2+1]=1
Else
encoded.0[d*2]=1
encoded.0[d*2+1]=0
EndIF
Next
d: number of bits to encode (8 bits)
v: byte to BE encoded
encoded: WORD sized variable holding the encoded
byte v.
Note that the encoded value is double size always.
Use the above lines as subroutine to encode a byte before transmission
Mel,
Loooking at your code I'm a bit stumped.
How is each BIT in the MyData referenced to CounterA?
Also.... It looks like you are shifting through each BIT on Mydata on the last line:
MyData=MyData>1
But shouldn't it be: MyData=MyData>>1
I have read your FAQ on Bits, bytes and Words, but I can't relate your code here to the examples given in the FAQ.
But otherwise, the code looks like it should work.
Cheers
J
Thanks for letting me know. Code has been up for eons and nobody spotted the typo before now!
Hi Mel,
Yep I thought it had a bug, actually there is another ...(I swear I am not picking on you.... ) the NEXT counterA is missing in the EncodeManchester routine.
For a while I had a problem understanding how the shift right >> instruction ran. (Not using this before)
Also I assumed that the MyData BITS are stored in the registers as [0,1,2,3,4,5,6,7], but actually they are the other way around (same goes for the ManchesterWord bits).
Watching the code run through ICD makes a lot of sense. I have attached the code for anyone interested. It is fully working on a 16F628 chip and great to pause the code and swap a few bits in the manchesterword to trigger the ErrorFlag Bit.
Thanks again Melanie
Now then, where did I put those RF modules?.....
' ==========================
' DEVICE PROGRAMMING OPTIONS
' ==========================
' PRESET FUSES
@ DEVICE PIC16F628, WDT_ON
@ DEVICE PIC16F628, INTRC_OSC_NOCLKOUT
@ DEVICE PIC16F628, BOD_ON
@ DEVICE PIC16F628, PROTECT_OFF
@ DEVICE PIC16F628, MCLR_ON
' =============
' USER DEFINES
' =============
DEFINE OSC 4
' =================
' SET USART PARAMS
' =================
DEFINE HSER_RCSTA 90h ' Set receive register to receiver enabled
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 4800 ' Set baud rate
DEFINE HSER_SPBRG 51 ' Set SPBRG directly (normally set by HSER_BAUD)
' ===========================
' CONFIGURE COMPARATOR MODULE
' ===========================
CMCON = 7 ' TURN ANALOG COMPARATOR MODE OFF
' ==============
' PROGRAM VARS
' ==============
COUNTERA var Byte
ERRORFLAG var Bit
MYDATA var Byte
MANCHESTERWORD var Word
' ===================
' START
' ===================
START:
HSERIN [MYDATA]
HSEROUT ["ENTERED VALUE: ",MYDATA,13,10]
HSEROUT ["ENCODING TO MANCHESTER NOW",13,10]
GOSUB EncodeManchester
HSEROUT ["ENCODING COMPLETED",13,10]
PAUSE 1000
HSEROUT ["DECODING MANCHESTER NOW",13,10]
GOSUB DECODEMANCHESTER
HSEROUT ["DECODING COMPLETED",13,10]
IF ERRORFLAG THEN
HSEROUT ["VALUE CORRUPTED!: ",MYDATA,13,10,13,10]
ELSE
HSEROUT ["FINAL VALUE: ",MYDATA,13,10,13,10]
ENDIF
GOTO START:
' Subroutine Encodes Manchester Word
' Enter subroutine with data in MyData
' Exit Subroutine with encoded ManchesterWord
' MyData contents are destroyed on exit
ENCODEMANCHESTER:
ManchesterWord=0
For CounterA=0 to 7
If MyData.0=0 then
ManchesterWord.14=1
else
ManchesterWord.15=1
endif
If CounterA<7 then ManchesterWord=ManchesterWord>>2
MyData=MyData>>1
Next CounterA
Return
' Subroutine Decodes Manchester Word
' Enter subroutine with encoded data in ManchesterWord
' Exit subroutine with decoded data in MyData
' ManchesterWord contents are destroyed on exit
' Also on exit, ErrorFlag=0 then Data is good
' ErrorFlag=1 then Data is suspect/corrupt
DECODEMANCHESTER:
ErrorFlag=0
For CounterA=0 to 7
If ManchesterWord.1=0 then
MyData.7=0
If ManchesterWord.0=0 then ErrorFlag=1
else
MyData.7=1
If ManchesterWord.0=1 then ErrorFlag=1
endif
ManchesterWord=ManchesterWord>>2
If CounterA<7 then MyData=MyData>>1
NEXT COUNTERA
Return
Thank you for your contribution ,Squibcakes.
Your code can be an excellent example for someone wanting RC5 over RF.
regards
hi all, i don't believe the decoding part of that code works! if you received an encoded stream of 0110100110011010 for example, this would raise the erro flag?
If ManchesterWord.1=0 then
MyData.7=0
If ManchesterWord.0=0 then ErrorFlag=1
else
MyData.7=1
If ManchesterWord.0=1 then ErrorFlag=1
endif
any help?
thanks.
Bookmarks