How to write/read a float variable to EEPROM

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello all,
I need to save a float variable to the EEPROM and I give a look to AVR-libc, but, I am starting to learn C, so, I do not understand how to use it.

void eeprom_write_float  ( float *  __p, float  __value )  

Can you please help and explain how to use this function.

Thanks,
Manuel

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It sounds like you need to understand more about pointers. Try reading this online tutorial:http://home.netcom.com/~tjensen/ptr/pointers.htm

Also, generally speaking, you might want to use "void eeprom_update_float" instead of "void eeprom_write_float".

"float * __p" is the address of your target variable, while "__value" is the float value you want to save.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also you can put float into the eeprom byte by byte (just 4 bytes). You can write your own functions (see datasheet) to write and read to/from eeprom. But this case you should choose for training in C, in programming and in uC.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'd suggest you start by reading the article about GCC/EEPROM in the Tutorial Forum. But roughly:

float f_in_ee EEMEM;

eeprom_write_float(&f_in_ee, 3.1415926);

then later:

float f_in_RAM;

f_in_RAM = eeprom_read_float(&f_in_ee);

(I'll move this to GCC forum).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for your inputs.

I tried the below code, compile it without errors, but, with view memory there wasn't anything written in the EEPROM, also I attempted to define the "EEP_adr" as "int" but the compiler gave an error.

  
float EEP_adr=0X01;
float *ptr1;
float volt_element=3.51;
ptr1=&EEP_adr;


void eeprom_write_float  ( float *ptr1, float volt_element );

I know that "ptr1" as the address of the variable "EEP_adr", which is the EEPROM adr I want to write.

I read most of the tutorial you advise, but, there isn't any use of the above functions, also, the example are all with integer , char and array, etc.

Can you please help on saying what is wrong.

Thanks, Manuel

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

read chapter 2 and 3: [url] http://home.netcom.com/~tjensen/... [/url]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need to get a book on basic C. When a function has a prototype:

void eeprom_write_float  ( float *ptr1, float volt_element ); 

Then it's saying that the first parameter is a pointer to float - that is the address of a float variable (in EEMEM). The second parameter is a float value to be stored at that address.

So you start by defining a float variable in EEMEM:

float EEMEM place_to_store_float;

Then you'll use the & operator (meaning address of) as in:

eeprom_write_float(&place_to_store_float, volt_element);

You don't precede the call with "void" (that was just the prototye saying "this function returns nothing useful") and you don't include the variable type ("float *") in the invocation. You simply pass something that is of type float * and that means "address of place_to_store_float" which is written in C as &place_to_store_float.