Go Down

Topic: Question using an array (Read 632 times) previous topic - next topic

Frisky

Hi
I have an array of pin numbers they are part of a library and I'm using them with interrupts
External Interrupts and with Timer 3 output compare. I have declared the array as volatile I was wondering if I could put this array in flash memory since it the values never change? How would I do that inside a library? I'm using the values stored for digitalWrite and digitalRead. It works as I have it just fine but I'm wasting 12 bytes on sram do it the way I have below I'm using the 2560 board.
Thanks
Don

Code: [Select]

// variable used by interrupts
volatile uint8_t eomFlag = 0;
volatile uint8_t rxPins[] = {5,6,7,9,10,11};         // would like to put in flash memory
volatile uint8_t cdPins[] = {21,20,19,18,02,03};  // would like to put in flash memory


Nick Gammon

There is no point in making variables volatile, if they don't change.
http://www.gammon.com.au/electronics

Erdin

If the values don't change, use them as 'const' and not 'volatile'.
The 'volatile' is for variables that can be changed in an interrupt, without knowing when that happens.

To place code in Flash, see this,  http://www.arduino.cc/en/Reference/PROGMEM
The problem is that you can't use them as variables, you have to use a function to read them. The pgm_read... functions.

Frisky

Thank for your reply Nick.
I changed those arrays too below and the library still operates perfectly.

Code: [Select]

#include <avr/pgmspace.h>

prog_uchar rxPins[] = {5,6,7,8,9,10,11};
prog_uchar cdPins[] = {21,20,19,18,02,03};


Nick Gammon

http://www.gammon.com.au/electronics

Erdin

You need also to add the PROGMEM keyword to put them into flash memory.
If they are in flash memory, you can't use them anymore. You will need those pgm_read.... functions.

Nick Gammon

That type has the keyword:

Code: [Select]

typedef unsigned char PROGMEM prog_uchar;
http://www.gammon.com.au/electronics

Frisky

I didn't realize there is special read functions to fetch values from flash memory.
I work on that thanks guys.

Go Up