Go Down

Topic: pgm_read_byte doesn't work (Read 3955 times) previous topic - next topic

darknails

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

Here is the code:
Code: [Select]

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



Nick Gammon

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: [Select]

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: [Select]

0    22
0    33
184    44
0    55
0    66
0    77
1    88
0    99

Please post technical questions on the forum, not by personal message. Thanks!

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

darknails

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy