Help with serin, serout, Manchester encoding


Closed Thread
Results 1 to 31 of 31

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Encoding for RF communications

    Simple RF receivers need a preamble to condition the data slicer. I have had great success with low power Rf comms by using a pseudo Manchester strategy.

    Manchester code is usually recommended because it guarantees at least one transition per bit so a long run of 000 or 111 can be successfully decoded.

    The following also works. Assuming a 4 bit nibble is to be sent, build a character of start bit, 8 data bits and one or preferably 2 or more stop bits. Use Char_Pacing to get extra stop bits. 8 bit bytes need to be broken into two halves before sending. A checksum on any data block is highly recommended.

    Byte bit 0 is nibble bit 0
    Byte bit 1 is inverted nibble bit 0
    Byte bit 2 is nibble bit 1
    Byte bit 3 is inverted nibble bit 1
    Byte bit 4 is nibble bit2
    Byte bit 5 is inverted nibble bit 2
    Byte bit 6 is nibble bit 3
    Byte bit 7 is inverted nibble bit 3

    Now transmit this character many times - say 32 or more. The first few characters act as the preamble and the data stream is very nearly 50:50 duty cycle so the receiver data slicer is happy. Depending on link characteristics you may be able to cut the transmitted data down to 16 or even less repetitions.

    Perform tests on the received character stream to make sure you receive it, say, at least 3 consecutive times, where the characters are identical to each other AND the data structure of bit/inverted-bit is obeyed. Activating parity can also give you an extra check.

    I have used this with expensive 154 MHz transceivers and cheap 315 and 433 MHz Tx/Rx modules with great success.

    HTH
    Brian

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by BrianT View Post
    The following also works. Assuming a 4 bit nibble is to be sent, build a character of start bit, 8 data bits and one or preferably 2 or more stop bits. Use Char_Pacing to get extra stop bits.
    Brian
    I agree with your strategy almost 100% ('cept I use lookup/lookdown tables).
    But about the Char_Pacing above...
    If the Char_Pacing value is too large (over a few of ms, 5ms for the TX/RX modules I use according to the spec's), the data slicer will lose it's mind and basically every byte will need a preamble.

  3. #3


    Did you find this post helpful? Yes | No

    Default Char_Pacing limits

    You are correct that setting Char_Pacing too long will defeat the purpose. The reason for adding an extra one or two stop bits is to prevent the problem where the SERIN software UART can misframe on a repetitive pattern and what it takes for the start bit may well be bit 3, 4, 5, etc. By giving the system 2 or 3 stop bits the SERIN command will fall into sync in just a couple of characters.

    HTH
    Brian

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by BrianT View Post
    You are correct that setting Char_Pacing too long will defeat the purpose. The reason for adding an extra one or two stop bits is to prevent the problem where the SERIN software UART can misframe on a repetitive pattern and what it takes for the start bit may well be bit 3, 4, 5, etc. By giving the system 2 or 3 stop bits the SERIN command will fall into sync in just a couple of characters.

    HTH
    Brian
    Good call, never really thought of that. I'm going to add that to a couple of my RF projects and see what happens. As it is, at 9600, I'm losing a few bytes here and there at the longer ranges. Maybe the 'extra stop bit' will help out.

  5. #5
    Join Date
    Mar 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    I got serin to partially work on the receiver side (with and without wireless). The problem I am having now is it seems like after I decode it I have about 4 extra bits at the end of my decoded sequence. How do I get rid of it?

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Cool

    Quote Originally Posted by oneohthree View Post
    I got serin to partially work on the receiver side (with and without wireless). The problem I am having now is it seems like after I decode it I have about 4 extra bits at the end of my decoded sequence. How do I get rid of it?
    And we're supposed to be able to help you fix that because we can see what your code looks like, right?

  7. #7
    Join Date
    Mar 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    Here is my revised code:

    ------------------------------------------------------------------------------------------------------------------
    'Transmitter Code
    Include "Modedefs.bas"
    v var word 'v is a byte to be encoded
    counter var byte 'counter is a byte
    encoded var word 'encoded is a word sized variable that holds the encoded byte v
    'Code to be transmitted
    v = %1101 '1101 in Binary
    'Manchester Encoding
    For counter = 0 to 3 'number of bits to be encoded (4 bits)
    If v.0[counter]=0 Then encoded.0[counter*2]=0 'if it is a zero make the first bit 0 encoded.0[counter*2+1]=1 'make the second bit 1
    Else
    encoded.0[counter*2]=1 'if it is a 1 make the first bit 1
    encoded.0[counter*2+1]=0 'make the second bit 0
    EndIf
    Next counter
    'Serial Out
    serout PORTB.0, t2400, [$55,$55, $55, $aa, encoded] 'serial out in pin 0 using non-inverted 2400 bps to encoded
    --------------------------------------------------------------------------------------------------------------------------------
    'Receiver Code
    Include "Modedefs.bas"
    s var word 's is a byte to be encoded
    B0 var byte 'B0 is a byte
    B1 var byte 'B1 is a byte
    counter var byte
    encoded1 var word 'encoded1 word sized variable that holds the encoded byte v
    action var byte 'action variable
    alert var byte 'alert variable
    B0=1 'hardwired address
    B1=1 'hardwired address

    'Decoding
    waitfor55:
    serin PORTB.0, t2400, encoded1: if encoded1 = $55 then goto waitfor55
    waitforaa:
    serin PORTB.0, t2400, encoded1: if encoded1 = $aa then goto waitforaa

    serin PORTB.0, t2400, encoded1 'serial in to pin 0, a non-inverting 2400 bps to encoded1
    serout PORTB.3, t2400, [encoded1]
    loop:
    For counter = 0 to 3
    s.0[counter]=encoded1.0[counter*2] 'proceed through counter and copy encoded1 to s
    Next counter
    goto loop
    End
    serout PORTB.3, t2400, [s] 'serial out to pin 3, a non-inverting 2400 bps, to s

    'Tracking code
    If (s.2==0) & (s.3==0) Then 'If the code matches 00 then it is in the tracking mode

    If (s.0==B0) & (s.1==B1) Then 'If the address matches then
    s.2=1 'Change the third bit to 1
    action = s.2 'action becomes 1
    alert = 0 'alert becomes 0
    serout PORTB.1, t2400, [s] 'Send the changed word back, serial out to pin 1, a non-inverting 2400 bps, to s
    Else
    s.2=0 'Since the addresses does not match do not change anything
    action = s.2 'action becomes 0
    alert = 0 'alert becomes 0
    serout PORTB.1, t2400, [s] 'Send the changed word back, serial out to pin 1, a non-inverting 2400 bps, to s
    Endif
    Endif

    'Tracking and Finding code
    If (s.2==0) & (s.3==1) Then 'If the code matches 01 then it is in the tracking and finding mode

    If (s.0==B0) & (s.1==B1) Then 'If the address matches then
    s.2=1 'Change the third bit to 1
    action=s.2 'action becomes 1
    alert=1 'alert becomes 1
    serout PORTB.1, t2400, [s] 'Send the changed word back, serial out to pin 1, a non-inverting 2400 bps, to s
    High 2
    Else
    s.2=0 'Since the address does not match do not change anything
    action=s.2 ;action becomes 0 and alert is also 0
    alert=0
    serout PORTB.1, t2400, [s] 'Send te changed word back, serial out to pin 1, a non-inverting 2400 bps, to s
    Endif
    Endif

Similar Threads

  1. A Serial GLCD 128x64 Simple Project
    By Oldspring in forum Off Topic
    Replies: 0
    Last Post: - 8th March 2010, 21:58
  2. Serout to serial servo
    By azmax100 in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 12th August 2009, 17:46
  3. PIC16f877 code crosses boundary @800h
    By inventosrl in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 6th April 2009, 23:03
  4. Advice-scrutiny for my serial controller
    By kevlar129bp in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 13th December 2008, 18:11
  5. SerIn and SerOut
    By Dwayne in forum FAQ - Frequently Asked Questions
    Replies: 0
    Last Post: - 21st July 2004, 16:54

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts