Using Arrays

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Ron
Posts: 225
Joined: Wed Apr 11, 2007 6:15 pm
Has thanked: 2 times
Contact:

Using Arrays

Postby Ron » Sat Feb 18, 2012 4:25 am

Hi,

I want to set up an array and when receiving a packet via the rs232 component macro put each received CHAR into a position in the array.

In the Variable Manager window I have created a byte array called...

RS485_TX_ARRAY[256] <--- is this the correct way to create the array?

Once I receive a BYTE how to I place it in the array based on the number of times I loop?

Example:

I receive the first BYTE, I want to put it in RS485_TX_ARRAY[0], second received byte into RS485_TX_ARRAY[1] and so on. I imagine I use the value in the variable that contains the number of times I have looped to receive the bytes.... first time thru loop would populate RS485_TX_ARRAY[0], second time thru loop would populate RS485_TX_ARRAY[1], and so on.

Once I receive all the bytes I will separate then into individual bytes, calculate the checksum to ensure the packet is valid, then process the data.

How/what is the best way to set all elements of the array to 0?

Thank you,

Ron

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 64 times
Been thanked: 306 times
Contact:

Re: Using Arrays

Postby JonnyW » Sat Feb 18, 2012 8:05 pm

Hello. Yes, setting up the array in this way is fine. However remember that the PIC chips have memory limitations so I believe that only 128 element arrays can be created at a time. This is a limitation of the hardware (I may be wrong on this though, I'm not an expert).

You are spot on as to how to write to the array. In a calculation box do:

Code: Select all

RS485_TX_ARRAY[index] = received_byte
index = index + 1

You can loop until 'index' is your maximum.

To reset all the elements to zero, you can either use a loop:

Code: Select all

calculation: index = 0
loop: (index < 256)
  calculation: RS485_TX_ARRAY[index] = 0
               index = index + 1

Or you could use a C icon calling memset(); that does the same thing. This will not be simulated though.

Remember to make sure 'index' is an int (not a byte) if you have 256 elements or more, or it will wrap around after 255 to zero!

The fastest way to set all elements to zero in your case though is just not actually set anything to zero at all, and instead pretend that anything over your 'index' is zero when calculating your checksum and processing the data. Write your code around the data received, not the size of the buffer.

Jonny
These users thanked the author JonnyW for the post:
Ron (Sun Feb 19, 2012 1:21 pm)
Rating: 5.26%
 

Ron
Posts: 225
Joined: Wed Apr 11, 2007 6:15 pm
Has thanked: 2 times
Contact:

Re: Using Arrays

Postby Ron » Sun Feb 19, 2012 1:20 pm

Hi,

Benj responded with a previous question I had about arrays regarding size.

Hello Ron,

AVR's, PIC24's, dsPIC's and ARM devices can have arrays up to any size providing they have enough ram to complete the array. PIC16 and PIC18 devices can have a maximum array size of 256 bytes due to their archetecture. You can create multiple arrays in a PIC and then use a overall switching technique to switch between arrays. However you would need a lot of arrays to cover the 3096, 13 to be exact.


Where is this found in the datasheet?

Thanks,

Ron

User avatar
Benj
Matrix Staff
Posts: 14219
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4360 times
Been thanked: 4096 times
Contact:

Re: Using Arrays

Postby Benj » Mon Feb 20, 2012 11:26 am

Hi Ron,

I don't think it would be mentioned in the datasheet unless its under the memory section. Basically a 8-bit PIC's memory is split up into banks of 256 bytes to allow their addressing architecture to work. Some C compilers do allow for arrays bigger then 256 bytes but will bank switch to the appropriate bank for the byte you are trying to read or write. This can cause hidden complication to the underlying code so it looks like BoostC and Microchip have taken the approach that if you need a bigger array you have to switch between arrays yourself.

Eg while array index > 256 then move to next array and subtract 256 from the index.

alanwms
Posts: 48
Joined: Tue Sep 16, 2008 2:09 pm
Location: Minnesota USA
Has thanked: 3 times
Been thanked: 17 times
Contact:

Re: Using Arrays

Postby alanwms » Thu Nov 19, 2015 4:49 pm

Or in the case of more than 2 arrays - One would need to state If pointer is >255 AND < 512 then use array 2