Continue to Site

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.

[SOLVED] 16F887 > Converting Long (and Double Long) to String

Status
Not open for further replies.

Eric_O

Advanced Member level 4
Joined
May 31, 2020
Messages
104
Helped
0
Reputation
0
Reaction score
0
Trophy points
16
Activity points
997
Bonjour,

I m looking for some code in order to display on an LCD a counting from :
- 1 000 000,00 to + 1 000 000,00

Thanks for your help.

Eric
 

Solution
hello,


i extended the last function to _999999.99 up to +999999.99
Code:
void float_to_ASCII_with_2_decimal_v5(float fx, char *pt)
{
unsigned long e = 0; // 0 to more than 65535.
unsigned long d = 0; // 0 to more than 65535.
float f0, chiffre_decimal=0.0;
int k=0;
if (fx < 0)
  {
     *(pt) = '-';     //  0cx2D
   chiffre_decimal=-fx;
   }
   else
   {
     *(pt) = '+'; // 0x2B signe +
     chiffre_decimal=fx;
   }
e =(unsigned long)  floor(chiffre_decimal); // Partie entière.
d =(chiffre_decimal - (float)e) * 100; // Partie décimale (2 chiffres);
// Partie entière de 99999 à 0 :
if (e/100000L >= 0) *(pt+1) =(unsigned char) (e/100000L) + 0x30;
e = e%100000L;
if (e/10000L >= 0) *(pt+2) =(unsigned char) (e/10000L) + 0x30;
e = e%10000L...
Hi,

what you see is the resolution problem of a single precision floating point variable.
2^23 = 8,388,608

Klaus
 

    Eric_O

    Points: 2
    Helpful Answer Positive Rating
what you see is the resolution problem of a single precision floating point variable.
2^23 = 8,388,608
What a pity the OP didn't see my comments about the use of floating point and signle precision in my Post #2.
Again - it is really a BAD IDEA to use floating point in these small(er) MCUs - scaled integer is way better if you reall yneed this sort of thing. However you are dealing with the ADC output with is integer to start with!
Susan
 

If you don't want to use the MikroC library functions then a quick Google (other search engines are available!) of 'ltoa c' and 'ftoa c' produce a number of examples.
(I'm in Melbourne - as you say, a lovely place but 0C this morning as we are in the middle of winter; promises to be a sunny day but cool!)
Susan
Bonjour Susan,

Sorry for my late reply. To be honest I’m more confident in a source, a solution, coming from
Paul.
I perform some quick searchs on the web but looks so indigestible 🤢 for me 😕 and after that have to translate in MikroC ...

https://www.microchip.com/forums/m183763.aspx
ftoa.c | searchcode
Convert a float to a string - Stack Overflow
https://www.geeksforgeeks.org/convert-floating-point-number-string/

Maybe this following link looks interesting, but it’s in assembler, not my favorite langage ...


Eric
 

Hello,

works fine on my 18F26K22 and UART output ...

Test Toggle Led Rouge RA4
Test float_to_ASCII_with_2_decimal_v7(f1,pt) gamme -9 999 999 à + 9 999 999;
Valeur de depart = - 9999999,00 Increment = 10000.01;

but in your code

Code:
while(1)                                                  // Endless loop.
{
  unsigned char *pointeur_de_char;      // Déclaration d'un pointeur (*) de char "pointeur_de_char".
 // En n'indiquant aucun chiffre entre les cochets [], le compilateur ajustera automatiquement la taille du
 // tableau, et allouera en mémoire RAM, le bon nombre d'éléménts, soit 13 ...
  char txt[13];

try to put declarations outside of the main
as global variables..

16F887 has less RAM and ROM than 18F ...
check in compile messages if you get an
.....IRP_Bit ...... => Bank problems

Result on terminal
- 9999999,00
- 9899999,00
- 9799999,00
- 9699999,00
- 9599999,00
- 9499999,00
- 9399999,00
- 9299999,00
- 9199999,00
- 9099999,00
- 8999999,00
- 8899999,00
- 8799999,00
- 8699999,00
- 8599999,00
- 8499999,00
- 8399999,00
- 8299999,00
- 8199999,00
- 8099999,00
- 7999999,00
- 7899999,00
- 7799999,00
- 7699999,00
- 7599999,00
- 7499999,00
- 7399999,00
- 7299999,00
- 7199999,00
- 7099999,00
- 6999999,00
- 6899999,00
- 6799999,00
- 6699999,00
- 6599999,00
- 6499999,00
- 6399999,00
- 6299999,00
- 6199999,00
- 6099999,00
- 5999999,00
- 5899999,00
- 5799999,00
- 5699999,00
- 5599999,00
- 5499999,00
- 5399999,00
- 5299999,00
- 5199999,00
- 5099999,00
- 4999999,00
- 4899999,00
- 4799999,00
- 4699999,00
- 4599999,00
- 4499999,00
- 4399999,00
- 4299999,00
- 4199999,00
- 4099999,00
- 3999999,00
- 3899999,00
- 3799999,00
- 3699999,00
- 3599999,00
- 3499999,00
- 3399999,00
- 3299999,00
- 3199999,00
- 3099999,00
- 2999999,00
- 2899999,00
- 2799999,00
- 2699999,00
- 2599999,00
- 2499999,00
- 2399999,00
- 2299999,00
- 2199999,00
- 2099999,00
- 1999999,00
- 1899999,00
- 1799999,00
- 1699999,00
- 1599999,00
- 1499999,00
- 1399999,00
- 1299999,00
- 1199999,00
- 1099999,00
- 999999,00
- 899999,00
- 799999,00
- 699999,00
- 599999,00
- 499999,00
- 399999,00
- 299999,00
- 199999,00
- 99999,00
1,00
100001,00
200001,00
300001,00
400001,00
500001,00
600001,00
700001,00
800001,00
900001,00
1000001,00
1100001,00
1200001,00
1300001,00
1400001,00
1500001,00
1600001,00
1700001,00
1800001,00
1900001,00
2000001,00
2100001,00
2200001,00
2300001,00
2400001,00
2500001,00
2600001,00
2700001,00
2800001,00
2900001,00
3000001,00
3100001,00
3200001,00
3300001,00
3400001,00
3500001,00
3600001,00
3700001,00
3800001,00
3900001,00
4000001,00
4100001,00
4200001,00
4300001,00
4400001,00
4500001,00
4600001,00
4700001,00
4800001,00
4900001,00
5000001,00
5100001,00
5200001,00
5300001,00
5400001,00
5500001,00
5600001,00
5700001,00
5800001,00
5900001,00
6000001,00
6100001,00
6200001,00
6300001,00
6400001,00
6500001,00
6600001,00
6700001,00
6800001,00
6900001,00
7000001,00
7100001,00
7200001,00
7300001,00
7400001,00
7500001,00
7600001,00
7700001,00
7800001,00
7900001,00
8000001,00
8100001,00
8200001,00
8300001,00
8400001,00
8500001,00
8600001,00
8700001,00
8800001,00
8900001,00
9000001,00
9100001,00
9200001,00
9300001,00
9400001,00
9500001,00
9600001,00
9700001,00
9800001,00
9900001,00
 

Hi,

adding/subtracting 100,000 to 9,999,999 may work.

But does adding/subtracting 0.1 to 9,999,999 also work? I´m not sure.

The problem I see is the 24 bit mantisse limit of a singleprecision floating point variable.

Klaus
 

Hello,

works fine on my 18F26K22 and UART output ...

Test Toggle Led Rouge RA4
Test float_to_ASCII_with_2_decimal_v7(f1,pt) gamme -9 999 999 à + 9 999 999;
Valeur de depart = - 9999999,00 Increment = 10000.01;

but in your code

Code:
while(1)                                                  // Endless loop.
{
  unsigned char *pointeur_de_char;      // Déclaration d'un pointeur (*) de char "pointeur_de_char".
// En n'indiquant aucun chiffre entre les cochets [], le compilateur ajustera automatiquement la taille du
// tableau, et allouera en mémoire RAM, le bon nombre d'éléménts, soit 13 ...
  char txt[13];

try to put declarations outside of the main
as global variables..

16F887 has less RAM and ROM than 18F ...
check in compile messages if you get an
.....IRP_Bit ...... => Bank problems

Result on terminal
Merci !
Will try.
Probably 16F is too limited.
I bought two 18F45K22 recently. Waiting for delivery. Will be able to try with and also use your sources easier on your website.
👍
 

Hi,

adding/subtracting 100,000 to 9,999,999 may work.

But does adding/subtracting 0.1 to 9,999,999 also work? I´m not sure.

The problem I see is the 24 bit mantisse limit of a singleprecision floating point variable.

Klaus
Hello,

I agree with you,
but at least,
maybe with GPS os Astronomy calculus ..must be concerned with high accuracy request ..
In this case A FPU arithmetic coprocesor (64bits float) could be used with a PIC 16F or 18F if needed.
(else PIC32 ou DSP ..or a old CRAY 1)
This (lazy) solution , without divide or multiply , could be enough for Eric .
I tested succesfully ( except accuracy problem around 0.0000 value and as you said with high values with decimal values.)
on a 16F877 .

To Eric ,
i tested on UART output because my MicroChip IrdA Demo Board allready handle a LCD on different Pins (soldered!)
it is OK , appart accuracy on decimal with very high values.. and just around 0.000
floating point "Zero" value is difficult to get .. so i declared -0.005< ZERO<0.005
with increment=0.01
- 0,05
- 0,04
- 0,03
- 0,02
- 0,01
- 0,00
+ 0,00
+ 0,01
+ 0,02
+ 0,03
+ 0,04
+ 0,05
+ 0,06
+ 0,07
+ 0,08
+ 0,09
+ 0,10
+ 0,11



see the log file in the zip .
i did litle changes in declaration and code , and added the signe '+' to see the space after the signe
.... long long value is not defined in MikroC (+- 9 223 372 036 854 775 807)
 

Attachments

  • _16F877_IRDA_Board_test_Eric_V7.zip
    19.5 KB · Views: 58

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top