Manchester encoding
Hubert
Posts: 22
I am currently using the BS2 to transmit serial data at 4800 baud and I wanted to explore the possibility of using software subroutines to implement a manchester code instead of using·external hardware.·This would simplify my design but would also increase the data rate to 9600.· Any suggestion on how to go about doing this ??
Comments
The following routine is thanks to Ioannis Kyriakidis, with minor additions by Bruce Bates.
'Manchester encoding can be done in Pic Basic Pro (PBP) nice and easily:
/code
v var BYTE
encoded var WORD
For d=0 TO 7
IF v.0[noparse][[/noparse]d]=0 Then
encoded.0[noparse][[/noparse]d*2]=0
encoded.0[noparse][[/noparse]d*2+1]=1
Else
encoded.0[noparse][[/noparse]d*2]=1
encoded.0[noparse][[/noparse]d*2+1]=0
EndIF
Next
d: number of bits to encode (nominally 8 bits)
v: BYTE to BE encoded
encoded: WORD sized variable holding the encoded byte v.
Note the encoded value is always double size (2 X the input byte size).
Use the above lines as subroutine to encode a byte before transmission.
- - - - -
'PBASIC conversion of the above Manchester Encoding Routine
v var BYTE 'Addressed below as an implied 8 bit array
encoded var WORD 'Addressed below as an implied 16 bit array
For d=0 TO 7
··· IF v(d)=0 Then
····· encoded(d*2) = 0
····· encoded(d*2+1) = 1
··· Else
····· encoded(d*2) = 1
····· encoded(d*2+1) = 0
··· ENDIF
Next
code/
Note: The above converted routine has not been tested or even run through the Stamp Editor. Use at your own risk!
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->
Post Edited (Bruce Bates) : 2/14/2006 9:09:54 PM GMT
'bit-bang' is a term used to describe the process of emulating some hardware piece (like a UART) with software code inside a microprocessor. The PIC inside your BS2 is VERY good at bit-banging, that's how the SERIN and SEROUT commands are implemented. But to get the speed, PBasic does this in assembly code inside your BS2.
I'm also not aware of other devices that use Manchester Encoding (except for Ethernet NIC cards) so what are you trying to talk to?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Truly Understand the Fundamentals and the Path will be so much easier...
Here is the code that i am using:
d VAR Byte
v VAR Byte
encoded VAR Byte
FOR d=0 TO 7
··· IF v(d)=0 THEN
····· encoded(d*2) = 0
····· encoded(d*2+1) = 1
··· ELSE
····· encoded(d*2) = 1
····· encoded(d*2+1) = 0
··· ENDIF
NEXT
DEBUG BIN encoded
END
If you look back at the program code that I originally provided, you will see that the variable "encoded" is supposed to be a WORD sized variable, not a BYTE sized variable. Change that and you should be good to go.
The input to the routine is a BYTE, and the output is TWO BYTES, or a WORD.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->
d VAR Byte
v VAR Byte 'Addressed below as an implied 8 bit array
encoded VAR Word 'Addressed below as an implied 16 bit array
v=(%11)
FOR d=0 TO 7
··· IF v(d)=0 THEN
····· encoded(d*2) = 0
····· encoded(d*2+1) = 1
··· ELSE
····· encoded(d*2) = 1
····· encoded(d*2+1) = 0
··· ENDIF
NEXT
DEBUG DEC encoded
END
I doubt you are doing what you think you're doing. Here is your existing code with my added comments:
d VAR Byte
v VAR Byte 'Addressed below as an implied 8 bit array
encoded VAR Word 'Addressed below as an implied 16 bit array
v=(%11) '<<== This sets two BITs to ONE. Did you mean instead to set it to HEX 11?
v=$11··· '<<== If so, set the·BYTE to HEX 11 as shown
FOR d=0 TO 7
··· IF v(d)=0 THEN
····· encoded(d*2) = 0
····· encoded(d*2+1) = 1
··· ELSE
····· encoded(d*2) = 1
····· encoded(d*2+1) = 0
··· ENDIF
NEXT
DEBUG DEC encoded 'This displays contents·as a decimal number - not very useful
DEBUG HEX encoded 'This displays the·contents as a HEX number - more useful
DEBUG BIN encoded 'This shows the entire BINARY string - MOST useful!
END
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->
The comments state that encoded is accessed as an array of bits.
However, since encoded is defined as a WORD (or BYTE) in the above examples, the indexing will index thru the implied array of WORDs (or BYTEs).
Should not the variable encoded be defined as a BIT?
The other, probably·key,·problem is that the statement·DEBUG encoded sends only the first of the implied array values to the monitor.· The probram will need to embed the DEBUG statement within a loop to interate over all of the array elements.
Alternatively, a WORD alias could be defined for the BIT array, and then the value of that WORD alias variable be sent to the monitor by the single DEBUG statement.
I didn't program this in the IDE and verify it, so I might be wrong.
Daniel
You are of course correct, and my error in doing the earlier conversion. It points to a significant difference in the way arrays are handled between the two different dialects of Basic <sigh>. The following should give full BIT-width and it removes a small inefficiency:
'Manchester Encoding Subroutine
'Input of 0 ==> 01 in output
'Input of 1 ==> 10 in output
d VAR NIB
v VAR BIT(8) 'Addressed below as an 8 bit array - input
encoded VAR·BIT(16) ·'Addressed below as a 16 bit array - output
v=%11
FOR d=0 TO 7
··· IF v(d)=0 THEN
····· encoded(d*2) = 0
····· encoded(d*2+1) = 1
··· ELSE
····· encoded(d*2) = 1
····· encoded(d*2+1) = 0
··· ENDIF
NEXT
DEBUG·HEX4 encoded
END
Sorry I didn't pick that up earlier! Thanks to Daniel for spotting it.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->
The PBP example manipulates the WORD encoded in a bitwise fashion by references to encoded.0; wouldn't PBASIC do the same by referencing the WORD encoded as encoded.bit0?
Daniel
Yes, you could do it manually that way, bit-by-bit, but it's my understanding you can't index through it in that manner in PBASIC. I certainly stand to be corrected. This is the most confusing difference (for me) between the two dialects.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->
In the IDE help file, near the bottom, just above the section on "scratchpad ram", arrays and varible modifiers are discussed and this sentence appears:
Daniel
I guess this is what it wants (below) but I have no way of testing it, or even seeing if it is syntactically correct. I'm not using a computer that has the PBASIC IDE on it at the moment- sorry.
'Manchester Encoding Subroutine
'Input of 0 ==> 01 in output
'Input of 1 ==> 10 in output
d VAR NIB
v VAR BIT(8) 'Addressed below as an 8 bit array - input
encoded VAR·BIT(16) ·'Addressed below as a 16 bit array - output
v=%11
FOR d=0 TO 7
··· IF v(d)=0 THEN
····· encoded.lowbit(d*2) = 0
····· encoded.lowbit(d*2+1) = 1
··· ELSE
····· encoded.lowbit(d*2) = 1
····· encoded.lowbit(d*2+1) = 0
··· ENDIF
NEXT
DEBUG·HEX4 encoded
END
If that doesn't do it, I'll leave it in someone elses more capable hands.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->
I think this works.
Ryan
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Ryan Clarke
Parallax Tech Support
RClarke@Parallax.com
Post Edited (Ryan Clarke (Parallax)) : 3/7/2006 11:30:43 PM GMT
And look what Jeff and I came up with.... [noparse]:)[/noparse]
Ryan
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Ryan Clarke
Parallax Tech Support
RClarke@Parallax.com
Thanx a million·for all of your efforts.··I greatly appreciate the·help!
-Hubert