serial port - Floating point value not being displayed using printf function -
i have been trying display floating point value using printf function using serial port of atmega8 instead of displaying floating point value, '?' character displayed. output is
float: ?
here code
#include <stdio.h> #include <float.h> #include <avr/io.h> int printchar(char character, file *stream) { while(!(ucsra&0x20)); udr=data; return 0; } file uart_str = fdev_setup_stream(printchar, null, _fdev_setup_rw); int main(void) { float fl = 1.3; stdout = &uart_str; ucsrb=0x18; // rxen=1, txen=1 ucsrc=0x06; // no parit, 1-bit stop, 8-bit data ubrrh=0; ubrrl=71; //9600 baud rate while(1) { printf("\r\nfloat: %f",fl); } }
by default, minimalistic printf library used, doesn't support floating point numbers , results in "?" placeholder value. have tell linker use floating point library.
for example (from gnu makefile)
printf_lib_min = -wl,-u,vfprintf -lprintf_min
you have use floating point version so:
printf_lib_float = -wl,-u,vfprintf -lprintf_flt
depending on base makefile, there should section this:
# if left blank, use standard printf version. printf_lib = #printf_lib = $(printf_lib_min) #printf_lib = $(printf_lib_float)
just uncomment line containing printf_lib_float.
edit:
if use avrstudio4 without custom makefile this:
- use project - configuration
- select icon ("libraries")
- highlight "libm.a" , add right panel
- do same "libprintf_flt.a"
- under 5th icon "custom"
- select "[linker]" optins
- type in "-wl,-u,vfprintf" , [add]
Comments
Post a Comment