EEPROM help

Hey guys, I am checking to see if the first 2 bytes of EEPROM are 0x0000 (previously active but not currently) or 0xFFFF (never active), and if so then ignore what’s been stored.
I’ve got the library pulled in

#include <SPI.h>           // SPI library for sending data to the MAX7219s
#include <avr/pgmspace.h>  // PROGMEM library for putting font array into flash
#include <EEPROM.h>        // EEPROM library for storing message array in EEPROM

So I’ve got some variables defined

unsigned int x; // for:next counter
unsigned int lowEEPROM = 0;
unsigned int highEEPROM = 0;
unsigned int EEPROMend = 0;

I read the addresses & do a little math in setup

  Serial.print ("EEPROM 0, 1 = ");
  // check if loading from EEPROM
  lowEEPROM = EEPROM.read(0);
  Serial.print(lowEEPROM, HEX);
  highEEPROM = EEPROM.read(1);
  Serial.println (highEEPROM, HEX);
  EEPROMend = highEEPROM<<8 + lowEEPROM;
  Serial.print ("EEPROMend = ");
  Serial.println (EEPROMend, HEX);
  if (EEPROMend == 0 || EEPROMend == 0xffff){
    // do nothing
    dataEntry = PC;
  }
  else {
    dataEntry = Memory;
    for (x = 2; x<EEPROMend +1; x=x+1){
      Serial.print ("Writing array address ");
      Serial.print (x, HEX);
      displayArray[x-1] = EEPROM.read(x);
      Serial.print (" ");
      Serial.println (displayArray[x-1]);
    }
  }

The result I get from combining the 2 reads from 0, 1 makes no sense, and kicks me into the else{} code.

EEPROM 0, 1 = FFFF
EEPROMend = 7F80
Writing array address 2 255
Writing array address 3 255
Writing array address 4 255
Writing array address 5 255
Writing array address 6 255

How is this possible?

You're reading a byte value into an int variable. If it was an signed value you'd get signed extension so your 0xFF from EEPROM would be stored as signed int 0xFFFF; your high/low byte combination code would then crash and burn. Since you're using an unsigned value you won't get sign extension so the 0xFF you read would be stored as 0x00FF; this means it fails your comparison against 0xFFFF.

Okay, I changed the int's to bytes

byte lowEEPROM = 0;
byte highEEPROM = 0;
unsigned int EEPROMend = 0;

and still this results

EEPROM 0, 1 = FFFF
EEPROMend = 7F80
Writing array address 2 255
Writing array address 3 255
Writing array address 4 255
Writing array address 5 255
Writing array address 6 255

so that doesn't seem to be it.

Ok, I did the shift & add combine as 2 steps

  Serial.print ("EEPROM 0, 1 = ");
  // check if loading from EEPROM
  lowEEPROM = EEPROM.read(0);
  Serial.print(lowEEPROM );
  highEEPROM = EEPROM.read(1);
  Serial.println (highEEPROM );
  EEPROMend = highEEPROM<<8;
  EEPROMend = EEPROMend + lowEEPROM;
  Serial.print ("EEPROMend = ");
  Serial.println (EEPROMend, HEX);
  if (EEPROMend == 0 || EEPROMend == 0xffff){
    // do nothing
    dataEntry = PC;
  }
  else {
    dataEntry = Memory;
    for (x = 2; x<EEPROMend +1; x=x+1){
      Serial.print ("Writing array address ");
      Serial.print (x, HEX);
      displayArray[x-1] = EEPROM.read(x);
      Serial.print (" ");
      Serial.println (displayArray[x-1]);
    }
  }

  Serial.print ("dataEntry = (1=PC, 2=EEPROM)");
  Serial.println (dataEntry);

and now it works

EEPROM 0, 1 = 255255
EEPROMend = FFFF
dataEntry = (1=PC, 2=EEPROM)1
Setup
Displaying from 2 to 8
Displaying from 3 to 8
Displaying from 4 to 8

dang compiler …

That's odd. I suppose it must be something to do with the types of the intermediate values in that expression.

