neingeist
/
arduinisten
Archived
1
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

198 lines
9.1 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Arduino - PROGMEM </title>
<link rel='stylesheet' href='arduino.css' type='text/css' />
</head>
<body>
<div id="page">
<!--PageHeaderFmt-->
<div id="pageheader">
<div class="title"><a href="http://www.arduino.cc"/>Arduino</a></div>
<div class="search">
<!-- SiteSearch Google -->
<FORM method=GET action="http://www.google.com/search">
<input type=hidden name=ie value=UTF-8>
<input type=hidden name=oe value=UTF-8>
<INPUT TYPE=text name=q size=25 maxlength=255 value="">
<INPUT type=submit name=btnG VALUE="search">
<input type=hidden name=domains value="http://www.arduino.cc/">
<input type=hidden name=sitesearch value="http://www.arduino.cc/">
</FORM>
<!-- SiteSearch Google -->
</div>
</div>
<!--/PageHeaderFmt-->
<!--PageLeftFmt-->
<div id="pagenav" style="text-align: right">
<div style="float: left;">
<p><a class='wikilink' href='http://www.arduino.cc/en/Main/Buy'>Buy</a>
|
<a class='wikilink' href='http://www.arduino.cc/en/Main/Software'>Download</a>
|
<a class='wikilink' href='Guide_index.html'>Getting Started</a>
|
<a class='wikilink' href='http://www.arduino.cc/en/Tutorial/HomePage'>Learning</a>
|
<a class='wikilink' href='index.html'>Reference</a>
|
<a class='wikilink' href='http://www.arduino.cc/en/Main/Hardware'>Hardware</a>
|
<a class='wikilink' href='FAQ.html'>FAQ</a>
</p>
<p class='vspace'></p>
</div>
<a class="urllink" href="http://www.arduino.cc/blog/" rel="nofollow">Blog &raquo;</a> |
<a class="urllink" href="http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl" rel="nofollow">Forum &raquo;</a> |
<a class="urllink" href="http://www.arduino.cc/playground/" rel="nofollow">Playground &raquo;</a>
</div>
<!--/PageLeftFmt-->
<div id="pagetext">
<!--PageText-->
<div id='wikitext'>
<p><strong>Reference</strong> &nbsp; <a class='wikilink' href='index.html'>Language</a> (<a class='wikilink' href='Extended.html'>extended</a>) | <a class='wikilink' href='Libraries.html'>Libraries</a> | <a class='wikilink' href='Comparison.html'>Comparison</a> | <a class='wikilink' href='Board.html'>Board</a>
</p>
<p class='vspace'></p><h2>PROGMEM</h2>
<p>Store data in flash (program) memory instead of SRAM. There's a description of the various <a class='urllink' href='http://www.arduino.cc/playground/Learning/Memory' rel='nofollow'>types of memory</a> available on an Arduino board.
</p>
<p class='vspace'></p><p>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.
</p>
<p class='vspace'></p><p>PROGMEM is part of the <a class='urllink' href='http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html' rel='nofollow'>pgmspace.h</a> library. So you first need to include the library at the top your sketch, like this:
</p><pre>
#include &lt;avr/pgmspace.h&gt;
</pre><h4>Syntax</h4>
<pre>
dataType variableName[] PROGMEM = {dataInt0, dataInt1, dataInt3...};
</pre>
<p class='vspace'></p><ul><li>program memory dataType - any program memory variable type (see below)
</li><li>variableName - the name for your array of data
</li></ul><p class='vspace'></p><p>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.
</p>
<p class='vspace'></p><pre>
dataType variableName[] PROGMEM = {};
dataType PROGMEM variableName[] = {};
PROGMEM dataType variableName[] = {};
</pre><p>Common programming styles favor one of the first two however.
</p>
<p class='vspace'></p><p>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).
</p>
<p class='vspace'></p><p>Using PROGMEM is also a two-step procedure. After getting the data into Flash memory, it requires special methods (functions), also defined in the <a class='urllink' href='http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html' rel='nofollow'>pgmspace.h</a> library, to read the data from program memory back into SRAM, so we can do something useful with it.
</p>
<p class='vspace'></p><p>As mentioned above, it is important to use the datatypes outlined in <a class='urllink' href='http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html' rel='nofollow'>pgmspace.h</a>. Some cryptic bugs are generated by using ordinary datatypes for program memory calls. Below is a list of variable types to use. Floating point numbers in program memory do not appear to be supported.
</p>
<p class='vspace'></p><pre> prog_char - a signed char (1 byte) -127 to 128
prog_uchar - an unsigned char (1 byte) 0 to 255
prog_int16_t - a signed int (2 bytes) -32,767 to 32,768
prog_uint16_t - an unsigned int (2 bytes) 0 to 65,535
prog_int32_t - a signed long (4 bytes) -2,147,483,648 to * 2,147,483,647.
prog_uint32_t - an unsigned long (4 bytes) 0 to 4,294,967,295
</pre>
<p class='vspace'></p><h4>Example</h4>
<p>The following code fragments illustrate how to read and write unsigned chars (bytes) and ints (2 bytes) to PROGMEM.
</p><pre>
#include &lt;avr/pgmspace.h&gt;
// save some unsigned ints
PROGMEM prog_uint16_t charSet[] = { 65000, 32796, 16843, 10, 11234};
// save some chars
prog_uchar signMessage[] PROGMEM = {"I AM PREDATOR, UNSEEN COMBATANT. CREATED BY THE UNITED STATES DEPART"};
unsigned int displayInt;
int k; // counter variable
char myChar;
// read back a 2-byte int
displayInt = pgm_read_word_near(charSet + k)
// read back a char
myChar = pgm_read_byte_near(signMessage + k);
</pre>
<p class='vspace'></p><p><strong>Arrays of strings</strong>
</p>
<p class='vspace'></p><p>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.
</p>
<p class='vspace'></p><p>These tend to be large structures so putting them into program memory is often desirable. The code below illustrates the idea.
</p>
<p class='vspace'></p><pre>
/*
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 &lt;avr/pgmspace.h&gt;
prog_char string_0[] PROGMEM = "String 0"; // "String 0" etc are strings to store - change to suit.
prog_char string_1[] PROGMEM = "String 1";
prog_char string_2[] PROGMEM = "String 2";
prog_char string_3[] PROGMEM = "String 3";
prog_char string_4[] PROGMEM = "String 4";
prog_char string_5[] PROGMEM = "String 5";
// Then set up a table to refer to your strings.
PGM_P PROGMEM string_table[] = // change "string_table" name to suit
{
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);
}
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 &lt; 6; i++)
{
strcpy_P(buffer, (char*)pgm_read_word(&amp;(string_table[i]))); // Necessary casts and dereferencing, just copy.
Serial.println( buffer );
delay( 500 );
}
}
</pre>
<p class='vspace'></p><h4>See also</h4>
<ul><li><a class='urllink' href='http://www.arduino.cc/playground/Learning/Memory' rel='nofollow'>Types of memory available on an Arduino board</a>
</li><li><a class='wikilink' href='Array.html'>array</a>
</li><li><a class='wikilink' href='String.html'>string</a>
</li></ul><p class='vspace'></p><p><a class='wikilink' href='index.html'>Reference Home</a>
</p>
<p class='vspace'></p><p><em>Corrections, suggestions, and new documentation should be posted to the <a class='urllink' href='http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?board=swbugs' rel='nofollow'>Forum</a>.</em>
</p>
<p class='vspace'></p><p>The text of the Arduino reference is licensed under a
<a class='urllink' href='http://creativecommons.org/licenses/by-sa/3.0/' rel='nofollow'>Creative Commons Attribution-ShareAlike 3.0 License</a>. Code samples in the reference are released into the public domain.
</p>
</div>
</div>
<!--PageFooterFmt-->
<div id="pagefooter">
<a href='#'>Edit Page</a> | <a href='#'>Page History</a> | <a href='#' target='_blank'>Printable View</a> | <a href='http://www.arduino.cc/en/Site/AllRecentChanges'>All Recent Site Changes</a>
</div>
<!--/PageFooterFmt-->
</div>
</body>
</html>