NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » printf_P problems

November 19, 2012
by Volleygod
Volleygod's Avatar

For the skimmers: Simply put here is the code :

printf_P(PSTR("%3.2f\r\n"),152.3658);

here is the result: ?

I can not for the life of me print floats, doubles, etc. I am sure it is something really simple I am missing but I can not figure out why???

For those who want the whole story: Ok I feel really stupid asking this question. I am a self-taught programmer (mostly web apps) I have working in many languages for many years and I am just making the move to micro controllers and C. I ordered the nerdkit a week or so ago and really love it. I have learned pretty quickly and can usually find any answer online. But this one has me stumped and I have googled the *** out if and haven't found anything due to the nature it is hard to get accurate search results. This started with the temp sensor project, but I quickly dismissed it and moved on because I really wanted to get to the scale project. (I am working on a project that uses strain gauges) I got everything working great except anytime I try to print a float it comes out with a ? It is driving me crazy... for my own purposes I wrote a application in C# (my primary language) to show the results (similar to the python program with the project) Again everything else works fine. I simplified the code to just print and it happens. Here is the entire code:

// printf_and_scanf.c
// for NerdKits with ATmega168
// mrobbins@mit.edu

#define F_CPU 14745600

#include <stdio.h>
#include <math.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>
#include <util/delay.h>
#include "../HelperLib/uart.h"
#include "../HelperLib/led.h"
//#include "../HelperLib/lcd.h"

int main() {
    led_init();
    led_on();
    _delay_ms(500);
    led_off();

    // start up the serial port
    uart_init();
    FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
    stdin = stdout = &uart_stream;
    printf_P(PSTR("Starting...\r\n"));

    printf_P(PSTR("%3.2f\r\n"),152.3658);
    while(1) {
    }

    return 0;
}
November 19, 2012
by Volleygod
Volleygod's Avatar

Oh yea I forgot to mention... I assume since no one else has reported this it is a compiler thing. I am using Atmel Studio 6 and have already turned off optimization.

November 19, 2012
by Volleygod
Volleygod's Avatar

Well since adding the last comment it helped me figure out search terms for the problem, I think I have found the solution and will try it when I get home. Sometime just posting the question helps answer it ;-)

November 20, 2012
by Ralphxyz
Ralphxyz's Avatar

Volleygod, please post your answer. There have been about a hundred post here in the Nerdkits forum referencing printf_P I am surprise you didn't find an answer.

As printf_P often causes problems there should be an article in the Nerdkits Community Library.

Ralph

November 20, 2012
by Volleygod
Volleygod's Avatar

It was just hard to find a solution because of the "?" I found dozend where printf_P wasn't working but to try and search for "?" or question mark didn't narrow the results. I did find the solution after realizing I needed to search for "Atmel Studio 6 printf float" Which led me to Here

It was a simple solution as I expected: for anyone in the future using Atmel Studio you must "...add -lprintf_flt or -lscanf_flt to the linker options which is done by listing printf_flt as a library."

Post a Reply

Please log in to post a reply.

Did you know that you need to think about wires differently when you're transmitting signals more than a few inches? Learn more...