Shop OBEX P1 Docs P2 Docs Learn Events
Manchester encoding — Parallax Forums

Manchester encoding

HubertHubert Posts: 22
edited 2006-03-08 03:02 in BASIC Stamp
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 confused.gif??

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-02-14 21:00
    Hubert -

    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
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-02-15 15:12
    If you 'bit-bang' a port to implement a Manchester code at 9600 baud, then each bit will be 104 uSec wide. I believe this is faster than the PBasic can run in the BS2, so this approach won't work that fast on a BS2.

    '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?
  • Tom WalkerTom Walker Posts: 509
    edited 2006-02-15 16:10
    Bit-banging just happens to be discussed in the latest Servo.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Truly Understand the Fundamentals and the Path will be so much easier...
  • HubertHubert Posts: 22
    edited 2006-03-03 06:07
    I have tried to execute the modified code that bruce suggested.··However, I·have not been able to obtain the desired results.· When I try to debug the binary values contained·within the variable 'encoded' all·I get is a single digit.· It seems as if the program is not making the necessary iterations????

    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
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-03-03 07:23
    Hubert -

    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 -->
  • HubertHubert Posts: 22
    edited 2006-03-07 02:47
    I still dont know what I am doing wrong... Maybe its the way I am debugging.· I made the modifications that you(Bruce) suggested and I am still not getting what I need.· I was hoping to see·"1010"·when· I debugged encoded.· However,·all I get is one digit that is always the same as·the first digit in·v.· Is there possibly a problem with the value that I am assigning to v?· Here is the code again:



    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
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-03-07 03:18
    Hubert -

    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 -->
  • HubertHubert Posts: 22
    edited 2006-03-07 03:28
    I would like to simulate a binary input to the system with v and I want to debug the encoded binary string. Should "v" have a hex or binary formatter? I am still only getting one character at the output.
  • danieldaniel Posts: 231
    edited 2006-03-07 05:54
    It seems to me that the use of the variable encoded actually differs from the comments at its declaration.

    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
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-03-07 07:02
    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 -->
  • danieldaniel Posts: 231
    edited 2006-03-07 13:30
    Bruce,

    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
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-03-07 13:59
    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 -->
  • danieldaniel Posts: 231
    edited 2006-03-07 15:17
    Bruce,

    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:
    The modifier changes the meaning of the index value to match its own size.

    Daniel
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-03-07 19:07
    Gents -

    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 -->
  • Ryan ClarkeRyan Clarke Posts: 738
    edited 2006-03-07 22:33
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    'Manchester Encoding Subroutine
    
    'Input of 0 ==> 01 in output
    
    'Input of 1 ==> 10 in output
    
    d         VAR      Nib
    v         VAR      Byte            'Addressed below as an 8 bit array - input
    encoded   VAR      Word            'Addressed below as a 16 bit array - output
    
    v = %00000011
    
    FOR d= 0 TO 7
    
        IF v.BIT0(d)=0 THEN
          encoded.BIT0(d*2) = 0
          encoded.LOWBIT((d*2)+1) = 1
        ELSE
          encoded.LOWBIT(d*2) = 1
          encoded.LOWBIT((d*2)+1) = 0
        ENDIF
    
    NEXT
    
    DEBUG BIN encoded
    END
    



    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
  • Ryan ClarkeRyan Clarke Posts: 738
    edited 2006-03-07 23:05
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    
    'Manchester Encoding Subroutine
    
    'Input of 0 ==> 01 in output
    
    'Input of 1 ==> 10 in output
    
    d         VAR      Nib
    v         VAR      Byte            'Addressed below as an 8 bit array - input
    encoded   VAR      Word            'Addressed below as a 16 bit array - output
    
    v = %11111111
    
    FOR d = 7 TO 0
    
        encoded = (encoded << 2)| (%1 << v.BIT0(d))
    
    NEXT
    
    DEBUG BIN16 encoded
    END
    



    And look what Jeff and I came up with.... [noparse]:)[/noparse]

    Ryan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Ryan Clarke
    Parallax Tech Support

    RClarke@Parallax.com
  • danieldaniel Posts: 231
    edited 2006-03-07 23:32
    Bravo!
  • HubertHubert Posts: 22
    edited 2006-03-08 03:02
    gents,

    Thanx a million·for all of your efforts.··I greatly appreciate the·help!

    -Hubert
Sign In or Register to comment.