Pages: [1]   Go Down
Author Topic: Using PROGMEM data  (Read 943 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 73
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was reading the reference about PROGMEM data (http://www.arduino.cc/en/Reference/PROGMEM)
and I've found in internet examples using thing like this:

Serial.println(PSTR("this is a test"));

But I've also found this case:

Serial.println(F("this is a test"));

I'm tried both in one program where has ram problems, and found that the second case works better. My question is what is the difference? In which case should I use each one?

I've also seen some places where they uses P() instead of F()

I've found that there is a very usefull sprintf_P() function that can use PSTR() as parameter but if I use F() instead, the compiler reports an error.

« Last Edit: February 15, 2013, 08:33:57 am by anv » Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have a mixture of Arduino and avr gcc code.
The Arduino uses the arv gcc compiler, and every low level function can also be used with the Arduino.

For the Arduino, the 'F()' macro is created:
Code:
Serial.println(F("Hello string in flash memory"));
But the 'F()' macro is something typical for the Arduino IDE only.

The sprintf_P() functions are avr gcc functions, they work with PSTR.
Code:
sprintf_P( buffer, PSTR("Hello number twelve: %d"), 12);

Perhaps this will be fixed some day. For now it is a little inconsistant.
Logged

0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 73
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perhaps this will be fixed some day. For now it is a little inconsistant.

Ok, I understand.

But I've found stranges different results when I use F() and PSTR() in Serial.println. In a sketch that uses ethercard for tcp/ip (high memory usage), I had problems of garbage sent to Serial instead of some constant strings. I thought that PSTR was stored on flash, then it can't be modified after uploading the program. Then, I suppose PSTR is copying the string to RAM or something like this. This sould not be problem but I received the garbage... Then I replaced PSTR with F and the problem was solved... but F can only be used in Serial.print and a few places, PSTR works with sprintf, strcmp, etc.
« Last Edit: February 15, 2013, 10:14:37 am by anv » Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PSTR is the same as declaring a string in flash with PROGMEM.
So you can use sprintf_P() and strcpy_P() with PSTR and also with PROGMEM strings.

The Serial.println() uses the 'stream' class. The 'F()' macro is for that class.
Logged

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

But I've found stranges different results when I use F() and PSTR() in Serial.println. In a sketch that uses ethercard for tcp/ip (high memory usage), I had problems of garbage sent to Serial instead of some constant strings.

You'd need to post your problem sketch.
Logged


Pages: [1]   Go Up
Jump to: