PDA

View Full Version : 128 bit variable???



Hylan
- 28th October 2011, 02:02
I'm playing with an encryption module where I want to combine 3 bytes and it will pass 128bits encrypted back. I will then pass this on and reverse the process later. I'm using an 18F6520 chip with PBP. So I guess two questions:
1. Can I combine the 3 bytes into a single variable to pass in?
2. How do I handle the 128 bits coming out?

I was reading the ME Labs Compiler manual and it looks like the "Long" only goes to 32 bits.

any code samples would be helpful as well.

Thank you,
Hylan

HenrikOlsson
- 28th October 2011, 06:39
Hi,
Correct, a LONG is 4bytes (32 bits), perhaps you can use that for your 24bit variable. The bits is probably best handled as either an array, either of bits or bytes depending on how you need to read/write the 128bits (byte by byte or bit by bit etc). If a LONG doesn't suit your needs then you probably need to resort to an array in the case too.


myByteArray VAR BYTE[3] 'Create an array of 3 bytes (24bits)
myBitArray VAR BIT[128] 'Create an array of 128bits
Counter VAR BYTE

' Set all three bytes in the byteArray to $55
For Counter = 0 to 3
myByteArray[Counter] = $55
NEXT

' Set all bits in the bitArray to '1'
FOR Counter = 0 to 127
myBitArray[Counter] = 1
NEXT

'Copy the first 16 bits in the bitArray to the byteArray, starting at the second byte.
FOR Counter = 0 to 15
myByteArray.0[Counter+7] = myBitArray[Counter]
NEXT

/Henrik.

Hylan
- 28th October 2011, 15:02
Henrik,

I really appreciate it. I haven't used arrays before, so this will be good learning for me.
So what is the $55 ?
I sort of understand how the loop is adding to the array in each pass. But assuming I use the 'myByteArray' for my 3 bytes I need to combine (Var1, Var2, Var3) that are already saved after reading the status of the 3 ports. How does the loop know to look at these 3 variables?
Second, question: If the 128 bits is coming in on PortB.1 how does the bottom loop know to read that?
Thank you very much.
Hylan

HenrikOlsson
- 28th October 2011, 16:40
Hi,
The $55 is nothing in particular, just a number like any other number fitting into a byte, could have been 85 or 010101 (they are all the same thing by the way). It was just a number I took it doesn't mean or do anything in paricular in the example.

There are many ways to put Var1-Var3 in the array. The most obvious would be to simply copy them:

myByteArray[0] = Var1
myByteArray[1] = Var2
myByteArray[2] = Var3

Another, perhaps more elgant way is to create aliases pointing into the array

myByteArray VAR BYTE[3]
Var1 VAR myByteArray[0]
Var2 VAR myByteArray[1]
Var3 VAR myByteArray[2]

This way you can use FOR-NEXT loops etc to index the array AND access each entry just like any other byte. Remember that in this case there are only three bytes in RAM, Var1 is the same as myArray[0] - just another 'name' for it - for convinience. Like


myByteArray[0] = 128
HSEROUT[#Var1, 13]

The first line puts the value 128 in the first location of the array. The second line prints the content of Var1 which is the same thing as the first location of the array.

If your databits are comming in on PortB.1 and, I assume you have some sort of clock or something on PortB.0, then something like:

Counter = 0
While Counter < 128
While PortB.0 = 0 : WEND 'Wait here for rising edge of clock.
myBitArray[Counter] = PortB.1 'Get state of pin and store in array
While PortB.0 = 1 : WEND ' Wait for clock to return low
Counter = Counter + 1 ' Prepare for next bit
WEND
Something like that...

/Henrik.

Hylan
- 28th October 2011, 17:39
Henrick,

This is very helpfull. I just got Hyperterminal running so I can see the output. I will begin trying this out.

Thank you! Thank you!
Hylan