Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Float Values store in EEPROM of ATmega16

Status
Not open for further replies.

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
787
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,242
Please let me know that I wrote float values in EEPROM and when I go to retrieve the float value
it would not float its change to integer value????

How to adjust it?
 

andre_luis

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,308
Helped
1,166
Reputation
2,351
Reaction score
1,152
Trophy points
1,403
Location
Brazil
Activity points
54,230
If you are dealing to some C++ compiler, could convert float numbers to string notation for store on memory as 8 bit array, and perform the inverse conversion to retrieve data.


+++
 

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,021
Reputation
4,158
Reaction score
2,032
Trophy points
1,393
Location
Greece
Activity points
64,375
Which compiler are you using?

- - - Updated - - -

For AVR studio (avrgcc)


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <avr/io.h>
#include <avr/eeprom.h>
 
//declare an eeprom variable
float EEMEM my_eeprom_float;
 
// declare a ram variable
float my_ram_float;
 
 
int main(void)
 
{
 
    // assign a value
    my_eeprom_float = 1.234567;
 
    // save to eeprom
    eeprom_write_float(&my_eeprom_float, my_eeprom_float);
 
 
    // restore from eprom
    my_ram_float = eeprom_read_float(&my_eeprom_float);
 
    while(1);
 
}

 

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
787
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,242
I am using AVR Studio 4.

How to implement conversion from float to string and its inverse?

Please I want to learn how to implement?
 

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
787
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,242
Dear alexan_e,

I am using three digits 7-segment display.
AVR studio 4 ATmega16 @8MHz int.osc.

I heard sprintf uses more space in memory.

sprintf(my_string,"%.2f",my_float); what is .2?

sprintf(my_string,"%3.2f",my_float); what is 3.2?
 
Last edited:

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,021
Reputation
4,158
Reaction score
2,032
Trophy points
1,393
Location
Greece
Activity points
64,375
You can look at the flags explanation https://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm

.2 means round to two decimals so 1.2345 will be shown as "1.23"

3.2 is 3 char minimum to be printed and two decimal. If the value to be printed is shorter than this number, the result is padded with blank spaces so 1.2 will be shown as " 1.20"
 

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
787
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,242
Dear alexan_e,

Please more clarify this line below,

3.2 is 3 char minimum to be printed and two decimal. If the value to be printed is shorter than this number, the result is padded with blank spaces so 1.2 will be shown as " 1.20"

- - - Updated - - -

How to store float values using sprintf in EEPROM?
 

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,021
Reputation
4,158
Reaction score
2,032
Trophy points
1,393
Location
Greece
Activity points
64,375
In 3.2 the characters in the left side of the decimal point are at least three, if the digits are less than three then spaces are added.

Why do you want to store a float using sprintf?
 

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
787
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,242
Dear alexan_e,

Actually I am making digital ammeter using three 7-segment display and make it easy for user friendly.

User can easily set desired ampere, at set ampere values the relay operates in the ammeter and turn off the motor.

All program done successfully but problem in storing and retrieving values in eeprom.

Do you have any idea?
 

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
787
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,242
I want to make universal sub-routine that inputs any value (float , int , char) and display to three 7-segment display
with self dp management and as well as store values in EEPROM.

- - - Updated - - -

I wrote a subroutine, please read it,

Code:
void Print (float f)
{
char x,a,b,c,d,buff[10];
sprintf(buff , %f , f);
a=buff[0];
b=buff[1];
c=buff[2];
d=buff[3];

if(b = '.')
{
if(y==3)  dp is on when segment 3 is on.==> 1.23
DP_ON;
}
else if(c = '.')
{
if(y==2)  dp is on when segment 2 is on.==> 12.3
DP_ON;
}
}

- - - Updated - - -

What is difference between "eeprom_write" and "eeprom_update"?
 
Last edited:

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,021
Reputation
4,158
Reaction score
2,032
Trophy points
1,393
Location
Greece
Activity points
64,375
I'm not sure why you need a universal function, you as a programmer should me able to decide which type you want to use.
Using calculation with float wastes resources of both speed and memory, use only if needed.
You can just use a couple of arrays to hold "float" values as characters, eg
Code:
char my_float_array[] ="12.345";


What is difference between "eeprom_write" and "eeprom_update"?
Update checks the content of the eeprom and if the value you intend to write is the same as the existing one it skips the write, this can extend the eeprom life (since the write cycles are limited).
 

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
787
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,242
Ok,

If I want to store ADC values on EEPROM ,the ADC values are float in nature.

If want to display eeprom array values so how retrieve from below,

read_eeprom_array ((void *)RAMstring, (const void *)my_eeprom_float, 10);
Print(RAMstring);
 

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,021
Reputation
4,158
Reaction score
2,032
Trophy points
1,393
Location
Greece
Activity points
64,375
If I want to store ADC values on EEPROM ,the ADC values are float in nature.

Really? I always thought that the ADC result was a 10bit value , 0-1023 :roll:





Here is an example of reading and writing an array in eeprom


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <avr/io.h>
#include <avr/eeprom.h>
 
//declare an eeprom variable
char EEMEM my_eeprom_array[10];
 
// declare a ram variable
char my_ram_array[10] = "123456789";
char my_ram_array2[10];
char my_ram_array3[10];
 
 
int main(void)
 
{
 
    // save to eeprom five characters of my_ram_array starting from position 20
    eeprom_write_block(my_ram_array, 20, 5);
 
    // save to eeprom five characters of my_ram_array in location of my_eeprom_array
    eeprom_write_block(my_ram_array, &my_eeprom_array, 5);
 
    // restore from eeprom in my_ram_array2 five characters read from eeprom location of my_eeprom_array
    eeprom_read_block(my_ram_array2, my_eeprom_array, 5);
 
    // restore from eeprom in my_ram_array3 five characters read from eeprom location 20
    eeprom_read_block(my_ram_array3, 20, 5);
 
 
    while(1);
 
}

 

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
787
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,242
I said,
If I want to store ADC values on EEPROM ,the ADC values are float in nature.

You asked,
Really? I always thought that the ADC result was a 10bit value , 0-1023

Oh,sorry I wanted to say that input of ADC pin is float values like temperature,volts,amps etc.
 

paulfjujo

Advanced Member level 4
Joined
Jun 9, 2008
Messages
1,454
Helped
295
Reputation
590
Reaction score
278
Trophy points
1,363
Location
France 01800
Activity points
10,298
hello,



A simple float value is represented with 4 bytes (32 bits)
so you simply store your float value as 4 consecutive bytes..

Code:
float fvalue;
fvalue= 1.2345;
unsigned char *p;
unsigned char Adress;   // adresse for eeprom (in case of size=256bytes) 
// or use unisgned int if size is over

p=&fvalue;  // p pointe sur le flottant
Eeprom_Write(Adress,*(p));
Eeprom_Write(Adress+1,*(p+1));
Eeprom_Write(Adress+2,*(p+2));
Eeprom_Write(Adress+3,*(p+3));
Adress=Adress+4; // for the next float to store
and the reverse for reading back;
 
Last edited by a moderator:

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,021
Reputation
4,158
Reaction score
2,032
Trophy points
1,393
Location
Greece
Activity points
64,375
Oh,sorry I wanted to say that input of ADC pin is float values like temperature,volts,amps etc.

You don't need to use floats, do the calculations using long integers.
Assume that you have a Vref of 5v , in the voltage calculations you can use

Code:
Vadc= ADC * 5000 / 1023

Now the result will be in mV , 0mV to 5000mV so there is no need for floats
If you want to show it in the screen you just convert to an array "5000", show the first digit "5" then a dot "." and then the rest of the digits "000"
 

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
787
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,242
Thank you for reply.

Dear alexan_e,

I am using three 7-segment displays instead of four segments.

I am not only show mV values also value in Volts.

for example: 0.00 to 9.99 ==> 10.0 to 99.9 ==> 100 to 999.
 
Last edited:

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,021
Reputation
4,158
Reaction score
2,032
Trophy points
1,393
Location
Greece
Activity points
64,375
Thank you for reply.

Dear alexan_e,

I am using three 7-segment displays instead of four segments.

I am not only show mV values also value in Volts.

for example: 0.00 to 9.99 ==> 10.0 to 99.9 ==> 100 to 999.

How does this prevent you from using integers instead of floats?
It doesn't make a difference, when you have a value like 5000mV, depending on where you put the dot you change the scale.
You can replace any float you are using that has just three decimals with an integer multiplied by three.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top