code for EEPROM (Basic considerations)

Hello all,

Im starting with learn/test EEPROM write/read routines. Got an 24c256 chip and dealing with internal Atmega EEPROM and everything works as show on the tutorials and reference pages but got some questions:

After writing a byte of int value = 10, reading back that byte returns me value = 10 that sounds about right. Writing a byte of int value = -10 returns me value = 246. I understand that a byte have no room for keeping negative values considering the standard (of representation of a byte as range from 0 to 255). What is the default/common/usual manner of recording negative values into EEPROM? Suspect is that I will must waste 2 byte for this, but so I will need to create my own personal code for this or use something like http://www.arduino.cc/playground/Code/EEPROMWriteAnything ?

Other question is: Representation of ASCII table from 0 to 255, using Serial.print(value); returns me expected characters in the range of 0 to 126 but dont in the range of 128 to 254 accordingly with http://www.asciitable.com/ , it returns characters but in a mixed way considering the table values Atmega have its own character table for special characters? (extended table?)

And if someone know this one: on Serial.print(value); value for 126 returns me expected result that´s '~' but returns me '->' on LCD, so it´s something related to how LCD library was coded ?

Appreciate any clue.

Thanks,

Rodrigo

I understand that a byte have no room for keeping negative values considering the standard (of representation of a byte as range from 0 to 255).

Two's complement arithmetic (good Google terms) allows the range -128..+127 to be stored in a byte. Two's complement is how "int"s ( -32768 ..+32767) differ from "unsigned int"s (0..65535)

Writing a byte of int value = -10 returns me value = 246.

246 = 256 - 10.

Other answer:

Representation of ASCII table from 0 to 255

ASCII characters run from 0..127.

Allright AWOL, thanks.

So for negative numbers im stucked with the limit of -128. Im a bit confused how I will manage to store any needed data on the EEPROM, the only manner I think it´s convert anything else to char and deal with it on the code (that´s not that bad after all).

ASCII characters run from 0..127

I see..

But there are special, extended ASCII tables around (maybe called as extended ASCII with abuse of the term), like on http://www.ascii-code.com/ : "The extended ASCII codes (character code 128-255) There are several different variations of the 8-bit ASCII table. The table below is according to ISO 8859-1, also called ISO Latin-1. Codes 129-159 contain the Microsoft® Windows Latin-1 extended characters."

And looks that Atmega (Or IDE itself), have it´s own extended table. Sounds good if I translate those? Im right infront of the board :)

Thanks,

Rodrigo

So for negative numbers im stucked with the limit of -128.

That’s a limit you’re stuck with if you only use "signed char"s.
For larger numbers, just store "int"s (aka "short"s) or "long"s.

You don’t need to convert it to anything, just split it into bytes when you store it, and rebuild it into the appropriate datatype when you read it back.

int val;
..
..
byte MSB = val >> 8;
byte LSB = val & 0xFF;
// now write bytes to EEPROM
....
// then read them back
val = (MSB << 8) | LSB;

Ok, thanks once more AWOL

I need to study more as im very new to C++

Tweaked you code:

int val;
..
..
byte MSB = val >> 8; // I dont know what >> does, so >> 8 does (8 sounds like a byte anyway)
byte LSB = val & 0xFF; (I dont know what & does here, 0xFF sounds like hexa type anyway)
// now write bytes to EEPROM
....
// then read them back
val = (MSB << 8) | LSB; // Again cant get it.

What a tweak huh? :stuck_out_tongue:

I can see on http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B about Bitwise operations but dont know what they intended for and how to use.

In my opinion this is not the type of knowledge to be acquired on a forum board, I must go and start studying.

In this sense any suggestion where to start? books? online books or references? Maybe some O’Reilly material (they tend to be very good).

Thanks for the patience here,

Rodrigo

OK, >> is the bitwise shift right operator. If you have the decimal number 10, in binary this is 1010. If I shift it right one place, I divide it by two. So,

int a = 10;
int b = a >> 1;

Now 'b' contains the value 5 (binary 101). The least significant bit which was a zero is now lost. If I shift any byte value right eight places, I will get zero always.

Now, on the Arduino, an 'int' is sixteen bits wide, so shifting it right eight places moves the most-significant eight bits into the eight least-significant bits.

The '&' is the bitwise AND operation, and the '|' is the bitwise OR operation. ANDing a value with all ones simply copies the bits from the source, so if I had a sixteen bit value, and I want only the eight least-significant bits, I AND with 0xFF (decimal 255). If I wanted only the most significant eight bits left in place, I would AND with 0xFF00 (decimal 65280).

Very thanks for the detailed explanation.

This makes me understand a bunch of facts regarding data types on Arduino.

So for hexa, 0x is just the formatter? To tell compiler im declaring a value in hexa format? I mean, for hexa it will always starts with 0x ?

I will try Arduino for statiscal math and now I suspect it will not be any easy.

Not sure if this is right but Atmega168 have 1K of RAM, this mean i can deal with 256 variables at max, in long or float format (not counting RAM taken for another inherent internal tasks). With a 24c256 chip I can store 8.192 values in long or float format. So maybe with intensive write/read procedures to EEPROM i can achieve some tasks, but for now its uncertainty.

So maybe try converting to char for storage (even on the fly), and get more room for the momentarily calculations..... what a mess ! :P

Well, let´s see what happens next.

Many thanks,

Rodrigo

You might also want to adapt my "EEPROM_writeAnything" routine, which solves this "multiple bytes to store something" problem in the internal EEPROM.

http://www.arduino.cc/playground/Code/EEPROMWriteAnything

Replace the "write a byte" internal call with an appropriate "write a byte" statement for your external EEPROM chip. Same goes for reading, obviously.