How to Store Float and Negative Values on External EEPROM

by reza.fiftififty in Circuits > Arduino

4235 Views, 0 Favorites, 0 Comments

How to Store Float and Negative Values on External EEPROM

thumbnail2.jpg

Hello everybody, welcome to this tutorial.

In this tutorial we are going to talk about How to store float values in external EEPROMs. As you know, EEPROM is a kind of storage memory that you use to store your data which you want to have after even board is powered off. Data like user password, some dates or time and etc.

Some microcontrollers have internal EEPROM like Arduino and some don't, like STM microcontrollers. In some cases you have too much data that you want to store somewhere but your microcontroller doesn't have enough EEPROM capacity, So you need to use external EEPROMs.

Most of the external EEPROMs data buses are 8bit. It means that you can store 8bit length values in each address or in the other words, You can store values from 0 to 255 in each address.

float-size.png

In 8bit microcontrollers like Arduino the size of a float variable is 4 bytes or 32bits and the size of an integer value is 2 bytes or 16 bits, So you need to split them to 8bits values to store them on EEPROM. For example you need to split a float value to four separated 8 bits or an integer value to two separated 8 bits and then store each 8bits in each address. And when you want to read them you need to read each 8 bits and then place them together to form your value. But the problem is that this procedure doesn't work for float values and you can't form your initial float value by placing together these four bits, But it still works for integer values.

way1.png

One way to store float values is to separate each digits of your value ,and store each one, on one address of your external EEPROM. For example if you want to store 34.56 on your external EEPROM, you need to separate digits to 3, 4, 5, and 6.Then store each digit on specific address. The problem of this way is that you need to know where is the position of Decimal Point, so you have to store position of decimal point on a specific address on your EEPROM too.


For example the decimal point's position in number 34.56 is on third place, But in number 100.50 is on forth place.

uni.png

The simplest way that I'm going to explain you in this tutorial is to use Union. A union is a special data type available in C programming language that allows to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time.

The memory occupied by a union will be large enough to hold the largest member of the union. For example in our case, The size of our union will be equal to 4bytes which is equal to our float value size.

To define a union, You must use the union statement in the same way as you did while defining a structure. The union statement defines a new data type with more than one member for your program.

To store a float or a negative value on external EEPROM, You just need to store your float value in float variable that we defined in our union, And then just write unsigned char array members on your external EEPROM, because the memory location of these variables are the same.

For example if you want to store 34.56 value on your external EEPROM:

1: store your value in union float variable => Temp = 34.56;

2: store each unsigned char array member on external EEPROM memory location address:

x[0] => address0

x[1] => address1

x[2] => address2

x[3] => address3

It's done.

if you want to read this value, You just need to read each address from your external EEPROM and store them in our union's unsigned char array members.

For example:

addrress0 => x[0]

addrress1 => x[1]

addrress2 => x[2]

addrress3 => x[3]

And then just read union's float variable to form your data.

Also you can use this procedure to store integer values on your external EEPROM, You just need to use first two members of unsigned char array.

you can use this procedure to store negative float values on your external EEPROM too.

How to Store Float and Negative Variables On External EEPROM

You can use blow code that is written in Arduino IDE as an example code.

Code is written in C programming language and you can use it for another IDEs too.

Also you can see full explanation of example code and result of code running on Arduino in above video: