The error above occurs in the next line:82 375 Const expression expected
???short m = 0, j, tmp = 0, len = dec;
The error above occurs in the next line:82 375 Const expression expected
???short m = 0, j, tmp = 0, len = dec;
Archive is ZIP and there is no m variable in the attached example. I have double checked it and it compiles without problems.public2010 wrote:I tried the software posted in RAR archive but fail to compile because the following error occured:The error above occurs in the next line:82 375 Const expression expected???short m = 0, j, tmp = 0, len = dec;
Code: Select all
void FloatToStrSimple(float f, char * txt, char dec) {
unsigned long n;
short i = 0, j, tmp = 0, len = dec;
unsigned long p[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
n = f * p[dec];
if (n < p[dec])
tmp = 1;
do {
if (i == dec) {
txt[i++] = '.';
continue;
}
txt[i++] = n % 10 + '0';
n /= 10;
} while((len-- > 0) || n);
if (tmp)
txt[i++] = '0';
txt[i] = '\0';
for (j = i - 1, i = 0; i < j; i++, j--)
tmp = txt[i], txt[i] = txt[j], txt[j] = tmp;
}
void main() {
char txt[11];
float f;
f = 0.001234;
FloatToStrSimple(f, txt, 4);
}
Code: Select all
unsigned long n;
short i = 0, j, tmp = 0, len = dec;
unsigned long p[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
Code: Select all
unsigned long n;
short i = 0, j, tmp = 0, len;
unsigned long p[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
len = dec;
...
You wouldn't want to waste more than half of the ROM space on such a simple task. That is why you should write your own formatting function. Here's another idea: use FloatToStr, then find the decimal dot, skip the desired number of characters and put the terminator. You got yourself another formatting functionpublic2010 wrote:And all this is happening because the ATmega8 does not support sprintf!!!!! Embarrassing!
Code: Select all
// LCD module connections
sbit LCD_RS at PORTD2_bit;
sbit LCD_EN at PORTD3_bit;
sbit LCD_D4 at PORTD4_bit;
sbit LCD_D5 at PORTD5_bit;
sbit LCD_D6 at PORTD6_bit;
sbit LCD_D7 at PORTD7_bit;
sbit LCD_RS_Direction at DDD2_bit;
sbit LCD_EN_Direction at DDD3_bit;
sbit LCD_D4_Direction at DDD4_bit;
sbit LCD_D5_Direction at DDD5_bit;
sbit LCD_D6_Direction at DDD6_bit;
sbit LCD_D7_Direction at DDD7_bit;
// End LCD module connections
void FloatToStrSimple(float f, char * txt, char dec) {
unsigned long n;
short i = 0, j, tmp = 0, len = dec;
unsigned long p[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
n = f * p[dec];
if (n < p[dec])
tmp = 1;
do {
if (i == dec) {
txt[i++] = '.';
continue;
}
txt[i++] = n % 10 + '0';
n /= 10;
} while((len-- > 0) || n);
if (tmp)
txt[i++] = '0';
txt[i] = '\0';
for (j = i - 1, i = 0; i < j; i++, j--)
tmp = txt[i], txt[i] = txt[j], txt[j] = tmp;
}
void main() {
char txt[10];
float f1, f2;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
f1 = 0.001234;
f2 = 3.141592654;
FloatToStrSimple(f1, txt, 4); // 4 decimal places
Lcd_Out(1, 1, txt); // Display f1 in the first row (0.0012)
FloatToStrSimple(f2, txt, 2); // 2 decimal places
Lcd_Out(2, 1, txt); // Display f2 in the second row (3.14)
}