Author Topic: XC8 sprintf question  (Read 6272 times)

0 Members and 1 Guest are viewing this topic.

Offline mrcrud5Topic starter

  • Contributor
  • Posts: 36
  • Country: ca
XC8 sprintf question
« on: December 18, 2017, 09:52:54 pm »
Hi,
I want to add .1 to a float variable in a loop and use sprintf to convert it to its equivalent ascii values which are stored in buffer. Here is the main loop.  This works when I use a int and use %d in the sprintf function but for some reason it keeps giving me an error if I try and use %f.

void main(void)
{

float count = 1;
char buffer[50];

for(;; )
{

count = count + .1;
sprintf(buffer, "%f",count);

}
return;
}

 

Offline Andy Watson

  • Super Contributor
  • ***
  • Posts: 2085
Re: XC8 sprintf question
« Reply #1 on: December 18, 2017, 10:46:20 pm »
Since "f" is for fixed-point notation I assume it needs to be told how many digits to use and where to put the point. Something like:
sprintf(buffer, "%3.1f",count);
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 826
Re: XC8 sprintf question
« Reply #2 on: December 19, 2017, 12:38:05 am »
Quote
it keeps giving me an error
You are not going to get much help without providing the error message.
 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 26906
  • Country: nl
    • NCT Developments
Re: XC8 sprintf question
« Reply #3 on: December 19, 2017, 12:46:48 am »
Using floating points numbers and allowing printing them needs the floating point math library to work. You'll probably need to change the project settings to use floating point. Note that floating point will make the resulting binary much bigger and it executes slow. Not a problem if you have enough space and enough processor time available.

BTW: %f by itself works. Format specifiers aren't needed but it is likely not to show the number of digits you want by default.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline TK

  • Super Contributor
  • ***
  • Posts: 1722
  • Country: us
  • I am a Systems Analyst who plays with Electronics
Re: XC8 sprintf question
« Reply #4 on: December 19, 2017, 12:47:47 am »
adding #include <stdio.h> compiles with just a warning:

Code: [Select]
#include <stdio.h>

void main(void) {
        float count = 1;
        char buffer[50];

        for (;;) {
                count = count + .1;
                sprintf(buffer, "%f", count);
        }
return;
}

Your program is in an infinite loop
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: XC8 sprintf question
« Reply #5 on: December 19, 2017, 04:03:54 am »
A lot of compilers require extra "project" settings to get a printf() implementation that includes floating point output; it's usually at least twice the size of an integer-only printf() (and that assumes that you already sucked in the floating math libraries for your other float calculations.)

I'm not sure whether XC8 is one of them, or how you'd specify the option if so.  But it's something to look for.

 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: XC8 sprintf question
« Reply #6 on: December 19, 2017, 07:17:47 am »
have you looked at the XC8 manual / XC8 library manual? it should have all the information you need..
(you can find both both of them inside the XC8/v1.xx/doc folder)

i generally don't bother with floats on PICs without a FPU, but if i do there are some steps to perform and things to know. besides the fact that the default float size is 24 bit instead of 32 bit, i don't remember which they are but they are all in the compiler manual.
« Last Edit: December 19, 2017, 07:21:27 am by JPortici »
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 826
Re: XC8 sprintf question
« Reply #7 on: December 19, 2017, 12:55:28 pm »
Quote
I'm not sure whether XC8 is one of them, or how you'd specify the option if so.  But it's something to look for.
There is nothing to set, except to include stdio.h. It will work fine provided you have enough flash space (at least 8kw part). The op is also trying to use the frame stack for the buffer- xc8 may not like that large a frame stack and will complain (and fail to compile). It probably also depends on which version of xc8 is  using, with the latest ones complaining about all kinds of integer conversions. There could also be a missing semi-colon somewhere else in code not shown, among a hundred other possibilities...so, it is pointless to speculate without the error provided.
 

Offline mrcrud5Topic starter

  • Contributor
  • Posts: 36
  • Country: ca
Re: XC8 sprintf question
« Reply #8 on: December 19, 2017, 04:10:25 pm »
Quote
I'm not sure whether XC8 is one of them, or how you'd specify the option if so.  But it's something to look for.
There is nothing to set, except to include stdio.h. It will work fine provided you have enough flash space (at least 8kw part). The op is also trying to use the frame stack for the buffer- xc8 may not like that large a frame stack and will complain (and fail to compile). It probably also depends on which version of xc8 is  using, with the latest ones complaining about all kinds of integer conversions. There could also be a missing semi-colon somewhere else in code not shown, among a hundred other possibilities...so, it is pointless to speculate without the error provided.

I keep getting the error "1347 can't find 0xAA words" .... AA is a bunch of different values. I tried to compile this code for a pic16f688 and it didn't work. However it worked when I used a PIC18f2550. Searching some other forums it looks like this error mean its out of memory? 
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: XC8 sprintf question
« Reply #9 on: December 19, 2017, 04:21:09 pm »
i seem to recall it is. please copy and paste the whole line
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14472
  • Country: fr
Re: XC8 sprintf question
« Reply #10 on: December 19, 2017, 04:24:47 pm »
You could try the Microchip forums for such a specific question: http://www.microchip.com/forums/m677849.aspx

The linker probably can't fit the whole floating point library in the program area in flash?
The PIC16F688 only has 7KB of program memory and 256B of RAM!
« Last Edit: December 19, 2017, 04:26:33 pm by SiliconWizard »
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 826
Re: XC8 sprintf question
« Reply #11 on: December 19, 2017, 04:52:37 pm »
Quote
error "1347 can't find 0xAA words"
Simply- You can't use printf float with only 4k w part like a 16f688.
The error is complaining because it cannot find enough free space to place the code- this is what it looks like when you try to fit more code into a pic than it has.
Compile for an 8k part (like 16F18855), and it will take up >4kw, and will also fail because the allowable stack frame size is being exceeded. Put the buffer outside of main and it will compile for an 8kw+ part, but with xc8 1.44 you will also get a number of implicit signed to unsigned conversion warnings (they seem to have changed warning levels on some errors, I used 1.41 because of that).
 

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 19506
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: XC8 sprintf question
« Reply #12 on: December 19, 2017, 05:01:19 pm »
I want to add .1 to a float variable in a loop

Unless you have exotic hardware, you can't do that. The reason is simple and fundamental: 0.1 doesn't have an exact binary representation!

You can add a number close to 0.1, though.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 
The following users thanked this post: mrcrud5

Offline mrcrud5Topic starter

  • Contributor
  • Posts: 36
  • Country: ca
Re: XC8 sprintf question
« Reply #13 on: December 19, 2017, 06:10:11 pm »
Quote
error "1347 can't find 0xAA words"
Simply- You can't use printf float with only 4k w part like a 16f688.
The error is complaining because it cannot find enough free space to place the code- this is what it looks like when you try to fit more code into a pic than it has.
Compile for an 8k part (like 16F18855), and it will take up >4kw, and will also fail because the allowable stack frame size is being exceeded. Put the buffer outside of main and it will compile for an 8kw+ part, but with xc8 1.44 you will also get a number of implicit signed to unsigned conversion warnings (they seem to have changed warning levels on some errors, I used 1.41 because of that).

Yep I realize that now. Thanks! I feel kind of silly for not seeing this sooner.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf