4 Bytes one button press


Closed Thread
Results 1 to 6 of 6
  1. #1

    Default 4 Bytes one button press

    ..transmitted via wireless reliably....!

    Hi all

    I have been playing around with things like serin/out, debug serin2/out2 and hserin/out.

    I am using hserout for this example with a TX and RX module from Rentron which work very well.

    I have two tiny problems at this stage though.

    1.I need to be able to on the press of the transmit button send 4

    2. The receiver must do some sort of check on what it has received.
    Now as I understand it this could be in the form of manchester coding on both sides, bit-wise compliment send and check,the use of endocder/decoder pairs (or code) and or a CRC or checksum.

    So I scoured the forums looking for something really simple to experiment with initially and came up with the code below.It's bulky and won't expand easily and what's more is I haven't tested it yet .. TIME... SIGH!

    I would just like some advice as to where or not I am on the right track (will the code even work ?) and possibly some extra options or thoughts, perhaps some simple maths on both sides ?

    I would lolve to hear your thoughts and experiences and possibly a little help

    Kind regards
    Dennis
    See my code below (4 bytes of which 3 will be constants)
    Code:
    TX side
    -------
    hserout [TRAIN,TRAIN,TRAIN,TRAIN,TRAIN,TRAIN,synch,hcode,dcode,pcode,hcode,dcode,pcode,data,data,data,data,data,data]
    
    'where hdcode,dcode and pcode are constant
    hcode con 31
    dcode con 32
    pcode con 11
    'data is either 0 or 1
    data var byte
    
    RX side
    -------
    
    hcode con 31
    dcode con 32
    pcode con 11
    
    address1 var byte
    address2 var byte
    address3 var byte
    address4 var byte
    address5 var byte
    address6 var byte
    data1
    data2
    data3
    data4
    data5
    data6
    
    hserin[WAIT(SYNK),address1, address2,address3,address4,address5,address6,data1,data2,data3,data4,data5,data6]
    
    IF ((address1 == hcode) && (address3 == hcode)) && ((address2 == pcode) &&(address4 == pcode)) && ((address2 == dcode) &&(address4 == dcode))then
    portb.0=1 'becuase all 3 address bits match
    else
    portb.1=0
    endif
    
    if ((data1 == data3) && (data2 == data4) && (data5 == data6)) then 
    'check on MATCH variable!
    'or I could use this
    'if data1 == data2 == data3 == data4 == data5 == data6 then 
    let data 1 = match 
    let match = portb

  2. #2
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default

    You mean a way to calc a checksum for four bytes?

    Here's an easy one, start with the hex value 3F, and XOR it with all four bytes on the way out:

    Checksum = 3F

    Checksum = Checksum ^ byte_1
    Checksum = Checksum ^ byte_2
    Checksum = Checksum ^ byte_3
    Checksum = Checksum ^ byte_4

    Send out via radio byte_1, byte_2, byte_3, Byte_4, Checksum.


    Now on the way in the receive device:


    Receive byte_1, byte_2, byte_3, Byte_4, Checksum.

    Checksum_Check = 3F
    Checksum_Check = Checksum_Check ^ byte_1
    Checksum_Check = Checksum_Check ^ byte_2
    Checksum_Check = Checksum_Check ^ byte_3
    Checksum_Check = Checksum_Check ^ byte_4

    IF Checksum_Check = Checksum THEN
    Data packet was valid
    ENDIF


    In your program you'd use an array for the data bytes and wrap that up in a FOR...NEXT loop
    So to send:

    Checksum = 3F
    FOR index = 0 TO 4
    send byte[index]
    Checksum = Checksum ^ byte[index]
    NEXT index
    send Checksum


    Cheers, Art.

  3. #3


    Did you find this post helpful? Yes | No

    Default A byte or a word ?

    Hi Art

    Thanks so much that's a very helpful tip indeed!

    I haven't tried this yet but I will as soon as I get a chance and feedback asap.

    Just curious if I need to declare CHECKSUM as a byte or a word ?
    For example if the byte is 255 Decimal aka 11111111 Binary, would there be any overflow into a second byte

    Kind regards

    Dennis

  4. #4
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default

    The checksum is a byte.
    If you XOR a number with any other number, there can never be an overflow.

    The XOR operation tests each bit of the two bytes operated on.
    If the first two bits of each value are the same, the first bit of the new byte is 0.
    If the first two bits of each value are different, the first bit of the new number is 1.

    0000111 XOR
    1010101 =

    1010010

  5. #5


    Did you find this post helpful? Yes | No

    Default ok ..Hi

    Hi Art

    Once again thanks for the explanation and reply :-)

    Mmm the eXclusive OR so the truth table then looks like this ...

    A B Result
    1 1 0
    1 0 1
    0 1 1
    0 0 0

    So ....
    The XOR operator returns a 1 when the value of either the first bit or the second bit is a 1.

    The XOR operator returns a 0 when neither or both of the bits is 1.

    And for a variable swap we could do something like this then ?
    X var byte
    y var byte

    x = x xor y
    y = x xor y
    x = x xor y

    which would be something to consider but NOT to do :-) (just wondering how PBP would react to that one ?)
    See here:
    http://betterexplained.com/articles/...les-using-xor/
    and here:
    http://www.topbits.com/xor-encryption.html

    Thanks so much one again
    Will definitely give it a bash and post some feedback of the results :-)

    Kind regards

    Dennis

  6. #6


    Did you find this post helpful? Yes | No

    Default ok ....

    Hi Art

    Once again thanks for the explanation and reply :-)

    Mmm the eXclusive OR so the truth table then looks like this ...

    A B Result
    1 1 0
    1 0 1
    0 1 1
    0 0 0

    So ....
    The XOR operator returns a 1 when the value of either the first bit or the second bit is a 1.

    The XOR operator returns a 0 when neither or both of the bits is 1.

    And for a variable swap we could do something like this then ?
    X var byte
    y var byte

    x = x xor y
    y = x xor y
    x = x xor y

    which would be something to consider but NOT to do :-) (just wondering how PBP would react to that one ?)
    See here:
    http://betterexplained.com/articles/...les-using-xor/
    and here:
    http://www.topbits.com/xor-encryption.html

    Thanks so much once again!

    Will definitely give it a bash and post some feedback of the results :-)

    Kind regards

    Dennis

Similar Threads

  1. multi functions button press
    By malwww in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 27th May 2009, 00:12
  2. Using Sleep
    By elec_mech in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 17th August 2008, 04:05
  3. Button press and press & hold how to ?
    By GrandPa in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 22nd August 2007, 03:37
  4. 3 HPWM channels
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 4th April 2006, 02:43
  5. Code check -- button not working
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 2nd March 2006, 22:43

Members who have read this thread : 1

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