Although I can’t replicate those oddball results, part of the problem is because the addition operator has higher precedence than left or right shift. The statement “a = b<<8 + c;” compiles as if it were written “a = b << (8+c);” not the way that it looks “a = (b<<8) + c;”

Pete

Ah ha - so adding ( ) around the shift will likely resolve it

EEPROMend = (highEEPROM<<8) + lowEEPROM;

Will give that a try.

Got a bigger problem right now - I did something, and can’t figure out what, while trying to set a couple of variables based on EEPROMend being 0/0xffff or not.
Not sure
I’m getting this compile error:

eightx32display_progmem_eeprom.cpp:95: warning: only initialized variables can be placed into program memory area
eightx32display_progmem_eeprom.cpp: In function 'void setup()':
b_setup:4: error: expected primary-expression before '=' token

which highlights the first line f void setup, but the problem seems to be sooner.
Any ideas?
May have caused a typo as I was mousing around, but I can’t find it.
Let me post this and add a second post of the pre-setup code that I think is causing the problem.

So something here is causing the first line of void setup to look bad:

//a_presetup

#include <SPI.h>           // SPI library for sending data to the MAX7219s
#include <avr/pgmspace.h>  // PROGMEM library for putting font array into flash
#include <EEPROM.h>        // EEPROM library for storing message array in EEPROM

// define pins usage
// D0, D1 Serial interface
// D11-12-13 SPI interface
// daisy chaining's a pain in the wrist, address each one!
#define ssOutput = 10;
#define ss0 = 7; //MSB -> easy to plug a header into Arduino
#define ss1 = 6;
#define ss2 = 5;
#define ss3 = 4; // LSB

/* define keypad, 4 across, 5 down, eventually
 pins 2-3-8-9  rows
 pins 14-15-16-17 - columns
 */
// couple buttons for whatever, eventually
#define button0 = 18;
#define button1 = 19;

// define variables
// bytes to send to MAX7219s, with some initial data
// connect so that Upper Left is 7,7  Lower Right is 0,0
unsigned int arraySize = 1500; // 0 to xxx = max

byte displayArray[1499] = {
}; // displayArray/6 = # of characters that can be shown

byte updateDisplay = 0; // flag, update 7219's when high

unsigned int displayStart = 1; // pointer into displayArray
unsigned int displayEnd = 8; // pointer to end of the displayArray
unsigned int startX = 0;
unsigned int endX = 0;
unsigned int fontArrayPointer = 0;
byte goodFont = 0; // good display character received
unsigned int x; // for:next counter
byte digitRegister = 0;
unsigned long currentMillis;
unsigned long previousMillis;
// hold tome at each position - base this on a pot reading or something later
unsigned long holdDuration = 200; 

byte incomingByte = 0;

byte PCmode = 0;
byte SaveMode = 0;
const byte PC = 1;
const byte Memory = 2;
byte dataEntry = PC;
byte lowEEPROM = 0;
byte highEEPROM = 0;
unsigned int EEPROMend = 0;

// define 7219 register addresses for setup
byte decode_mode = 0x09; // 0x00 = No decode for digits 7-0
byte intensity_level = 0x0A; // 0x08 = mid level. Range is 0x00 to 0x0F
byte scan_limit = 0x0B; // 0x07 for all columns
byte shutdown_normal = 0x0C; // 0x00 - shutdown, 0x01 = normal
byte display_test = 0x0F; // 0x00 = normal, 0x01 = display test mode all on full

