2018
April Sunday 22

Bookmark and Share

Related Articles...

MILWAUKEE WI Arduino Meet Up Maker Space

WANTED:   TEAM MEMBERS TO START UP A NON-PROFIT Wisconsin Embedded Computing Group.

WANTED: Angle Investor Start-Up Funding - Have Locations In 3 Ward Milwaukee AVAILABLE!

Internet Infrastructure Teir- 1 Connected Facility! = We Own Our Own Internet Access Space!

Large Industrial Zoned Arduino Make Space Acquired!

OPEN-SOURCE Wisconsin INTERNET Backbone!!

Email :

COASMailBox @ gmail.com

MILWAUKEE WI Raspberry Pi Meet Up Make Space

WANTED: CORE TEAM MEMBER TO HELP START NON-PROFIT GROUP.

WANTED: Angle Investor Start-Up Funding

Large Industrial Zoned Raspberry Pi Make Space Acquired!

Email :

COASMailBox @ gmail.com

MILWAUKEE WI Internet of Things (IoT) Meet Up Make Space

WANTED: CORE TEAM MEMBER TO HELP START NON-PROFIT GROUP.

WANTED: Angle Investor Start-Up Funding

Large Industrial Zoned Make Space Acquired!

Full Advanced Fabrication Facility, machine Shop, Welding, Electronics Lab Equipment Acquired!

Email :

COASMailBox @ gmail.com

MILWAUKEE WI Machine Repair, & Installation  - Tier 1 Business Support

Machine Technician Mechanic 20+ Years  Personal Experienced In Industrial Factory Machine Servicing, Rebuilding, Installation And Repair - Plus A 35+ Years Experienced Computer Engineer Service Technician Team Ready For Your Outsourced Market Advantage Needs!

Hartung Industrial - Member - CGI Science & Technology Group

Email :

COASMailBox @ gmail.com


2017 DECEMBER REVIEW

Arduino Open Source Community member - Tier 1 Full Stack Developer

Arduino 101 : PROGMEN?

EMBEDDED COMPUTING PROGRAMMING

2017 OCTOBER | by Gene Casanova

Senior Systems Engineer


PROGMEN

"PROGMEN" is an Arduino source-code compiler directive; to modify a variable.  It should only be used with the datatypes defined in the header file "pgmspace.h".  It directs the Arduino compiler to put information into flash memory.  Normally, information would be saved to SRAM memory.


Usage

'PROGMEM' is part of the "pgmspace.h" library.  It is included automatically in modern versions of the Arduino IDE application. 

PROGMEM can be used with a single value, however, it is intended for storing larger blocks of data; usually easiest in an array, (or another C data structure).

When using an older version below 1.0 (2011), first include the library at the top of a sketch; using the following syntax:

#include <avr/pgmspace.h>

Using PROGMEM is a two-step procedure.  After putting data into Flash memory, it requires special methods (functions), also defined in the http://www.nongnu.org/avr-libc/user-manual/groupavrpgmspace.html[pgmspace.h] library, to read the data from program memory back into SRAM.


Syntax

const dataType variableName[] PROGMEM = {data0, data1, data3…​};
dataType
Any variable type.
variableName
The reference-name for the array of data.

NOTICE: Because 'PROGMEM' is a variable modifier, there is no rule to where it should go.  The Arduino compiler accepts all of the definitions below, which are also synonymous.  However, experiments have indicated, 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 IDE v13.  Earlier versions of the Arduino IDE may work better if "PROGMEM" is included after the variable name.

const dataType variableName[] PROGMEM = {}; // use this syntax.
const PROGMEM dataType variableName[] = {}; // or this syntax. const dataType PROGMEM variableName[] = {}; // NOT This Syntax!

Example Code

The following code, illustrates how to read and write, unsigned chars (bytes) and ints (2 bytes), to PROGMEM.

// Save unsigned integers:
const PROGMEM uint16_t charSet[] = { 65000, 68000, 68020, 10, 11234};
// Save chars:
const char signMessage[] PROGMEM  = {"Hello World! This is a character string; created to demonstrate holding this data into flash memory."};

Full Process Example

Example Arduino source-code using 'PROGMEN' for saving data into flash memory, and then, reading data from the flash memory locations, and outputting it to the serial port:

// Save unsigned integers:
const PROGMEM uint16_t charSet[] = { 65000, 68000, 68020, 10, 11234}; // Save chars: const char signMessage[] PROGMEM = {"Hello World! This is a character string; created to demonstrate holding this data into flash memory."};
unsigned int displayInt; int k; // counter variable char myChar; void setup() { Serial.begin(9600); while (!Serial); // Wait for serial port to establish. Needed for native USB.
// Read a 2-byte integer from the previously stored 'charSet' above; now in flash :
for (k = 0; k < 5; k++)
{
displayInt = pgm_read_word_near(charSet + k);
Serial.println(displayInt);
}
Serial.println(); // Read characters stored:
for (k = 0; k < strlen_P(signMessage); k++)
{
myChar = pgm_read_byte_near(signMessage + k);
Serial.print(myChar); } Serial.println(); } void loop() { }

 


Arrays of Strings

When working with large amounts of text, like lines on a LCD display, an array of strings can be used. Strings are arrays.  The following code is an example of using a two-dimensional array.

/* PROGMEM string demo
 * How to store a table of strings in program memory (flash), and retrieve them.
 * 
 * Setting up a table (array) of strings template:
 * 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"; // Next, set up a table to refer to the strings: const char* const string_table[] PROGMEM = {string_0, string_1, string_2, string_3, string_4, string_5}; char buffer[30]; // make sure the buffer is large enough for the largest string to hold. void setup()
{
Serial.begin(9600);
while(!Serial); // wait for serial port to connect. Needed for native USB
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 ); } }

NOTES

Variables must be globally defined, or, defined with the 'static' keyword, in order to work with PROGMEM.

// Save unsigned integers:
        const static char long_str[] PROGMEM = "Hi, I Am Outfitted With Outfitting Gear Only From CasanovasAdventures.com\n"

The F() Macro

In the following process, the string to be printed is normally saved in RAM.

Serial.print("Write something to the serial monitor");

When a sketch outputs a lot of content to the serial port, the RAM can be filled.  If there is free flash memory available, easily direct the string to be saved in the available flash memory; using the syntax:

Serial.print(F("Write something to the serial monitor, stored in flash memory."));

 


Arduino Open Source Community member - Tier 1 Full Stack Developer

'IoT & Embedded MAKER SPACE - MKE'

CONTACT to learn about forming a new maker group in Milwaukee, Racine, Kenosha, South Eastern WI And Northern IL, space and/or spaces.

An Industrial Zoned Making Space With Outfitted Machine Shop And Lab Space Is Available!

Learn And Create... Come Help..... Core Team Members Wanted,  Learning, Socializing/Networking... Start-Up Incubator Space, Students Tutoring Space, Retired... Hobbyists, Educators, Scientists, Researchers, Developers.

Mega Industrial Machines Building!!!!  AI Development.... Super Computing.... @ Tier 1 Level.  Locations looking to establish a network space, include Chicago IL, Melbourne Vic Australia.


Use The Technology Wisely & Keep It Simple

- Cheers!

Gene Casanova


Need More?   Need Help? ........Software, Network, System & Data Center Builder & Developer | Development Services Available - Freelance Small Jobs; To Outsourced Long Term Service Provider Contract Labor Available.

CGI Computer Wares | EST 1979

Send E-MailCONTACT: [click]@CGIComputerWares.com