Pages: [1]   Go Down
Author Topic: Question using an array  (Read 541 times)
0 Members and 1 Guest are viewing this topic.
Chicago
Offline Offline
Full Member
***
Karma: 0
Posts: 113
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

There is no point in making variables volatile, if they don't change.
Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Chicago
Offline Offline
Full Member
***
Karma: 0
Posts: 113
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#include <avr/pgmspace.h>

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

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh? Strange. smiley
Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That type has the keyword:

Code:
typedef unsigned char PROGMEM prog_uchar;
Logged

Chicago
Offline Offline
Full Member
***
Karma: 0
Posts: 113
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: