I want to set up my project to store calibration values to EEPROM. I’m using an ATmega168, which has 512 bytes of space.
According to the arduino examples for EEPROM.write, it looks like I’m limited to writing a single 8 bit data per write command, however my values are in the range of 0-4000 (depending on the sensor).
How do I break up my values into 8 bit bytes and recombine them on a read?
4000 decimal = 0xFA0 in hex which fits into a 16 bit value.
Mask and shift the high 8 bits into a byte, write this to the EEPROM. Then mask for the lower 8bits into a byte then write this to the EEPROM. Reverse when reading the two bytes out of the EEPROM.
int CAL
FullByte = (highbyte << 8 ) // Shift the bits from highbyte (or high bite) into upper part of FullByte
FullByte = (FullByte & lowbyte) // Combine lowByte with highbyte
CAL = FullByte // push FullByte into the interger CAL
The assignment of highbtye is good but you need to ‘OR’ in the lowbyte since an ‘AND’ will clear the high order bits in FullByte ( a zero bit AND’ed with any bit value = zero).
Or this can be done in one line:
FullByte = (highbyte << 8) | lowbyte;
And depending on how the compiler works you may need to ‘cast’ the char value to an int value.
Got it. Thanks. No, haven’t tried it yet, I still need to finish off the code for collecting values, averaging, and determining a calibration value. As I was going to also compare new to old values to see if it needed re-saved and error detect, I wanted to get all the info I could before finalizing code.
What do you mean by “cast”?
I’m using Arduino 1.0.3
I was assuming when you declare the variables as ‘int’ and ‘byte’ they remained as such and the compiler did the format change. Is this a bad assumption?
What can happen is that the bits in lowbyte get shifted to the left but there isn’t any room for them so so lowbyte becomes 0x00 and then assigned to fullbyte. fullbyte would then have a value of 0x0000 instead of 0x5500 as expected.
To fix this you ‘cast’ the type of lowbyte to an int like this:
Now when the left shift is preformed it will use a 16 bit variable to shift the bits into and fullbyte will become 0x5500.
Some compilers will see that lowbyte needs to be 16 bits for the shift and will do the ‘cast’ auto-magically but some compilers will only do exactly as the code is written. So you must read the compiler documents, the C standards and then check the actual code operation.
Therefore, you should get the ‘pieces’ of your code working before it gets too complicated to easily debug. For instance, you read the high and low bytes for the EEPROM, combine them, do calculations on their values then display the result.
What do you do if the displayed result is nonsense? Where in the code is the problem? Hope you see the point now of testing each piece of code first then put the pieces together.
I ran into some issues with data types lately messing around with millis(), and hope I learned my lesson, but that’s a neat trick I didn’t know about. Thanks a lot!