Convert Float to Int

Status
Not open for further replies.

abdoalghareeb

Member level 5
Joined
Feb 5, 2010
Messages
84
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,981
My circuit consists of atmega8A , 16x2LCD ,increase button and decrease button
my code is an example about convert float value to integer value (I need this function to write float value on serial EEPROM)
the problem is that when use (int) structure, the result always less by 0.1
that problem happened when the float value is more than the initial value (25.8)
the following video is about the results.


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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//==========================================================
//==========================================================
#include <mega8.h>
#include <delay.h>
#include <stdlib.h>
// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x12 ;PORTD
#endasm
#include <lcd.h> 
//----------------------------------------------------------
int i=0;;
float f=25.8;
float y=0;
char buffer[16];
//----------------------------------------------------------
void main(void)
{
PORTC=0b00111000;
DDRC =0b00000000;
// LCD module initialization
lcd_init(16);
//==========================================================
while (1)
{
//--------------- increase push button ---------------------
if(~PINC.5)   
    {
    f = f + 0.1; 
    y=f*10;
    i=(int)y; 
    }     
//-------------- decrease push button ----------------------
if(~PINC.3)
    {
    f = f - 0.1; 
    y=f*10;
    i=(int)y;
    }     
//---------------- display on LCD ---------------------------
lcd_clear();
lcd_gotoxy(0,0);lcd_putsf("F= ");ftoa(f,1,buffer);lcd_puts(buffer);
lcd_gotoxy(0,1);lcd_putsf("I= ");itoa(i,buffer);lcd_puts(buffer);
                  
delay_ms(300);
//----------------------------------------------------------
};                                                               
}
//==========================================================
//==========================================================



Your browser is not able to display this video.


[ MODERATOR ACTION : Added SYNTAX tags to code ]
 
Last edited by a moderator:

Hi,

there are a lot of questions:
* what range and what resolution do you need? (for your float)
* why do you need float at all? Usually one wants to avoid this with so small microcontrollers.
--> consider to use a fixed point, or an integer where the LSB represents the expected resolution for your float variable.
Example: if you want two decimals resolution: (like "12.34") then use an integer where the LSB represents "0.01". I.E. use the integer value of "1234" to represent "12.34".
* for writing a float into an EEPROM you don´t need to convert the variable at all. Just write the 4 bytes of the float and you are done.
* for writing to the LCD you don´t need a "float to integer" either. You rather need a "float to ASCII"

Klaus

added:
btw: your "losing 0.1" most probably is caused by this line: (I´m not sure, because I´m not very familiar with C)
"i=(int)y;"
the "int" statement may just truncate the decimals instead of rounding it.

"using i=(int)y+0.5;"

may solve this problem.
 
Writing a float as bytes to an EEPROM is easy.
Make a union to overlap the float with enough bytes (chars) and then save the bytes individually. If you do the reverse you get the float back again. Study union structures and you will get the idea.

Brian.
 
If you need float in embedded the :

Newlib-nano​


For ARM stuff much smaller footprint and very suitable for embedded work.

I think there was work done for Atmega as well.


Regards, Dana.
 
thanks
--- Updated ---

actually I cant Make a union to overlap the float with enough bytes
In Codevision It's only able in integer or long integer variable types
 
Last edited:

Hi,

a float just uses 4 bytes. I don´t see why the union does not work.

****
if you define the float using a pointer .. I guess you can address each single byte of the float like an array of bytes.

Klaus
 
thank you
It's solved
I use these two functions
ftoa() and atof()
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…