Checksum, EEPROM

Greetings,

My goal is to have a checksum created for flash memory, store the checksum in EEPROM, and compare the two.

I am using AVRDude to write the checksum value a420 to EEPROM

avrdude: Device signature = 0x1e950f
avrdude> write eeprom 0 0xa4

write eeprom 0 0xa4

avrdude> write eeprom 1 0x20

write eeprom 1 0x20

avrdude> quit

I found example code for calculating the checksum. The code converts the checksum to a string (a420), and reads the values out of EEPROM location 0 and 1, and converts to a string (a420). When the compare occurs, the results indicate they are not equal, I display the value of the checksum (str_Temp4) = a420.

For debug purposes, I change that to display the string created from reading EEPROM 0, 1, and display that value (str1) = a420.

The displayed values are equal, I don't understand why it doesn't pass.

The code:

   {
  max_1.begin(MAX31865_3WIRE);  // set to 3WIRE  //Start probe 1
  max_2.begin(MAX31865_3WIRE);  // set to 3WIRE  //Start probe 2

  /* --- Initialize display -------------------------------------------------- */

  /****************************************************************************/
  display.begin(SSD1306_SWITCHCAPVCC);
  display.clearDisplay(); //clear display buffer
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(35, 25);
  display.setFont(&FreeSansBold9pt7b);
  display.print(""); //write to display buffer
  display.display(); // diplay contents of display buffer
  // init done
  pixeltest(); //functional test all pixels of display
  delay(2000);
  display.clearDisplay(); //clear contents of display buffer
  // Checksum routine //

  lng_Checksum = 0;
  int_Checksum = 0;

  for (i = 0; i < 0x3FFF; i++)
  {
    b = pgm_read_byte_near(i);
    lng_Checksum += (uint32_t)b;
  }
  int_Checksum = (uint16_t)(lng_Checksum & 0x0000FFFF);
  itoa (int_Checksum, str_Temp4, 16);

  a = EEPROM.read(0);
  z = EEPROM.read(1);
  itoa (a, str1, 16);
  itoa (z, str2, 16);

  strcat(str1, str2);


  /******************compare checksum to EEPROM*******************/
  if (str_Temp4 != str1)
  {
    while (str_Temp4 != str1) {
      display.clearDisplay();
      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(35, 25);//(30, 25) //sets base of character string
      display.setFont(&FreeSansBold9pt7b); //defines Font being used
      //display.print(" Ck Err"); //laod display buffer
      //display.print(str1); //laod display buffer
      display.print(str_Temp4);
      display.display(); //display above message
      display.clearDisplay();
    }
  }
  else
  {
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(30, 25);//(30, 25) //sets base of character string
    display.setFont(&FreeSansBold9pt7b);
    display.println("V1"); //laod display buffer
    display.display(); //display above message
    delay(2000);
    display.clearDisplay(); //clear contents of display buffer
    display.display(); // diplay contents of display buffer
    //
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(30, 25);//(30, 25) //sets base of character string
    display.setFont(&FreeSansBold9pt7b);
    display.println("  PASS"); //laod display buffer
    display.display(); //display above message
    delay(2000);
    display.clearDisplay(); //clear contents of display buffer
    display.display(); // diplay contents of display buffer

  }
}

I would just compare the two integer checksums, and save a whole lot of trouble.

if (str_Temp4 != str1)

If these are char arrays, you can't use != and ==, you need to use strcmp() for those.

@jremington - thank you for the quick reply. int_Checksum is displayed = 44174

To write that to EEPROM I would need to covert 44174 to hex? And when I read it back out, I don't know how to read the variables a and z and combing them without making them a string.

How would I accomplish that?

To write that to EEPROM I would need to covert 44174 to hex?

No. Just store the two bytes in the correct order, or use the EEPROM_writeAnything routine to store the int.

HEX is a human readable representation of a binary number, similar to decimal representation.

thanks to you both:
Cosme_Fulanito -

-If these are char arrays, you can't use != and ==, you need to use strcmp() for those.

Success in making the change to strcmp()

jremington -

Thank you for the direction:

-No. Just store the two bytes in the correct order, or use the EEPROM_writeAnything routine to store the int.
-HEX is a human readable representation of a binary number, similar to decimal representation.

I will look into using EEPROM_writeAnything routine as well.

jremington:
or use the EEPROM_writeAnything routine to store the int.

Is there any value in that? Current standard eeprom library has put and get methods that can read and write integers, structs etc

Is there any value in that?

Yes.