PDA

View Full Version : 4 Bytes one button press



Dennis
- 13th January 2010, 18:09
..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)



TX side
-------
hserout [TRAIN,TRAIN,TRAIN,TRAIN,TRAIN,TRAIN,synch,hcode,dc ode,pcode,hcode,dcode,pcode,data,data,data,data,da ta,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

Art
- 15th January 2010, 00:13
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.

Dennis
- 15th January 2010, 10:35
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

Art
- 15th January 2010, 11:41
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

Dennis
- 15th January 2010, 22:34
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/swap-two-variables-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

Dennis
- 15th January 2010, 22:36
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/swap-two-variables-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