Using Arrays

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

Moderators: Benj, Mods

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

Using Arrays

Post by Ron »

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: 63 times
Been thanked: 290 times
Contact:

Re: Using Arrays

Post by JonnyW »

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

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

Re: Using Arrays

Post by Ron »

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: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using Arrays

Post by Benj »

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: 67
Joined: Tue Sep 16, 2008 2:09 pm
Location: Minnesota USA
Has thanked: 4 times
Been thanked: 17 times
Contact:

Re: Using Arrays

Post by alanwms »

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

Leomar
Posts: 3
Joined: Fri Feb 28, 2020 9:01 pm
Has thanked: 1 time
Contact:

Re: Using Arrays

Post by Leomar »

Hello Friend

How do I test inside the matrix, which is the biggest number ??

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

Re: Using Arrays

Post by Benj »


medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Using Arrays

Post by medelec35 »

I have converted the bubble sort Ben linked to, from pre Flowcode V6 to Flowcode V7/V8
Attachments
Bubble sort.fcfx
(8.99 KiB) Downloaded 291 times
Martin

Post Reply