Pages: [1]   Go Down
Author Topic: pgm_read_byte doesn't work  (Read 2367 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I want to use the flash memory of my Arduino Nano 168, but somehow it gives strange results.

Here is the code:
Code:
const prog_uint8_t BitMap[8] = {   // store in program memory to save RAM
        B10000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
 };
 
 void setup()
{
  Serial.begin(9600);

}


void loop()
{

  Serial.print(BitMap[0]);
  Serial.print("    ");
  Serial.print(pgm_read_byte (&BitMap[0]));
  Serial.print("    ");
 
}

The values BitMapp[0] and pgm_read_byte (&BitMap[0]) are different.
BitMapp[0] is 128 (correct), but pgm_read_byte (&BitMap[0]) gives 144 and sometimes different depending on where i put the print command.

And why would one need pgm_read_byte at all. Why not just use BitMapp[0]?

thanks..


Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The way your array is defined appears to give strange results. This modified version works, and note that only the progmem access works, not the direct access.

Code:
byte PROGMEM BitMap  [8] = {  22, 33, 44, 55, 66, 77, 88, 99 };
 
void setup()
  {
  Serial.begin(115200);
  }

void loop()
  {
  for (int i = 0; i < 8; i++)
    {
    Serial.print(BitMap[i]);
    Serial.print("    ");
    Serial.print(pgm_read_byte (&BitMap[i]));
    Serial.println ();
    }
  while (true);  // loop forever
  }


Output:

Code:
0    22
0    33
184    44
0    55
0    66
0    77
1    88
0    99
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, Nick. If I change const to PROGMEM, then it works.
Logged

Pages: [1]   Go Up
Jump to: