PROGMEM
설명
데이터를 SRAM 대신 플래시(프로그램) 메모리에 저장한다.
There's a description of the various types of memory available on an Arduino board.
The PROGMEM keyword is a variable modifier, it should be used only with the data types defined in pgmspace.h. It tells the compiler "put this information into flash memory", instead of into SRAM, where it would normally go.
PROGMEM is part of the pgmspace.h library. It is included automatically in modern versions of the IDE, however if you are using an IDE version below 1.0 (2011), you'll first need to include the library at the top your sketch, like this:
#include <avr/pgmspace.h>
문법
const dataType variableName[] PROGMEM = {data0, data1, data3...};
※ 주의 및 경고:
Note that because PROGMEM is a variable modifier, there is no hard and fast rule about where it should go, so the Arduino compiler accepts all of the definitions below, which are also synonymous. However experiments have indicated that, in various versions of Arduino (having to do with GCC version), PROGMEM may work in one location and not in another. The "string table" example below has been tested to work with Arduino 13. Earlier versions of the IDE may work better if PROGMEM is included after the variable name.
While PROGMEM could be used on a single variable, it is really only worth the fuss if you have a larger block of data that needs to be stored, which is usually easiest in an array, (or another C++ data structure beyond our present discussion).
Using PROGMEM is also a two-step procedure. After getting the data into Flash memory, it requires special methods (functions), also defined in the pgmspace.h library, to read the data from program memory back into SRAM, so we can do something useful with it.
매개변수
- dataType - any variable type
- variableName - the name for your array of data
예제 코드
The following code fragments illustrate how to read and write unsigned chars (bytes) and ints (2 bytes) to PROGMEM.
Arrays of strings
It is often convenient when working with large amounts of text, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually an example of a two-dimensional array.
These tend to be large structures so putting them into program memory is often desirable. The code below illustrates the idea.
※ 주의 및 경고:
PROGMEM을 쓰기위해서는 변수는 광역으로 정의되거나, 또는 static 키워드로 정의되어야 함을 주의하세요.
- 아래 코드는 함수 안에 있으면 동작하지 않을 거다:
- The following code WILL work, even if locally defined within a function:
The F() macro
When an instruction like :
is used, the string to be printed is normally saved in RAM. If your sketch prints a lot of stuff on the Serial Monitor, you can easily fill the RAM. If you have free FLASH memory space, you can easily indicate that the string must be saved in FLASH using the syntax:
더보기
※ ARDUINO BUY RECOMMENDATION
Arduino UNO R3 | |
Arduino Starter Kit |
Additionally, some links direct to products from our own brand, DIYables.