查看PROGMEM的源代码
←
PROGMEM
跳转至:
导航
、
搜索
因为以下原因,你没有权限编辑本页:
你刚才请求的操作只对属于该用户组的用户开放:
用户
您可以查看并复制此页面的源代码:
Store data in flash (program) memory instead of SRAM. There's a description of the various [http://playground.arduino.cc/Learning/Memory types of memory] available on an Arduino board. The PROGMEM keyword is a variable modifier, it should be used only with the datatypes 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 [http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html pgmspace.h] library that is available in the AVR architecture only. So you first need to include the library at the top your sketch, like this: <pre style="color:dimgray"> #include <avr/pgmspace.h> </pre> <font color="orange" size="+1">'''Syntax'''</font> <pre style="color:dimgray"> const dataType variableName[] PROGMEM = {data0, data1, data3...}; </pre> :- dataType - any variable type :- variableName - the name for your array of data 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. <pre style="color:dimgray"> const dataType variableName[] PROGMEM = {}; // use this form const PROGMEM dataType variableName[] = {}; // or this form const dataType PROGMEM variableName[] = {}; // not this one </pre> 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 [http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html pgmspace.h] library, to read the data from program memory back into SRAM, so we can do something useful with it. <font color="orange" size="+1">'''Example'''</font> The following code fragments illustrate how to read and write chars (bytes) and ints (2 bytes) to PROGMEM. <pre style="color:dimgray"> #include <avr/pgmspace.h> // save some unsigned ints const PROGMEM uint16_t charSet[] = { 65000, 32796, 16843, 10, 11234}; // save some chars const char signMessage[] PROGMEM = {"I AM PREDATOR, UNSEEN COMBATANT. CREATED BY THE UNITED STATES DEPART"}; unsigned int displayInt; int k; // counter variable char myChar; void setup() { Serial.begin(9600); while (!Serial); // put your setup code here, to run once: // read back a 2-byte int for (k = 0; k < 5; k++) { displayInt = pgm_read_word_near(charSet + k); Serial.println(displayInt); } Serial.println(); // read back a char int len = strlen_P(signMessage); for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(signMessage + k); Serial.print(myChar); } Serial.println(); } void loop() { // put your main code here, to run repeatedly: } </pre> <font color="" size="">'''Arrays of strings'''</font> 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. <pre style="color:dimgray"> /* PROGMEM string demo How to store a table of strings in program memory (flash), and retrieve them. Information summarized from: http://www.nongnu.org/avr-libc/user-manual/pgmspace.html Setting up a table (array) of strings in program memory is slightly complicated, but here is a good template to follow. Setting up the strings is a two-step process. First define the strings. */ #include <avr/pgmspace.h> const char string_0[] PROGMEM = "String 0"; // "String 0" etc are strings to store - change to suit. const char string_1[] PROGMEM = "String 1"; const char string_2[] PROGMEM = "String 2"; const char string_3[] PROGMEM = "String 3"; const char string_4[] PROGMEM = "String 4"; const char string_5[] PROGMEM = "String 5"; // Then set up a table to refer to your strings. const char* const string_table[] PROGMEM = {string_0, string_1, string_2, string_3, string_4, string_5}; char buffer[30]; // make sure this is large enough for the largest string it must hold void setup() { Serial.begin(9600); while(!Serial); Serial.println("OK"); } void loop() { /* Using the string table in program memory requires the use of special functions to retrieve the data. The strcpy_P function copies a string from program space to a string in RAM ("buffer"). Make sure your receiving string in RAM is large enough to hold whatever you are retrieving from program space. */ for (int i = 0; i < 6; i++) { strcpy_P(buffer, (char*)pgm_read_word(&(string_table[i]))); // Necessary casts and dereferencing, just copy. Serial.println(buffer); delay( 500 ); } } </pre> <font color="darkorenge">'''Note'''</font><br/> Please note that variables must be either globally defined, OR defined with the static keyword, in order to work with PROGMEM. The following code will '''NOT''' work when inside a function: <pre style="color:dimgray"> const char long_str[] PROGMEM = "Hi, I would like to tell you a bit about myself.\n"; </pre> The following code '''WILL''' work, even if locally defined within a function: <pre style="color:dimgray"> const static char long_str[] PROGMEM = "Hi, I would like to tell you a bit about myself.\n" </pre> <font color="orange" >The F() macro</font> When an instruction like : <pre style="color:dimgray"> Serial.print("Write something on the Serial Monitor"); </pre> 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: <pre style="color:dimgray"> Serial.print(F("Write something on the Serial Monitor that is stored in FLASH")); </pre> <font color="orange" >See also</font> *[[string - char array|string]] *[[array]] [http://playground.arduino.cc/Learning/Memory Types of memory available on an Arduino board] ---- [[Arduino语法参考 | 返回Arduino语法参考列表]] 更多建议和问题欢迎反馈至 [http://www.yfrobot.com YFRobot论坛]
返回
PROGMEM
。
导航菜单
个人工具
登录
名字空间
页面
讨论
不转换
变种
不转换
简体
繁體
大陆简体
香港繁體
澳門繁體
大马简体
新加坡简体
台灣正體
查看
阅读
查看源代码
查看历史
操作
搜索
导航
首页
YF-论坛提问
YFRobot-直营店
YFRobot-企业店
Arduino
Arduino之入门篇
Arduino入门教程
Arduino语法参考
Arduino库
Arduino核心代码
编程平台
Mixly库
Mind+库
MakeCode扩展
传感器系列
积木式传感器系列
黑板传感器系列
蓝板传感器系列
Micro:Bit
Micro:Bit 通用基础教程
Valon智能车
Valon-I
帮助
帮助
wiki语法参考
工具箱
链入页面
相关更改
特殊页面
页面信息