728x90

Serial.print(F(“Hello World”));

Oct 06, 2016, 03:38 pm

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?

 

 

Re: Serial.print(F(“Hello World”));

#1

Oct 06, 2016, 03:39 pm

It means the string isn't using up RAM.

 

 

 

Re: Serial.print(F(“Hello World”));

#2

Oct 06, 2016, 03:59 pm

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.

 

 

Re: Serial.print(F(“Hello World”));

#4

Oct 06, 2016, 08:08 pm

Quote from: CaverAdam on Oct 06, 2016, 03:59 pm

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.

Hackaday: https://hackaday.io/MarkRD
Advanced C++ Techniques: https://forum.arduino.cc/index.php?topic=493075.0

'홍익인간 프로젝트 > Arduino C' 카테고리의 다른 글

pulseIn() Function  (0) 2021.01.22
pinMode() Function  (0) 2021.01.22
u8g2setupcpp  (0) 2020.12.25
PCF8574 I2C 확장보드로 CLCD, Text(텍스트) LCD 구동 (아두이노)  (0) 2020.12.25
LiquidCrystal I2C - 3 Arguments Type  (0) 2020.12.25

+ Recent posts