abdoalghareeb
Member level 5
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.
[ MODERATOR ACTION : Added SYNTAX tags to code ]
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); //---------------------------------------------------------- }; } //========================================================== //==========================================================
[ MODERATOR ACTION : Added SYNTAX tags to code ]
Last edited by a moderator: