<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ja">
	<id>http://mochiuwiki.e2.valueserver.jp/index.php?action=history&amp;feed=atom&amp;title=Arduino%E3%83%95%E3%82%A1%E3%83%BC%E3%83%A0%E3%82%A6%E3%82%A7%E3%82%A2%E3%81%AE%E5%9F%BA%E7%A4%8E_-_PROGMEM</id>
	<title>Arduinoファームウェアの基礎 - PROGMEM - 版の履歴</title>
	<link rel="self" type="application/atom+xml" href="http://mochiuwiki.e2.valueserver.jp/index.php?action=history&amp;feed=atom&amp;title=Arduino%E3%83%95%E3%82%A1%E3%83%BC%E3%83%A0%E3%82%A6%E3%82%A7%E3%82%A2%E3%81%AE%E5%9F%BA%E7%A4%8E_-_PROGMEM"/>
	<link rel="alternate" type="text/html" href="http://mochiuwiki.e2.valueserver.jp/index.php?title=Arduino%E3%83%95%E3%82%A1%E3%83%BC%E3%83%A0%E3%82%A6%E3%82%A7%E3%82%A2%E3%81%AE%E5%9F%BA%E7%A4%8E_-_PROGMEM&amp;action=history"/>
	<updated>2026-09-06T13:32:15Z</updated>
	<subtitle>このウィキのこのページに関する変更履歴</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>http://mochiuwiki.e2.valueserver.jp/index.php?title=Arduino%E3%83%95%E3%82%A1%E3%83%BC%E3%83%A0%E3%82%A6%E3%82%A7%E3%82%A2%E3%81%AE%E5%9F%BA%E7%A4%8E_-_PROGMEM&amp;diff=4055&amp;oldid=prev</id>
		<title>Wiki: ページの作成:「== 概要 == &lt;code&gt;PROGMEM&lt;/code&gt;キーワードは変数修飾子であり、SRAMの代わりにフラッシュメモリにデータを格納する。&lt;br&gt; &lt;br&gt; &lt;code&gt;PRO…」</title>
		<link rel="alternate" type="text/html" href="http://mochiuwiki.e2.valueserver.jp/index.php?title=Arduino%E3%83%95%E3%82%A1%E3%83%BC%E3%83%A0%E3%82%A6%E3%82%A7%E3%82%A2%E3%81%AE%E5%9F%BA%E7%A4%8E_-_PROGMEM&amp;diff=4055&amp;oldid=prev"/>
		<updated>2021-08-14T12:01:59Z</updated>

		<summary type="html">&lt;p&gt;ページの作成:「== 概要 == &amp;lt;code&amp;gt;PROGMEM&amp;lt;/code&amp;gt;キーワードは変数修飾子であり、SRAMの代わりにフラッシュメモリにデータを格納する。&amp;lt;br&amp;gt; &amp;lt;br&amp;gt; &amp;lt;code&amp;gt;PRO…」&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新規ページ&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== 概要 ==&lt;br /&gt;
&amp;lt;code&amp;gt;PROGMEM&amp;lt;/code&amp;gt;キーワードは変数修飾子であり、SRAMの代わりにフラッシュメモリにデータを格納する。&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;PROGMEM&amp;lt;/code&amp;gt;キーワードは、pgmspace.hファイルで定義されたデータ型でのみ使用される。&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;u&amp;gt;Arduino IDEが古い場合は、以下のようにライブラリをインクルードする必要がある。&amp;lt;/u&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 #include &amp;lt;avr/pgmspace.h&amp;gt;&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 使用方法 ==&lt;br /&gt;
&amp;lt;code&amp;gt;PROGMEM&amp;lt;/code&amp;gt;キーワードの使用方法は、2ステップに分かれている。&amp;lt;br&amp;gt;&lt;br /&gt;
# まず、データをフラッシュメモリに格納する。&lt;br /&gt;
# 次に、プログラムメモリからSRAMにデータを読み戻す(使用する)には、avr/pgmspace.hファイルで定義されているPROGMEM専用の関数を使用する。&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
以下に、&amp;lt;code&amp;gt;PROGMEM&amp;lt;/code&amp;gt;キーワードのシンタックスを示す。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 const &amp;lt;データ型&amp;gt; &amp;lt;変数名または配列変数名&amp;gt; PROGMEM = {data0, data1, data3 ...};&lt;br /&gt;
 // または&lt;br /&gt;
 const PROGMEM &amp;lt;データ型&amp;gt; &amp;lt;変数名または配列変数名&amp;gt; = {data0, data1, data3 ...};&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
以下の例では、&amp;lt;code&amp;gt;PROGMEM&amp;lt;/code&amp;gt;キーワードを使用したchar型とuint16_t型の配列を読み書きしている。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 // save some unsigned int&lt;br /&gt;
 const PROGMEM uint16_t charSet[] = {65000, 32796, 16843, 10, 11234};&lt;br /&gt;
 &lt;br /&gt;
 // save some char&lt;br /&gt;
 const char signMessage[] PROGMEM = {&amp;quot;I AM PREDATOR,  UNSEEN COMBATANT. CREATED BY THE UNITED STATES DEPART&amp;quot;};&lt;br /&gt;
 &lt;br /&gt;
 unsigned int displayInt;&lt;br /&gt;
 char myChar;&lt;br /&gt;
 &lt;br /&gt;
 void setup()&lt;br /&gt;
 {&lt;br /&gt;
    Serial.begin(9600);&lt;br /&gt;
    while (!Serial);  // wait for serial port to connect. Needed for native USB&lt;br /&gt;
 &lt;br /&gt;
    // put your setup code here, to run once:&lt;br /&gt;
    // read back a 2-byte int&lt;br /&gt;
    for (byte k = 0; k &amp;lt; 5; k++)&lt;br /&gt;
    {&lt;br /&gt;
       displayInt = pgm_read_word_near(charSet + k);&lt;br /&gt;
       Serial.println(displayInt);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    Serial.println();&lt;br /&gt;
 &lt;br /&gt;
    // read back a char&lt;br /&gt;
    for (byte k = 0; k &amp;lt; strlen_P(signMessage); k++)&lt;br /&gt;
    {&lt;br /&gt;
       myChar = pgm_read_byte_near(signMessage + k);&lt;br /&gt;
       Serial.print(myChar);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    Serial.println();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void loop()&lt;br /&gt;
 {&lt;br /&gt;
    // put your main code here, to run repeatedly:&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
LCDを使用して大量のテキストを扱う場合、文字列配列を設定すると便利なことが多い。&amp;lt;br&amp;gt;&lt;br /&gt;
このような配列はサイズが大きくなりがちなため、フラッシュメモリ上に格納することが望ましい場合がある。&amp;lt;br&amp;gt;&lt;br /&gt;
以下は、その例である。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 /*&lt;br /&gt;
  PROGMEM string demo&lt;br /&gt;
  How to store a table of strings in program memory (flash),&lt;br /&gt;
  and retrieve them.&lt;br /&gt;
&lt;br /&gt;
  Information summarized from:&lt;br /&gt;
  http://www.nongnu.org/avr-libc/user-manual/pgmspace.html&lt;br /&gt;
&lt;br /&gt;
  Setting up a table (array) of strings in program memory is slightly complicated, but&lt;br /&gt;
  here is a good template to follow.&lt;br /&gt;
&lt;br /&gt;
  Setting up the strings is a two-step process. First, define the strings.&lt;br /&gt;
 */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;avr/pgmspace.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 const char string_0[] PROGMEM = &amp;quot;String 0&amp;quot;; // &amp;quot;String 0&amp;quot; etc are strings to store - change to suit.&lt;br /&gt;
 const char string_1[] PROGMEM = &amp;quot;String 1&amp;quot;;&lt;br /&gt;
 const char string_2[] PROGMEM = &amp;quot;String 2&amp;quot;;&lt;br /&gt;
 const char string_3[] PROGMEM = &amp;quot;String 3&amp;quot;;&lt;br /&gt;
 const char string_4[] PROGMEM = &amp;quot;String 4&amp;quot;;&lt;br /&gt;
 const char string_5[] PROGMEM = &amp;quot;String 5&amp;quot;;&lt;br /&gt;
 &lt;br /&gt;
 // Then set up a table to refer to your strings.&lt;br /&gt;
 const char *const string_table[] PROGMEM = {string_0, string_1, string_2, string_3, string_4, string_5};&lt;br /&gt;
 &lt;br /&gt;
 char buffer[30];  // make sure this is large enough for the largest string it must hold&lt;br /&gt;
 &lt;br /&gt;
 void setup()&lt;br /&gt;
 {&lt;br /&gt;
    Serial.begin(9600);&lt;br /&gt;
    while (!Serial);  // wait for serial port to connect. Needed for native USB&lt;br /&gt;
    Serial.println(&amp;quot;OK&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void loop()&lt;br /&gt;
 {&lt;br /&gt;
    /*&lt;br /&gt;
    Using the string table in program memory requires the use of special functions to retrieve the data.&lt;br /&gt;
    The strcpy_P function copies a string from program space to a string in RAM (&amp;quot;buffer&amp;quot;).&lt;br /&gt;
    Make sure your receiving string in RAM is large enough to hold whatever&lt;br /&gt;
    you are retrieving from program space.&lt;br /&gt;
    */&lt;br /&gt;
 &lt;br /&gt;
    for (int i = 0; i &amp;lt; 6; i++)&lt;br /&gt;
    {&lt;br /&gt;
       strcpy_P(buffer, (char *)pgm_read_word(&amp;amp;(string_table[i])));  // Necessary casts and dereferencing, just copy.&lt;br /&gt;
       Serial.println(buffer);&lt;br /&gt;
       delay(500);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 注意 ==&lt;br /&gt;
&amp;lt;code&amp;gt;PROGMEM&amp;lt;/code&amp;gt;キーワードを使用する場合、グローバル変数、または、関数内の変数でstaticキーワードを使用する必要があることに注意する。&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
以下の例は、関数内では動作しない。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 const char long_str[] PROGMEM = &amp;quot;Hi, I would like to tell you a bit about myself.\n&amp;quot;;&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
以下の例は、関数内に定義されていても動作する。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 const static char long_str[] PROGMEM = &amp;quot;Hi, I would like to tell you a bit about myself.\n&amp;quot;&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== F()マクロ ==&lt;br /&gt;
以下のような&amp;lt;code&amp;gt;Serial.print()&amp;lt;/code&amp;gt;を使用する場合、表示される文字列はRAMに保存される。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 Serial.print(&amp;quot;Write something on the Serial Monitor&amp;quot;);&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
シリアルモニタに多くのものを表示する場合、RAMが不足する可能性がある。&amp;lt;br&amp;gt;&lt;br /&gt;
この時、フラッシュメモリの空き容量がある場合は、&amp;lt;code&amp;gt;F()&amp;lt;/code&amp;gt;マクロを使用して、文字列をフラッシュメモリに保存することができる。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
 Serial.print(F(&amp;quot;Write something on the Serial Monitor that is stored in FLASH&amp;quot;));&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__FORCETOC__&lt;br /&gt;
[[カテゴリ:Arduino]]&lt;/div&gt;</summary>
		<author><name>Wiki</name></author>
	</entry>
</feed>