In order to obtain a clearer picture of the storage area (RAM or Flash or EEPROM) of the variables in AVR programming, I have executed few codes on ArduinoUNO board. The results tell:
- (Header File avr/pgmspace.h, keywords PROGMEM and const are not included)
The variable is initialized in Flash.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
lcd.begin(16, 2); //2-line 16 chracters
lcd.clear();
lcd.print(0,1); // cursor position
unsigned int x =0x34;
unsigned int y1, y2;
unsigned int *z;
//--------------
y1 = &x;
lcd.print(y1); // LCD shows 2298; It is a Flash Space
lcd.print(' ');
lcd.print(x, 16); // LCD shos: 34
lcd.print(' ');
//--------------
x = 0x56;
z = &x;
y2 = *z;
lcd.print(y2, 16); // LCD shows: 56
//----------------
}
void loop()
{
}
- (Header File avr/pgmspace.h, keyword PROGMEM are not included. Keyword const is included)
The variable is initialized in Flash. It can be accessed, but it can't be modified. However, using
pointer the value of the vaiable can be modified!
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
lcd.begin(16, 2); //2-line 16 chracters
lcd.clear();
lcd.print(0,1); // cursor position
unsigned const int x =0x34;
unsigned int y1, y2;
unsigned int *z;
//--------------
y1 = &x;
lcd.print(y1); // LCD shows 2298; It is a Flash Space
lcd.print(' ');
lcd.print(x, 16); // LCD shos: 34
lcd.print(' ');
//--------------
//x = 0x89; not allowed
z = &x;
*z = 0x89;
y2 = *z;
lcd.print(y2, 16); // LCD shows: 89
//----------------
}
void loop()
{
}
- (Header File avr/pgmspace.h, keywords PROGMEM and const are included)
The variable is initialized in Flash. It can be accessed and modified using pointer.
#include <avr/pgmspace.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
lcd.begin(16, 2); //2-line 16 chracters
lcd.clear();
lcd.print(0,1); // cursor position
unsigned const int x PROGMEM =0x34;
unsigned int y1, y2;
unsigned int *z;
//--------------
y1 = &x;
lcd.print(y1); // LCD shows 2298; It is a Flash Space
lcd.print(' ');
lcd.print(x, 16); // LCD shos: 34
lcd.print(' ');
//--------------
// x = 0x89; // not allowed
z = &x;
*z = 0x89;
y2 = *z;
lcd.print(y2, 16); // LCD shows: 89
//----------------
}
void loop()
{
}
- Is there anyway to initialise variable in RAM in the Arduino IDE?