XC8 sprintf question - Page 1
Forum section: electronics / general

author: mrcrud5
author_rank: Contributor
timestamp: December 18, 2017, 09:52:54 pm
content: 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;for(;; ){count = count + .1;sprintf(buffer, "%f",count);}return;}

author: Andy Watson
author_rank: Super Contributor
timestamp: December 18, 2017, 10:46:20 pm
content: 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);

author: cv007
author_rank: Frequent Contributor
timestamp: December 19, 2017, 12:38:05 am
content: QuoteYou are not going to get much help without providing the error message.

author: nctnico
author_rank: Super Contributor
timestamp: December 19, 2017, 12:46:48 am
content: 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.

author: TK
author_rank: Super Contributor
timestamp: December 19, 2017, 12:47:47 am
content: adding #include <stdio.h> compiles with just a warning:Code:#include <stdio.h>void main(void) {float count = 1;char buffer;for (;;) {count = count + .1;sprintf(buffer, "%f", count);}return;}Your program is in an infinite loop

author: westfw
author_rank: Super Contributor
timestamp: December 19, 2017, 04:03:54 am
content: 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.

author: JPortici
author_rank: Super Contributor
timestamp: December 19, 2017, 07:17:47 am
content: 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.

author: cv007
author_rank: Frequent Contributor
timestamp: December 19, 2017, 12:55:28 pm
content: QuoteThere 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.

author: mrcrud5
author_rank: Contributor
timestamp: December 19, 2017, 04:10:25 pm
content: Quote from: cv007 on December 19, 2017, 12:55:28 pmI 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?

author: JPortici
author_rank: Super Contributor
timestamp: December 19, 2017, 04:21:09 pm
content: i seem to recall it is. please copy and paste the whole line

author: SiliconWizard
author_rank: Super Contributor
timestamp: December 19, 2017, 04:24:47 pm
content: You could try the Microchip forums for such a specific question:http://www.microchip.com/forums/m677849.aspxThe 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!

author: cv007
author_rank: Frequent Contributor
timestamp: December 19, 2017, 04:52:37 pm
content: QuoteSimply- 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).

author: tggzzz
author_rank: Super Contributor
timestamp: December 19, 2017, 05:01:19 pm
content: Quote from: mrcrud5 on December 18, 2017, 09:52:54 pmUnless 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.

author: mrcrud5
author_rank: Contributor
timestamp: December 19, 2017, 06:10:11 pm
content: Quote from: cv007 on December 19, 2017, 04:52:37 pmYep I realize that now. Thanks! I feel kind of silly for not seeing this sooner.