'FloatToStr' issues!

General discussion on mikroC PRO for AVR.
Author
Message
public2010
Posts: 94
Joined: 05 May 2009 18:31
Location: Somewhere in Europe

Re: 'FloatToStr' issues!

#16 Post by public2010 » 30 May 2013 19:02

I tried the software posted in RAR archive but fail to compile because the following error occured:
82 375 Const expression expected
The error above occurs in the next line:
short m = 0, j, tmp = 0, len = dec;
???

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: 'FloatToStr' issues!

#17 Post by aCkO » 31 May 2013 01:52

public2010 wrote:I tried the software posted in RAR archive but fail to compile because the following error occured:
82 375 Const expression expected
The error above occurs in the next line:
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.

Regards

public2010
Posts: 94
Joined: 05 May 2009 18:31
Location: Somewhere in Europe

Re: 'FloatToStr' issues!

#18 Post by public2010 » 31 May 2013 07:22

I replaced the 'i' with 'm', because the 'i' I had used it in software. But even so, the software should be compiled correctly, right? And why the error occurs?
I'll try again.

public2010
Posts: 94
Joined: 05 May 2009 18:31
Location: Somewhere in Europe

Re: 'FloatToStr' issues!

#19 Post by public2010 » 11 Jun 2013 19:18

I try again your example (I created a new project with ATmega8 using only the code below):

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);
}
but again I can't compile the code above because it displays the error:
3 375 Const expression expected Float to str simple.c
Why ?

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: 'FloatToStr' issues!

#20 Post by aCkO » 11 Jun 2013 20:39

The code compiles correctly on mikroC PRO for AVR v6.0.1 as you can see from the attachment.

You are probably using some older version of the compiler. Based on the line number on which the error was reported try to change this piece of code:

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};
to:

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;
...
Regards
Attachments
FloatTest.zip
(31.31 KiB) Downloaded 175 times

public2010
Posts: 94
Joined: 05 May 2009 18:31
Location: Somewhere in Europe

Re: 'FloatToStr' issues!

#21 Post by public2010 » 11 Jun 2013 21:42

thanks,

I managed to compile the software but there was another issue. I have two variables of type float: f1 and f2.
So, explain better, I using two float variables to read information from the two ADC channels. The two values ​​then need to print on the LCD. In your code, if use one variable's is fine, but if I try use two, does not work at all, even if I create FloatToStrSimple 1 and FloatToStrSimple 2.

What should I do in this case? To create two identical functions: FloatToStrSimple 1 and FloatToStrSimple 2 (I try that but don't work) OR something else (here I would like some advice).

And all this is happening because the ATmega8 does not support sprintf!!!!! Embarrassing!

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: 'FloatToStr' issues!

#22 Post by aCkO » 11 Jun 2013 21:57

public2010 wrote:And all this is happening because the ATmega8 does not support sprintf!!!!! Embarrassing!
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 function :)

The code I posted works fine with LCD. Here's an example ():

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)
}
Regards

public2010
Posts: 94
Joined: 05 May 2009 18:31
Location: Somewhere in Europe

Re: 'FloatToStr' issues!

#23 Post by public2010 » 15 Jun 2013 08:26

Great, now it works. Thank you.
FloatToStrSimple function takes up less space in EEPROM than the original FloatToStr.
So, FloatToStrSimple is much better. Should be used by default in the mikroC for AVR library.

darren@adret.co.za
Posts: 11
Joined: 22 Jul 2013 10:41

Re: 'FloatToStr' issues!

#24 Post by darren@adret.co.za » 21 Oct 2013 10:54

Thanks for the code "aCk0"! You helped me quite a bit.

FER2
Posts: 1
Joined: 16 May 2014 11:13

Re: 'FloatToStr' issues!

#25 Post by FER2 » 16 May 2014 11:16

First of all many thanks to @acKo. But I have small problem with negative values. Could you help me with it somehow?

fvgm
Posts: 96
Joined: 30 Oct 2007 16:16
Location: Brazil

Re: 'FloatToStr' issues!

#26 Post by fvgm » 12 Dec 2015 20:32

Thank you. Tested with mikroC PRO V6.6.2, worked!
thanks again.

Post Reply

Return to “mikroC PRO for AVR General”