// define fonts, put in Flash memory
const byte PROGMEM fontArray[] = {
  0x00,0x00,0x00,0x00,0x00, // 0 "space" ASCII 0x20
  0xFb,0xFb,0x00,0x00,0x00, // 1 "!" 21
  0x20,0x60,0xc0,0x80,0x00, // 2 "'" tbd 27  apostrophe
  0x01,0x03,0x06,0x04,0x00, // 3 "," tbd 2c  comma
  0x24,0xff,0x24,0xff,0x24, // 4 " " tbd 23  #
  0x3c,0x42,0x81,0x81,0x00, // 5 "(" tbd 28  ( 
  0x00,0x81,0x81,0x42,0x3c, // 6 ")" tbd 29  ) 
  0x00,0x18,0x18,0x18,0x00, // 7 "-"  2d
  0x03,0x03,0x00,0x00,0x00, // 8 "." 2e  
  0x03,0x04,0x08,0x10,0x60, // 9 "/" 2f
  0x7e,0x89,0x91,0xa1,0x7e, // 10 "0" 30
  0x21,0x41,0xFF,0x01,0x01, // 11 "1" 31
  0x63,0x85,0x89,0x91,0x61, // 12 "2" 32
  0x42,0x89,0x89,0x89,0x76, // 13 "3" 33
  0xf8,0x08,0x08,0xff,0x08, // 14 "4" 34
  0xE1,0x91,0x91,0x91,0x8e, // 15 "5" 35
  0x7e,0x89,0x89,0x89,0x46, // 16 "6" 36
  0x80,0x8f,0x90,0x90,0x60, // 17 "7" 37
  0x6e,0x91,0x91,0x91,0x6e, // 18 "8" 38
  0x60,0x91,0x91,0x91,0x7e, // 19 "9" 39
  0x66,0x66,0x00,0x00,0x00, // 20 ":" 3A
  0x60,0x8d,0x90,0x60,0x00, // 21 "?" 3f
  // receive lower case, display as uppper case
  0x7f,0x90,0x90,0x90,0x7f, // 22 "a" 61
  0xff,0x91,0x91,0x91,0x6e, // 23 "b" 62
  0x7e,0x81,0x81,0x81,0x66, // 24 "c" 63
  0xff,0x81,0x81,0x81,0x7e, // 25 "d" 64
  0xff,0x91,0x91,0x81,0x81, // 26 "e" 65
  0xff,0x90,0x90,0x90,0x80, // 27 "f" 66
  0x7e,0x81,0x89,0x89,0x6e, // 28 "g" 67
  0xff,0x10,0x10,0x10,0xff, // 29 "h" 68
  0x00,0x81,0xff,0x81,0x00, // 30 "i" 69
  0x86,0x81,0x81,0xFF,0x80, // 31 "j" 6a
  0xff,0x10,0x28,0x44,0x83, // 32 "k" 6b
  0xff,0x01,0x01,0x01,0x01, // 33 "l" 6c
  0xff,0x20,0x18,0x20,0xff, // 34 "m" 6d
  0xff,0x20,0x18,0x04,0xff, // 35 "n" 6e
  0x7e,0x81,0x81,0x81,0x7E, // 36 "o" 6f
  0xff,0x88,0x88,0x88,0x70, // 37 "p" 70
  0x7e,0x81,0x8d,0x83,0x7e, // 38 "q" 71
  0xff,0x88,0x88,0x8c,0x73, // 39 "r" 72
  0x62,0x91,0x91,0x91,0x4e, // 40 "s" 73
  0x80,0x80,0xff,0x80,0x80, // 41 "t" 74
  0xfe,0x01,0x01,0x01,0xfe, // 42 "u" 75
  0xfc,0x06,0x03,0x06,0xfc, // 43 "v" 76
  0xfe,0x01,0x06,0x01,0xfe, // 44 "w" 77
  0xc7,0x28,0x10,0x28,0xc7, // 45 "x" 78
  0xe0,0x10,0x1f,0x10,0xe0, // 46 "y" 79
  0x87,0x89,0x91,0xa1,0xc1, // 47 "z" 7a

};  // 3F = 48 characters = 240 bytes

// movement control characters
const byte backspace = 0x5c; // use for backspace '\', delete from end
const byte hold =     0x2b; //  +
const byte oneLeft =  0x3c; //  <
const byte normal =   0x3d; //  =
const byte oneRight = 0x3e; //  >
byte move_control = normal;

// ************************************ //

and here’s the setup code

// b_setup

