Help with serin, serout, Manchester encoding


Closed Thread
Results 1 to 31 of 31
  1. #1
    Join Date
    Mar 2007
    Posts
    42

    Default Help with serin, serout, Manchester encoding

    I am trying to transmit 4 bits from a TLP-315 transmitter to a RLP-315 receiver for a project. At the receiver, I am taking the four bits and performing some comparisons on it and getting an output. I am using two PIC16F88 microcontrollers. I was wondering whether I coded the Manchester coding and used the serin, serout correct. Also, since I am using Manchester coding, do I have to send a preamble? I have attached my code below.

    'Transmit
    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 = 1010
    '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
    encoded.0[counter*2+1]=1
    Else
    encoded.0[counter*2]=1
    encoded.0[counter*2+1]=0
    EndIf
    Next counter
    'Serial Out
    serout PORTB.0, n2400, [encoded]


    'Receive

    '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=0 'hardwired address
    B1=1 'hardwired address

    'Decoding
    loop:
    serin PORTB.0, n2400, [encoded1]
    For counter = 0 to 3
    s.0[counter]=encoded1.0[counter*2]
    Next counter
    goto loop
    End

    '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
    alert = 0
    serout PORTB.1, n2400, [s] 'Send the changed word back
    Else
    s.2=0 'Since the addresses does not match do not change anything
    action = s.2
    alert = 0
    serout PORTB.1, n2400, [s] 'Send the changed word back
    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
    alert=1
    serout PORTB.1, n2400, [s] 'Send the changed word back
    High 2
    Else
    s.2=0 'Since the address does not match do not change anything
    action=s.2
    alert=0
    serout PORTB.1, n2400, [s] 'Send te changed word back
    Endif
    Endif

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by oneohthree View Post
    v = 1010
    For one thing, sending 1010 isn't going to work. 1010 is one thousand and ten, not 10 or $A). You probably want v = %1010.
    Last edited by skimask; - 30th March 2007 at 18:00.

  3. #3
    Join Date
    Mar 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    Also, I tested my transmitter and receiver with the microcontrollers. I was able to transmit the 1101 sequence from the transmitter to the receiver. I got the encoded version of the 1101 sequence. But, the microcontroller on the receiver end the serin command does not work for the microcontroller. When I look at the pins where I have serout on I don't see any signal. Did I do something incorrect?

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Question

    Quote Originally Posted by oneohthree View Post
    But, the microcontroller on the receiver end the serin command does not work for the microcontroller.
    What? Doesn't make any sense...

  5. #5
    Join Date
    Mar 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    I don't know what is wrong either. Somehow the serin command isnt taking the data that I send in from the receiver. I check the output at pin 1 and pin 3 using a logic analyzer and I get nothing. Is there something wrong with my receiver code. Also, should I send data in inverted (n2400) or non-inverted (t2400)?

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by oneohthree View Post
    I don't know what is wrong either. Somehow the serin command isnt taking the data that I send in from the receiver. I check the output at pin 1 and pin 3 using a logic analyzer and I get nothing. Is there something wrong with my receiver code. Also, should I send data in inverted (n2400) or non-inverted (t2400)?
    No, I meant your sentence didn't make any sense...

  7. #7
    Join Date
    Mar 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    Do you have any suggestion on how I can get my serin command to work correctly?

  8. #8
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by oneohthree View Post
    Do you have any suggestion on how I can get my serin command to work correctly?
    Yes, READ the WHOLE 'Serin SerOut Problem' thread, not to mention a half a dozen other PIC-PIC communication threads in the 'Serial' section of the forum, just like I've suggested in the other thread and more than once. And start simple and build up. You're still using the RF modules, but I don't see anything that says you're sure it all works without the RF modules. I don't see anything that says you're sure the whole thing works just between the 2 PIC's, a couple of buttons and a couple of LEDs.

  9. #9


    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

  10. #10
    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.

  11. #11


    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

  12. #12
    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.

  13. #13
    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?

  14. #14
    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?

  15. #15
    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

  16. #16
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    [QUOTE=oneohthree;35632]Here is my revised code:

    That code is entirely TOO complicated for the job you are trying to get done...
    Hang tight for a bit...

  17. #17
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    'Transmitter Code
    Include "Modedefs.bas"
    v var byte : counter var byte : encoded var byte : v = %1101
    main:
    For counter = 0 to 3
    If v.0[counter]=0 Then
    encoded.0[counter*2]=0
    Else
    encoded.0[counter*2]=1 : encoded.0[counter*2+1]=0
    EndIf
    Next counter : serout PORTB.0, t2400, [ $55 , $55 , $55 , $aa , encoded ]
    goto main



    'Receiver Code
    Include "Modedefs.bas"
    s var byte : B0 var byte : B1 var byte : counter var byte : cnt55 var byte
    encoded1 var byte : action var byte : alert var byte : B0=1 : B1=1

    main:
    cnt55 = 0

    waitfor55:
    serin PORTB.0, t2400, encoded1

    if encoded1 = $55 then
    cnt55 = cnt55 + 1 'got $55, inc cnt55 by 1
    if cnt55 = 3 then goto waitforaa 'if we got 3 in a row check for $aa
    else
    goto main 'if not $55, reset counter at main and restart
    endif
    goto waitfor55

    waitforaa:
    serin PORTB.0, t2400, encoded1
    if encoded1 <> $aa then goto main 'restart, didn't get right leader

    serin PORTB.0, t2400, encoded1 : serout PORTB.3, t2400, [encoded1]

    For counter=0 to 3:s.0[counter]=encoded1.0[counter*2]:Next counter
    serout PORTB.3, t2400, [s]

    'Tracking code
    If (s.2==0) & (s.3==0) Then
    If (s.0==B0) & (s.1==B1) Then
    s.2=1:action = s.2:alert = 0:serout PORTB.1, t2400, [s]
    Else
    s.2=0:action = s.2:alert = 0:serout PORTB.1, t2400, [s]
    Endif
    Endif

    'Tracking and Finding code
    If (s.2==0) & (s.3==1) Then
    If (s.0==B0) & (s.1==B1) Then 'If the address matches then
    s.2=1:action=s.2:alert=1:serout PORTB.1, t2400, [s]:High 2
    Else
    s.2=0:action=s.2:alert=0:serout PORTB.1, t2400, [s]
    Endif
    Endif

    Your logic for getting the leader bytes and stuff was messed up, so I rewrote it a bit.

    But I still think this is entirely too convoluted.
    What exactly are you trying to do? Because this seems a bit goofy to me, all this bit manipulation and such. Even though it's a short program, it just seems to me like you'll get yourself stuck in future trying to figure out what you were trying to do in the first place.

  18. #18
    Join Date
    Mar 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    This is a part of the 2 way paging system that I am doing for a project. Basically what I am trying to do is take 4 bits from the transmitter that I get at the receiver end and do some operations on it. I am assuming that the first two bits are the address and the last two bits are the mode. There are two modes tracking and finding. In each case, I am first checking the last two bits to see which mode it is. After I do that, I am checking if the first two bits of address matches. If the address matches, it will change the third bit (kind of like an acknowledgement) and output the result somewhere. If it does not match it will not change the code and output it back. In the find mode, it does the same thing except it will make a pin high in order to set off an alert like a LED. Is this making any sense?

  19. #19
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by oneohthree View Post
    This is a part of the 2 way paging system that I am doing for a project. Basically what I am trying to do is take 4 bits from the transmitter that I get at the receiver end and do some operations on it. I am assuming that the first two bits are the address and the last two bits are the mode. There are two modes tracking and finding. In each case, I am first checking the last two bits to see which mode it is. After I do that, I am checking if the first two bits of address matches. If the address matches, it will change the third bit (kind of like an acknowledgement) and output the result somewhere. If it does not match it will not change the code and output it back. In the find mode, it does the same thing except it will make a pin high in order to set off an alert like a LED. Is this making any sense?
    Ok, that makes a bit more sense....
    A bit (pun intended) simpler, maybe????......
    Take a sent byte...invert it and send it back as some sort of acknowledge.
    For instance: (using 4 bits, $5, $A, $0, $F, are already taken, so can't use those)
    Send $1, return $E, acknowledged
    Send $2, return $D, and so on...
    Anything else returned would be 'not' acknowledged...
    Leaves you with $1,2,3,4,6,7 for send codes ($E,D,C,B,9,8 for returned codes).... if 6 codes is enough for you...
    That way you could set the 'address' for each pager/receiver with 3 DIP switches (with 2 unused combo's)...

  20. #20
    Join Date
    Mar 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    Would it matter if I used t2400 or n2400 in my code for serin/serout? I used n2400 in my code and everything is encoding and decoding correctly. I know this because the number at the receiver checks when I write the result into EEPROM and convert the hexadecimal to binary. Also, it seems like the receiver code does not go to the if statements at the end. For example, if I receive 1100 it serout something to pin 1. When I put a LED at pin 1 I don't see anything. Is there anyway I can fix it? Would I have to change my decoded message from hexadecimal to binary in order to allow me to manipulate the bits?

    'Transmitter Code
    'Transmitter Code
    Include "Modedefs.bas"
    main:
    v var byte : counter var byte : encoded var byte : v = %1100
    'Manchester encoding
    For counter = 0 to 3
    If v.0[counter]=0 Then
    encoded.0[counter*2]=0 : encoded.0[counter*2+1]=1 'If it is a zero make the first bit 0 and second bit 1
    Else
    encoded.0[counter*2]=1 : encoded.0[counter*2+1]=0 'If not then make first bit 1 and second bit 0
    EndIf
    Next counter : serout PORTB.0, n2400, [ $55 , $55 ,$aa , encoded ] 'send out code
    write 0, encoded
    High 1
    Pause 1000
    Low 1
    goto main


    'Receiver Code
    Include "Modedefs.bas"
    s var byte : B0 var byte : B1 var byte : 's is byte to be encoded, B0 and B1 is byte
    counter var byte : cnt55 var byte: 'counter is byte and cnt55 is byte
    encoded1 var byte : action var byte : alert var byte : B0=1 : B1=1
    'encoded1 word sized variable that holds encoded byte v, alert variable, action variable, B0 and B1 hardwired address
    s=0: encoded1=0
    main:
    cnt55 = 0 'counter for $55 is zero

    waitfor55:
    serin PORTB.0, t2400, encoded1 'take in data at pin 0 and place in encoded1
    if encoded1 = $55 then
    cnt55 = cnt55 + 1 '$55 read, increase cnt55 by 1
    if cnt55 = 2 then goto waitforaa 'if $55 occurs 3 times in a row check for $aa
    else
    goto main 'if not $55, reset counter at main and restart
    endif
    goto waitfor55

    waitforaa:
    serin PORTB.0, n2400, encoded1
    if encoded1 <> $aa then goto main 'restart, incorrect leader bit

    serin PORTB.0, n2400, encoded1 : serout PORTB.4, n2400, [encoded1] 'take in from pin 0 to encoded 1 output to pin 3 contens in encoded1
    write 0,encoded1
    High 7
    Pause 10000
    Low 7


    'Decoding
    For counter=0 to 3:s.0[counter]=encoded1.0[counter*2]:Next counter
    serout PORTB.3, n2400, [s] 'Ouptut decoded message
    write 1, s
    High 6
    Pause 10000
    Low 6

    'Tracking code
    If (s.2==0) & (s.3==0) Then 'if the code matches 00
    If (s.0==B0) & (s.1==B1) Then 'if the address matches then
    s.2=1: action = s.2: 'set the third bit to 1 and send it back
    alert = 0:
    serout PORTB.1, n2400, [s] 'output to pin 1 value in s
    Else
    s.2=0: action = s.2: 'do not change anything and send back
    alert = 0:
    serout PORTB.2, n2400, [s] 'output to pin 1 value in s
    Endif
    Endif

    'Tracking and Finding code
    If (s.2==0) & (s.3==1) Then 'if code matches 01 then
    If (s.0==B0) & (s.1==B1) Then 'If the address matches then
    s.2=1: action=s.2: 'set the third bit to 1 and send back
    alert=1:
    serout PORTB.6, n2400, [s]: 'output to pin 1 value in s
    High 2 'set alert to 1 and make pin 2 high.
    Else
    s.2=0: action=s.2: 'do not change anything and send back
    alert=0:
    serout PORTB.5, n2400, [s] 'output to pin 1 value in s
    Endif
    Endif
    Last edited by oneohthree; - 4th April 2007 at 06:49.

  21. #21
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by oneohthree View Post
    'Transmitter Code
    Include "Modedefs.bas"
    main:
    v var byte : counter var byte : encoded var byte : v = %1100
    'Manchester encoding
    For counter = 0 to 3
    If v.0[counter]=0 Then
    encoded.0[counter*2]=0 : encoded.0[counter*2+1]=1 'If it is a zero make the first bit 0 and second bit 1
    Else
    encoded.0[counter*2]=1 : encoded.0[counter*2+1]=0 'If not then make first bit 1 and second bit 0
    EndIf
    Next counter : serout PORTB.0, n2400, [ $55 , $55 , $55 , $55 , $55 , $55 ,$aa , encoded ] 'send out code
    write 0, encoded
    High 1
    Pause 1000
    Low 1
    goto main


    'Receiver Code
    Include "Modedefs.bas"
    s var byte : B0 var byte : B1 var byte : 's is byte to be encoded, B0 and B1 is byte
    counter var byte : cnt55 var byte : cntaa var byte 'counter is byte and cnt55 is byte
    encoded1 var byte : action var byte : alert var byte : B0=1 : B1=1
    'encoded1 word sized variable that holds encoded byte v, alert variable, action variable, B0 and B1 hardwired address
    s=0: encoded1=0
    main:
    cnt55 = 0 : cntaa = 0 'counter for $55 & $aa is zero

    waitfor55:
    serin PORTB.0, t2400, encoded1 'take in data at pin 0 and place in encoded1
    if encoded1 = $55 then
    cnt55 = cnt55 + 1 '$55 read, increase cnt55 by 1
    if cnt55 > 3 then goto waitforaa 'if $55 occurs more than 3 times in a row check for $aa
    else
    goto main 'if not $55, reset counter at main and restart
    endif
    goto waitfor55

    waitforaa:
    serin PORTB.0, n2400, encoded1
    if encoded1 <> $aa then
    cntaa = cntaa + 1
    if cntaa > 5 then goto main 'restart, incorrect leader bit
    endif
    serin PORTB.0, n2400, encoded1 : serout PORTB.4, n2400, [encoded1] 'take in from pin 0 to encoded 1 output to pin 3 contens in encoded1
    write 0,encoded1
    High 7
    Pause 10000
    Low 7


    'Decoding
    For counter=0 to 3:s.0[counter]=encoded1.0[counter*2]:Next counter
    serout PORTB.3, n2400, [s] 'Ouptut decoded message
    write 1, s
    High 6
    Pause 10000
    Low 6

    'Tracking code
    If (s.2==0) & (s.3==0) Then 'if the code matches 00
    If (s.0==B0) & (s.1==B1) Then 'if the address matches then
    s.2=1: action = s.2: 'set the third bit to 1 and send it back
    alert = 0:
    serout PORTB.1, n2400, [s] 'output to pin 1 value in s
    Else
    s.2=0: action = s.2: 'do not change anything and send back
    alert = 0:
    serout PORTB.2, n2400, [s] 'output to pin 1 value in s
    Endif
    Endif

    'Tracking and Finding code
    If (s.2==0) & (s.3==1) Then 'if code matches 01 then
    If (s.0==B0) & (s.1==B1) Then 'If the address matches then
    s.2=1: action=s.2: 'set the third bit to 1 and send back
    alert=1:
    serout PORTB.6, n2400, [s]: 'output to pin 1 value in s
    High 2 'set alert to 1 and make pin 2 high.
    Else
    s.2=0: action=s.2: 'do not change anything and send back
    alert=0:
    serout PORTB.5, n2400, [s] 'output to pin 1 value in s
    Endif
    Endif

    Try that out. Send more $55's for the leader (more time for RX to sync up), wait for at least 3 of them. When you get 3, start watching for an $AA. If you don't get an $AA within 5 bytes, restart the process.

    As far as changing hex to binary...PBP doesn't care one way or the other. You are working with a byte. PBP can work on bytes, bits, hex, binary, doesn't matter. Just a matter of looking up the right math operator and using it.

  22. #22
    Join Date
    Mar 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    The encoding and decoding works properly, I have 4 $55's now. I also changed my logic around in my receiver code so that it checks the address (check the first two bits) then determines the mode (checks the last two bits). But, I discover that my microcontroller repeatedly doesn't execute the logic correctly. Even if the first two bits match it always jumps to the statement (indicated by the **** in my code) where it writes to the third memory location in the EEPROM.
    I do not understand why it does that.

    Receiver code:

    Include "Modedefs.bas"
    s var byte : B0 var bit : B1 var bit : 's is byte to be encoded, B0 and B1 is byte
    counter var byte : cnt55 var byte: 'counter is byte and cnt55 is byte
    encoded1 var byte : action var byte : alert var byte : B0=1 : B1=1
    'encoded1 word sized variable that holds encoded byte v, alert variable, action variable, B0 and B1 hardwired address
    s=0: encoded1=0

    main:
    cnt55 = 0 'counter for $55 is zero

    waitfor55:
    serin PORTB.0, t2400, encoded1 'take in data at pin 0 and place in encoded1
    if encoded1 = $55 then
    cnt55 = cnt55 + 1 '$55 read, increase cnt55 by 1
    if cnt55 = 4 then goto waitforaa 'if $55 occurs 4 times in a row check for $aa
    else
    goto main 'if not $55, reset counter at main and restart
    endif
    goto waitfor55

    waitforaa:
    serin PORTB.0, n2400, encoded1
    if encoded1 <> $aa then goto main 'restart, incorrect leader bit

    serin PORTB.0, n2400, encoded1 : 'serout PORTB.4, n2400, [encoded1] take in from pin 0 to encoded 1 output to pin 3 contens in encoded1
    write 0,encoded1
    High 7
    Pause 2000
    Low 7


    'Decoding
    For counter=0 to 3:s.0[counter]=encoded1.0[counter*2]:Next counter
    serout PORTB.3, n2400, [s] 'Ouptut decoded message
    write 1, s
    High 6
    Pause 2000
    Low 6


    if (s.0=1) AND (s.1=1) then 'Check if address matches
    goto L2
    else *****
    write 3, s
    High 2
    Pause 2000
    Low 2
    End
    endif


    L2:
    if (s.2=0) And (s.3=0) then 'if the address Tracking then
    s.2=1: action = s 'set the third bit to 1 and send it back
    alert = 0: serout PORTB.1, n2400, [action] 'output to pin 1 value in s
    write 2, action
    High 1
    Pause 2000
    Low 1
    endif

    if (s.2=0) AND (s.3=1) then 'If the address Tracking and Finding then
    s.2=1: action=s 'set the third bit to 1 and send back
    alert=1: serout PORTB.4, n2400, [action]: 'output to pin 1 value in s
    write 4, action
    High 4
    Pause 2000
    Low 4
    endif

    End

  23. #23
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    if (s.0=1) AND (s.1=1) then 'Check if address matches
    goto L2
    else *****
    write 3, s
    High 2
    Pause 2000
    Low 2
    End
    endif



    Why is there an 'End' in the middle of your code?

  24. #24
    Join Date
    Mar 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    If the address (first two bits) don't match then I would end the program. I took that out but it seems like it is not advancing towards the correct loop. When my decoded data is stored in the byte s the first bit of my sequence should be coded a s.0 or do I have to define something else before doing s.0?

    Include "Modedefs.bas"
    s var byte : B0 var bit : B1 var bit : 's is byte to be encoded, B0 and B1 is byte
    counter var byte : cnt55 var byte: 'counter is byte and cnt55 is byte
    encoded1 var byte : action var byte : alert var byte : B0=1 : B1=1
    'encoded1 word sized variable that holds encoded byte v, alert variable, action variable, B0 and B1 hardwired address
    s=0: encoded1=0

    main:
    cnt55 = 0 'counter for $55 is zero

    waitfor55:
    serin PORTB.0, n2400, encoded1 'take in data at pin 0 and place in encoded1
    if encoded1 = $55 then
    cnt55 = cnt55 + 1 '$55 read, increase cnt55 by 1
    if cnt55 = 4 then goto waitforaa 'if $55 occurs 4 times in a row check for $aa
    else
    goto main 'if not $55, reset counter at main and restart
    endif
    goto waitfor55

    waitforaa:
    serin PORTB.0, n2400, encoded1
    if encoded1 <> $aa then goto main 'restart, incorrect leader bit

    serin PORTB.0, n2400, encoded1 : 'serout PORTB.4, n2400, [encoded1] take in from pin 0 to encoded 1 output to pin 3 contens in encoded1
    write 0,encoded1
    High 7
    Pause 2000
    Low 7


    'Decoding
    For counter=0 to 3:s.0[counter]=encoded1.0[counter*2]:Next counter
    'serout PORTB.3, n2400, [s] 'Ouptut decoded message
    write 1, s
    High 6
    Pause 2000
    Low 6


    if (s.bit0=0) AND (s.bit1=1) then 'Check if address matches
    goto L3
    else
    write 2,s
    High 2
    Pause 2000
    Low 2
    endif


    L3:
    if (s.bit2=0) And (s.bit3=0) then 'if the address Tracking then
    s.bit2=1: action = s 'set the third bit to 1 and send it back
    alert = 0: 'serout PORTB.1, n2400, [action] 'output to pin 1 value in s
    write 3, action
    High 1
    Pause 2000
    Low 1
    endif

    if (s.bit2=0) AND (s.bit3=1) then 'If the address Tracking and Finding then
    s.bit2=1: action=s 'set the third bit to 1 and send back
    alert=1: 'serout PORTB.4, n2400, [action]: 'output to pin 1 value in s
    write 4, action
    High 4
    Pause 2000
    Low 4
    endif

    End
    Last edited by oneohthree; - 4th April 2007 at 22:16.

  25. #25
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Are you sure that you are receiving at the RX the same thing you are sending at the TX?


    'Decoding
    For counter = 0 to 3 : s.0[counter] = encoded1.0[counter * 2 + 1] : Next counter

    Try that and see what happens...for grins...

  26. #26
    Join Date
    Mar 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    I think I figured it out I was reading the bits the wrong way. For example, when I send 2345 I originally assumed that s.0=2, s.1=3, s.2=4 and s.3=5 but it was in fact the other way. My code now works 90% of the time, occasionally my program will jump to if statements that it is not supposed to go to and write data there.

  27. #27
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by oneohthree View Post
    I think I figured it out I was reading the bits the wrong way. For example, when I send 2345 I originally assumed that s.0=2, s.1=3, s.2=4 and s.3=5 but it was in fact the other way. My code now works 90% of the time, occasionally my program will jump to if statements that it is not supposed to go to and write data there.
    If you're using the RF link, try the straight serial connection first. You can keep the encoding in there.
    If it still doesn't work, check your oscillators. If you're using the internal oscillators, probably should go to a crystal.

  28. #28
    Join Date
    Mar 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    I'm using a 4 MHz crystal oscillator. I'm sure that the stuff is encoding and decoding correctly since it is written correctly in my EEPROM and my logic works most of the time. It seems like my program is executing straight down and is going through incorrect if loops sometimes. For example, when I enter 0101 but somehow my microcontroller will write in memory location 3 and 4. It is only supposed to write in memory location 4. Is there anyway to clear the memory before I start my program? Also, is there anyway that I can prevent it from executing through other if loops? I have attached my new code below.

    Receiver code:
    Include "Modedefs.bas"
    s var byte : B0 var bit : B1 var bit : 's is byte to be encoded, B0 and B1 is byte
    counter var byte : cnt55 var byte: 'counter is byte and cnt55 is byte
    encoded1 var byte : action var byte : alert var byte : action2 var byte: B0=1 : B1=1
    'encoded1 word sized variable that holds encoded byte v, alert variable, action variable, B0 and B1 hardwired address
    s=0: encoded1=0

    main:

    maina:
    cnt55 = 0 'counter for $55 is zero

    waitfor55:
    serin PORTB.0, n2400, encoded1 'take in data at pin 0 and place in encoded1
    if encoded1 = $55 then
    cnt55 = cnt55 + 1 '$55 read, increase cnt55 by 1
    if cnt55 = 4 then goto waitforaa 'if $55 occurs 4 times in a row check for $aa
    else
    goto maina 'if not $55, reset counter at main and restart
    endif
    goto waitfor55

    waitforaa:
    serin PORTB.0, n2400, encoded1
    if encoded1 <> $aa then goto maina 'restart, incorrect leader bit

    serin PORTB.0, n2400, encoded1 : 'serout PORTB.4, n2400, [encoded1] take in from pin 0 to encoded 1 output to pin 3 contens in encoded1
    write 0,encoded1
    High 7
    Pause 2000
    Low 7


    'Decoding
    For counter=0 to 3:s.0[counter]=encoded1.0[counter*2]:Next counter
    'serout PORTB.3, n2400, [s] 'Ouptut decoded message
    write 1, s
    High 6
    Pause 2000
    Low 6


    if ((s.3==0) && (s.2==1)) then 'Check if address matches
    goto L3
    else
    write 2,s
    High 2
    Pause 2000
    Low 2
    End
    endif


    L3:
    if ((s.1==0) && (s.0==0)) then 'if the address Tracking then
    action = s: action.1=1'set the third bit to 1 and send it back
    alert = 0: 'serout PORTB.1, n2400, [action1] 'output to pin 1 value
    write 3, action
    High 1
    Pause 2000
    Low 1
    endif

    if ((s.1==0) && (s.0==1)) then 'If the address Tracking and Finding then
    action2 = s:action2.1=1 'set the third bit to 1 and send back
    alert=1: 'serout PORTB.4, n2400, [action2]: 'output to pin 1 value i
    write 4, action2
    High 4
    Pause 2000
    Low 4
    endif

    End

  29. #29
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by oneohthree View Post
    It seems like my program is executing straight down and is going through incorrect if loops sometimes.
    Well, yes, it is going to execute straight down (as it's laid out here in the forums). Read thru it. How is it supposed to NOT execute straight down? There's no jumps back to the main loop or anything anywhere (except for where the serial data is received).

  30. #30
    Join Date
    Mar 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    For my code, I use If ((s.3==0) && (s.2==1)) then
    goto L3

    Is it better to place L3 before the if statement or after it?
    Is it a good idea to do If ((s.3==0) && (s.2==1)) or should I separate the if statement?

  31. #31
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by oneohthree View Post
    For my code, I use If ((s.3==0) && (s.2==1)) then
    goto L3

    Is it better to place L3 before the if statement or after it?
    Is it a good idea to do If ((s.3==0) && (s.2==1)) or should I separate the if statement?
    If ( s.3 = 0 ) and ( s.2 = 1 ) is fine. (In practice I don't use ==, I always use =, same thing for the &&, I always use 'AND', easier to read)

    If you think the L3 before the if will work better, then try it.

Similar Threads

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

Members who have read this thread : 2

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