Wednesday, November 2, 2011

PROGMEM - some interesting stuff from Peter B

PROGMEM example

/*
 * Project:    PROGMEM_example
 * Author:     Peter Brook, tidied by Jane-Maree Howard
 * Date:       Tuesday 01/11/2011
 * Platform:   Arduino 22
 * Purpose:    To demonstrate storing data in Arduino's flash memory
 * Operation:  Description: Stores 10x10 matrix (int) in Flash memory
 *                          using the PROGMREM keywords & library
 *             Declare:     formatting buffer (char); word counter (int);
 *                          10X10 data matrix - myData[10][10] (int)
               Setup():     Serial@38400baud:TAKE CARE TO SET SM CORRECTLY         
               Loop():      Print to SM by ROWS, then by COLUMNS; 
 *                          setup() again & loop()
 */
#include {avr/pgmspace.h}      // PROGMEM library
 char chBuffer [5];            // Serial output formatting buffer
 word serialCount;             // word count (2 Bytes)
void setup(void)
{
    Serial.begin(38400);       // Serial connection @ 38400 baud!!
    Serial.println("\nPress space bar .");
//    while (Serial.read() != 32)   delay(1); // Wait for space 
}//setup()

// this is the data to be stored
const int myData[10][10] PROGMEM = {
    '0', '1', '2', '3', '4', '5', '6', '7', '8', 'A',
    '1', '1', '2', '3', '4', '5', '6', '7', '8', 'B',
    '2', '1', '2', '3', '4', '5', '6', '7', '8', 'C',
    '3', '1', '2', '3', '4', '5', '6', '7', '8', 'D',
    '4', '1', '2', '3', '4', '5', '6', '7', '8', 'E',
    '5', '1', '2', '3', '4', '5', '6', '7', '8', 'F',
    '6', '1', '2', '3', '4', '5', '6', '7', '8', 'G',
    '7', '1', '2', '3', '4', '5', '6', '7', '8', 'H',
    '8', '1', '2', '3', '4', '5', '6', '7', '8', 'I',
    '9', '1', '2', '3', '4', '5', '6', '7', '8', 'Z' };

void loop(void) 
{
    int i,j;      // loop counters
    byte n;       // Byte output vble
    
    // print heading for ROWS
    Serial.println("\nData by Rows.");
    for (i=0; i < 100;i++) 
    {
       // some formatting conditions
       if ((i %8)==0) 
          Serial.print("|");
       if ((i %16)==0) 
          Serial.println("");
       // now read a Byte from Flash memory
       n = pgm_read_byte_near(i);
       Serial.print(" ");
       // format output to Serial
       sprintf(chBuffer, "%02x", n);
       // print output 
       Serial.print(chBuffer);
       // next word (= 2 Bytes)
       serialCount++; 
     }//for (i)
    
    // print heading for COLUMNS
    Serial.println(" ");
    Serial.println("\nData by Columns.");
    for (i=0; i < 10; i++) 
    {
      Serial.println(" ");
      for (j=0; j < 10; j++) 
      {
        // print each column entry in BYTE format
        n = pgm_read_word_near(&myData[j][i]);
        Serial.print(n, BYTE);   
      }//for(j)
    }//for (i)
    
    // now set up to go round again
    Serial.println(" ");
    delay(3000);   
    setup();    
    Serial.println(" ");  //start over
}//loop()
/* END */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
See the following links to find out how some of this stuff works.. 
PROGMEM      printf
 

No comments:

Post a Comment