void setup(){

  pinMode (ssOutput, OUTPUT); // need for SPI
  pinMode (ss0, OUTPUT);
  digitalWrite (ss0, HIGH);
  pinMode (ss1, OUTPUT);
  digitalWrite (ss1, HIGH);
  pinMode (ss2, OUTPUT);
  digitalWrite (ss2, HIGH);
  pinMode (ss3, OUTPUT);
  digitalWrite (ss3, HIGH);

  pinMode (button0, INPUT); // internal pullup in IDE 1.0.1
  digitalWrite (button0,  HIGH);
  pinMode (button1, INPUT);
  digitalWrite (button1, HIGH);

  // turn on serial port
  Serial.begin(115200);
  Serial.println("");
  Serial.println ("start setup ");

  // turn on SPI port
  SPI.begin();

  /*  set up MAX7219 registers  */

  // loop thru the array from current start position and send it out
  // do it in 4 passes, one for each MAX7219, LSB out first

  // clear the MAX7219 digit registers

  digitRegister = 0x01;  // digit0, up to 0x80 for digit7
  for (x = 0; x<8; x=x+1){  
    digitalWrite (ss0, LOW);
    SPI.transfer (digitRegister);
    SPI.transfer(0);
    digitalWrite (ss0, HIGH);
    digitRegister = digitRegister+1;
  }
  // next digit
  digitRegister = 0x01;  // digit0, up to 0x80 for digit7
  for (x = 0; x<8; x=x+1){  
    digitalWrite (ss1, LOW);
    SPI.transfer (digitRegister);
    SPI.transfer (0);
    digitalWrite (ss1, HIGH);
    digitRegister = digitRegister+1;
  }

  // next digit
  digitRegister = 0x01;  // digit0, up to 0x08 for digit7
  for (x = 0; x<8; x=x+1){  
    digitalWrite (ss2, LOW);
    SPI.transfer (digitRegister);
    SPI.transfer(0);
    digitalWrite (ss2, HIGH);
    digitRegister = digitRegister+1;
  }

  // next digit
  digitRegister = 0x01;  // digit0, up to 0x08 for digit7
  for (x = 0; x<8; x=x+1){  
    digitalWrite (ss3, LOW);
    SPI.transfer (digitRegister);
    SPI.transfer(0);
    digitalWrite (ss3, HIGH);
    digitRegister = digitRegister+1;
  }

  // decode to No decode mode
  digitalWrite (ss0, LOW);
  SPI.transfer (decode_mode);
  SPI.transfer (0x00);
  digitalWrite (ss0, HIGH);
  digitalWrite (ss1, LOW);
  SPI.transfer (decode_mode);
  SPI.transfer (0x00);
  digitalWrite (ss1, HIGH);
  digitalWrite (ss2, LOW);
  SPI.transfer (decode_mode);
  SPI.transfer (0x00);
  digitalWrite (ss2, HIGH);
  digitalWrite (ss3, LOW);
  SPI.transfer (decode_mode);
  SPI.transfer (0x00);
  digitalWrite (ss3, HIGH);

  // intensity to high level
  digitalWrite (ss0, LOW);
  SPI.transfer (intensity_level);
  SPI.transfer (0x0f);
  digitalWrite (ss0, HIGH);
  digitalWrite (ss1, LOW);
  SPI.transfer (intensity_level);
  SPI.transfer (0x0f);
  digitalWrite (ss1, HIGH);
  digitalWrite (ss2, LOW);
  SPI.transfer (intensity_level);
  SPI.transfer (0x0f);
  digitalWrite (ss2, HIGH);
  digitalWrite (ss3, LOW);
  SPI.transfer (intensity_level);
  SPI.transfer (0x0f);
  digitalWrite (ss3, HIGH);

  // set scan limit to all 8 columns
  digitalWrite (ss0, LOW);
  SPI.transfer (scan_limit);
  SPI.transfer (0x07);
  digitalWrite (ss0, HIGH);
  digitalWrite (ss1, LOW);
  SPI.transfer (scan_limit);
  SPI.transfer (0x07);
  digitalWrite (ss1, HIGH);
  digitalWrite (ss2, LOW);
  SPI.transfer (scan_limit);
  SPI.transfer (0x07);
  digitalWrite (ss2, HIGH);
  digitalWrite (ss3, LOW);
  SPI.transfer (scan_limit);
  SPI.transfer (0x07);
  digitalWrite (ss3, HIGH);

  // dispay test On
  digitalWrite (ss0, LOW);
  SPI.transfer (display_test);
  SPI.transfer (0x01);
  digitalWrite (ss0, HIGH);
  delay(50);
  digitalWrite (ss1, LOW);
  SPI.transfer (display_test);
  SPI.transfer (0x01);
  digitalWrite (ss1, HIGH);
  delay(50);
  digitalWrite (ss2, LOW);
  SPI.transfer (display_test);
  SPI.transfer (0x01);
  digitalWrite (ss2, HIGH);
  delay(50);
  digitalWrite (ss3, LOW);
  SPI.transfer (display_test);
  SPI.transfer (0x01);
  digitalWrite (ss3, HIGH);
  delay(50);

  // dispay test to normal
  digitalWrite (ss0, LOW);
  SPI.transfer (display_test);
  SPI.transfer (0x00);
  digitalWrite (ss0, HIGH);
  delay(50);
  digitalWrite (ss1, LOW);
  SPI.transfer (display_test);
  SPI.transfer (0x00);
  digitalWrite (ss1, HIGH);
  delay(50);
  digitalWrite (ss2, LOW);
  SPI.transfer (display_test);
  SPI.transfer (0x00);
  digitalWrite (ss2, HIGH);
  delay(50);
  digitalWrite (ss3, LOW);
  SPI.transfer (display_test);
  SPI.transfer (0x00);
  digitalWrite (ss3, HIGH);
  delay(50);

  // shutdown to Normal mode
  digitalWrite (ss0, LOW);
  SPI.transfer (shutdown_normal);
  SPI.transfer (0x01);
  digitalWrite (ss0, HIGH);
  digitalWrite (ss1, LOW);
  SPI.transfer (shutdown_normal);
  SPI.transfer (0x01);
  digitalWrite (ss1, HIGH);
  digitalWrite (ss2, LOW);
  SPI.transfer (shutdown_normal);
  SPI.transfer (0x01);
  digitalWrite (ss2, HIGH);
  digitalWrite (ss3, LOW);
  SPI.transfer (shutdown_normal);
  SPI.transfer (0x01);
  digitalWrite (ss3, HIGH);

  // clear out the displayArray
  for (x=0; x<(arraySize+1); x=x+1){
    displayArray[x] = 0;
  }
  Serial.print ("EEPROM 0, 1 = ");
  // check if loading from EEPROM
  lowEEPROM = EEPROM.read(0);
  //  Serial.print(lowEEPROM );
  highEEPROM = EEPROM.read(1);
  //  Serial.println (highEEPROM );
  EEPROMend = (highEEPROM<<8) + lowEEPROM;
//  EEPROMend = EEPROMend + lowEEPROM;
    Serial.print ("EEPROMend = ");
    Serial.println (EEPROMend, HEX);
  if (EEPROMend == 0 || EEPROMend == 0xffff){
    // do nothing, normal startup waiting for data via serial port
  }
  else {
    // EEPROM had been previously loaded, start showing it 
    dataEntry = Memory;
    displayEnd = EEPROMend;
    for (x = 2; x<EEPROMend +1; x=x+1){
      Serial.print ("Writing array address ");
      Serial.print (x, HEX);
      displayArray[x-1] = EEPROM.read(x);
      Serial.print (" ");
      Serial.println (displayArray[x-1]);
    }
  }

//  Serial.print ("dataEntry source (1=PC, 2=EEPROM)" );
//  Serial.println (dataEntry);

//  Serial.println("Setup");

} // end setup
void loop(){}

presetup with

void setup(){}
void loop(){}

tacked on the end compiles fine.
I’m thinking it has to do with something towards the end where I was messing with the EEPROM address. I can’t seem to undo anything to fix it tho.

Ha! Found it - it was the defines!

#define ssOutput  10
#define ss0  7 //MSB -> easy to plug a header into Arduino
#define ss1  6
#define ss2  5
#define ss3  4 // LSB

had ='s & ;'s in them.

I had changed them from this format for some reason, screwed things up. byte ssOutput = 10;

If I change back to that now it compiles, ok. So I must have fixed whatever else I did.