Skip to content

Commit 206dc82

Browse files
author
Your Name
committed
Changed PROGMEM according to the new documentation online
1 parent 3464207 commit 206dc82

File tree

1 file changed

+47
-36
lines changed

1 file changed

+47
-36
lines changed

Language/Variables/Utilities/PROGMEM.adoc

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,22 @@ The `PROGMEM` keyword is a variable modifier, it should be used only with the da
2424

2525
[float]
2626
=== Syntax
27-
`dataType variableName[] PROGMEM = {dataInt0, dataInt1, dataInt3...};`
27+
const dataType variableName[] PROGMEM = {data0, data1, data3...};`
2828

29-
`program memory dataType` - any program memory variable type (see below)
29+
`dataType` - any variable type
3030
`variableName` - the name for your array of data
3131

32-
Note that because PROGMEM is a variable modifier, there is no hard and fast rule about where it should go, so the Arduino compiler accepts all of the definitions below, which are also synonymous. However experiments have indicated that, in various versions of Arduino (having to do with GCC version), PROGMEM may work in one location and not in another. The "string table" example below has been tested to work with Arduino 13. Earlier versions of the IDE may work better if PROGMEM is included after the variable name.
32+
Note that because PROGMEM is a variable modifier, there is no hard and fast rule about where it should go, so the Arduino compiler accepts all of the definitions below, which are also synonymous. However experiments have indicated that, in various versions of Arduino (having to do with GCC version), PROGMEM may work in one location and not in another. The "string table" example below has been tested to work with Arduino 13. Earlier versions of the IDE may work better if PROGMEM is included after the variable name.
33+
34+
`const dataType variableName[] PROGMEM = {}; // use this form` +
35+
`const PROGMEM dataType variableName[] = {}; // or this one`+
36+
`const dataType PROGMEM variableName[] = {}; // not this one
3337

34-
`dataType variableName[] PROGMEM = {}; // use this form` +
35-
`dataType PROGMEM variableName[] = {}; // not this one` +
36-
`PROGMEM dataType variableName[] = {}; // use this form`
3738

3839
While `PROGMEM` could be used on a single variable, it is really only worth the fuss if you have a larger block of data that needs to be stored, which is usually easiest in an array, (or another C data structure beyond our present discussion).
3940

4041
Using `PROGMEM` is also a two-step procedure. After getting the data into Flash memory, it requires special methods (functions), also defined in the http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html[pgmspace.h] library, to read the data from program memory back into SRAM, so we can do something useful with it.
4142

42-
As mentioned above, it is important to use the datatypes outlined in http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html[pgmspace.h]. Some cryptic bugs are generated by using ordinary datatypes for program memory calls. Below is a list of variable types to use. Floating point numbers in program memory do not appear to be supported.
43-
44-
`prog_char` - a signed char (1 byte) -127 to 128 +
45-
`prog_uchar` - an unsigned char (1 byte) 0 to 255 +
46-
`prog_int16_t` - a signed int (2 bytes) -32,767 to 32,768 +
47-
`prog_uint16_t` - an unsigned int (2 bytes) 0 to 65,535 +
48-
`prog_int32_t` - a signed long (4 bytes) -2,147,483,648 to * 2,147,483,647. +
49-
`prog_uint32_t` - an unsigned long (4 bytes) 0 to 4,294,967,295
5043
[%hardbreaks]
5144
--
5245
// OVERVIEW SECTION ENDS
@@ -69,20 +62,43 @@ The following code fragments illustrate how to read and write unsigned chars (by
6962
7063
7164
// save some unsigned ints
72-
PROGMEM prog_uint16_t charSet[] = { 65000, 32796, 16843, 10, 11234};
65+
const PROGMEM uint16_t charSet[] = { 65000, 32796, 16843, 10, 11234};
7366
7467
// save some chars
75-
prog_uchar signMessage[] PROGMEM = {"I AM PREDATOR, UNSEEN COMBATANT. CREATED BY THE UNITED STATES DEPART"};
68+
const char signMessage[] PROGMEM = {"I AM PREDATOR, UNSEEN COMBATANT. CREATED BY THE UNITED STATES DEPART"};
7669
7770
unsigned int displayInt;
7871
int k; // counter variable
7972
char myChar;
8073
81-
// read back a 2-byte int
82-
displayInt = pgm_read_word_near(charSet + k)
8374
84-
// read back a char
85-
myChar = pgm_read_byte_near(signMessage + k);
75+
void setup() {
76+
Serial.begin(9600);
77+
while (!Serial);
78+
79+
// put your setup code here, to run once:
80+
// read back a 2-byte int
81+
for (k = 0; k < 5; k++)
82+
{
83+
displayInt = pgm_read_word_near(charSet + k);
84+
Serial.println(displayInt);
85+
}
86+
Serial.println();
87+
88+
// read back a char
89+
for (k = 0; k < strlen(signMessage); k++)
90+
{
91+
myChar = pgm_read_byte_near(signMessage + k);
92+
Serial.print(myChar);
93+
}
94+
95+
Serial.println();
96+
}
97+
98+
void loop() {
99+
// put your main code here, to run repeatedly:
100+
101+
}
86102
87103
----
88104
*Arrays of strings*
@@ -105,34 +121,28 @@ These tend to be large structures so putting them into program memory is often d
105121
here is a good template to follow.
106122
107123
Setting up the strings is a two-step process. First define the strings.
108-
109124
*/
110125
111126
#include <avr/pgmspace.h>
112-
prog_char string_0[] PROGMEM = "String 0"; // "String 0" etc are strings to store - change to suit.
113-
prog_char string_1[] PROGMEM = "String 1";
114-
prog_char string_2[] PROGMEM = "String 2";
115-
prog_char string_3[] PROGMEM = "String 3";
116-
prog_char string_4[] PROGMEM = "String 4";
117-
prog_char string_5[] PROGMEM = "String 5";
127+
const char string_0[] PROGMEM = "String 0"; // "String 0" etc are strings to store - change to suit.
128+
const char string_1[] PROGMEM = "String 1";
129+
const char string_2[] PROGMEM = "String 2";
130+
const char string_3[] PROGMEM = "String 3";
131+
const char string_4[] PROGMEM = "String 4";
132+
const char string_5[] PROGMEM = "String 5";
118133
119134
120135
// Then set up a table to refer to your strings.
121136
122-
PROGMEM const char *string_table[] = // change "string_table" name to suit
123-
{
124-
string_0,
125-
string_1,
126-
string_2,
127-
string_3,
128-
string_4,
129-
string_5 };
137+
const char* const string_table[] PROGMEM = {string_0, string_1, string_2, string_3, string_4, string_5};
130138
131139
char buffer[30]; // make sure this is large enough for the largest string it must hold
132140
133141
void setup()
134142
{
135143
Serial.begin(9600);
144+
while(!Serial);
145+
Serial.println("OK");
136146
}
137147
138148
@@ -147,10 +157,11 @@ void loop()
147157
for (int i = 0; i < 6; i++)
148158
{
149159
strcpy_P(buffer, (char*)pgm_read_word(&(string_table[i]))); // Necessary casts and dereferencing, just copy.
150-
Serial.println( buffer );
160+
Serial.println(buffer);
151161
delay( 500 );
152162
}
153163
}
164+
154165
----
155166
[%hardbreaks]
156167

0 commit comments

Comments
 (0)