Go Down

Topic: Serial.print(F(“Hello World”)); (Read 2595 times) previous topic - next topic

PickyBiker

I found this comment on Serial.print:

You can pass flash-memory based strings to Serial.print() by wrapping them with F(). For example :

Serial.print(F("Hello World"))

What does it mean to pass flash-memory based strings?

Delta_G

It means the string isn't using up RAM.
If at first you don't succeed, up - home - sudo - enter.

CaverAdam

When you compile your program it says how much program memory (stored in flash) you are using and how much dynamic ram you are using.

If you use F() you can move constant strings to the program memory instead of the ram. This will take up space that will decrease the amount of other code you can write. But it will free up dynamic ram.

My chip has 32KB of Flash memory (2Kb taken up by bootloader) and 2KB or RAM. Sometimes I need to store lots of data in RAM so I move the constant strings to Flash memory. Sometimes my program has a lot of steps and I have a shortage of Flash memory so I leave the strings in RAM.

mrburnette

#3
Oct 06, 2016, 07:52 pm Last Edit: Oct 06, 2016, 07:55 pm by mrburnette
Quote
If you use F() you can move constant strings to the program memory instead of the ram.
Actually what happens in a Harvard architecture uC is that the compiled string stays in flash and does not get copied to SRAM during the C++ initialization that happens before your sketch receives run control.

Since the string is not moved to SRAM, it has the PROGMEM property and runs from flash.

Ray


5 minute read about flash and SRAM here

Jiggy-Ninja

Sometimes my program has a lot of steps and I have a shortage of Flash memory so I leave the strings in RAM.
That's not how it works.

Your string constants are ALWAYS in flash memory. They have to be, because when you take power away from RAM everything's gone. When you put power back on again, they have to be reloaded from somewhere, and that somewhere is the flash memory. Leaving off PROGMEM will do nothing to save you significant amounts of flash memory.

It's just that a normal part of initializing a C++ program is to load those arrays into SRAM before setup(). PROGMEM is an AVR-specific attribute that can be applied to variables that tells the compiler to not do that. Because flash is in a different memory space than RAM, special functions are needed to access from flash than from RAM.

CaverAdam


PickyBiker


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