[SOLVED] Problem printing float value in Atmel Studio 6.2(version 6.2.1153)

Go To Last Post
20 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi everyone,

 

   Good day. I have a problem in printing float values in atmel studio 6.2. I think it may be a BUG. And also ftoa function also not available in Atmel Studio 6.2. I am using Atmega 32 with 8MHz crystal.  And my code is:

 

/*
 * Float_Bug.c
 *
 * Created: 12/11/2014 9:57:54 AM
 *  Author: Dev5
 */ 

#define F_CPU 8000000UL    // Atmega32 with clock 8MHz

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include <util/delay.h>


void uart0_init(void)
{
	
	UCSRA=0x00;
	UCSRB=0x18;
	UCSRC=0x06;
	UBRRH=0x00;
	UBRRL=0x33;
}

void transmitByte( unsigned char data )
{
	while ( !(UCSRA & (1<<UDRE)) )
	; 			                /* Wait for empty transmit buffer */
	UDR = data; 			        /* Start transmition */
}

void transmitString(char* string)
{
	while (*string)
	transmitByte(*string++);
}

int main(void)
{
	float data_i_f;
	float data_f = 1.1;
	unsigned int data_i = 22;
	char temp[20];
	
	uart0_init();
	_delay_ms(500);
	
	transmitString("\nin main");
	
	sprintf(temp,"\ndata_f=%f",data_f);
	transmitString(temp);
	_delay_ms(200);
	
	data_i_f = data_i / 10.0;
	
	sprintf(temp,"\ndata_i_f=%f",data_i_f);
	transmitString(temp);
	
	
    while(1)
    {
        //TODO:: Please write your application code 
    }
}

And the result i am getting is:

in main
data_f=?
data_i_f=?

 

This topic has a solution.

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

Last Edited: Thu. Dec 11, 2014 - 10:41 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need to include the floating point library,  libm.a + other bits, no idea how to do it in AS6.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks John. its there already John.  

 

  

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What about the -Wl,-u,vfprintf over-ride?

 

Also your display above only shows libm not libprintf_flt that you also require.

 

If in the Solution "tree" at the top right you right-click on the word "libraries" there is an "add library" dialog there and it should already have libprintf_flt.a listed.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes Clawson, even after adding libprintf_fit.a  also getting same result only. Am i need to add anything other.

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And what about -Wl,-u,vfprintf ?

 

(there's no question that this works so you are clearly doing something wrong - perhaps post raw build output and I'll tell you what it is).

Last Edited: Thu. Dec 11, 2014 - 09:39 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ok clawson. what is these -Wl,-u,vfprintf .  And i couldn't attach a zip.  how to attach a zip file.

 

 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

Last Edited: Thu. Dec 11, 2014 - 09:59 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Beneath this message editing box is an "add attachments" link in blue (big text!). use that if you have a .zip

 

I'm not sure why you need to attach a .zip though - I only asked for a copy of your build output. You should be able to do that with Ctrl-C (in Studio) and Ctrl-V (in your browser) to paste the build text into a message here.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Okay I am sorry. build output is 

------ Build started: Project: Float_Bug, Configuration: Debug AVR ------
Build started.
Project "Float_Bug.cproj" (default targets):
Target "PreBuildEvent" skipped, due to false condition; ('$(PreBuildEvent)'!='') was evaluated as (''!='').
Target "CoreBuild" in file "C:\Program Files\Atmel\Atmel Studio 6.2\Vs\Compiler.targets" from project "C:\Documents and Settings\Dev5\Desktop\SASI\AVR_STUDIO\AtmelStudio_6.2\Float_Bug\Float_Bug\Float_Bug.cproj" (target "Build" depends on it):
    Task "RunCompilerTask"
        Shell Utils Path C:\Program Files\Atmel\Atmel Studio 6.2\shellUtils
        C:\Program Files\Atmel\Atmel Studio 6.2\shellUtils\make.exe all 
        Building file: .././Float_Bug.c
        Invoking: AVR/GNU C Compiler : 4.8.1
        "C:\Program Files\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.1056\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG -DF_CPU=8000000UL  -Os -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega32 -c -std=gnu99 -MD -MP -MF "Float_Bug.d" -MT"Float_Bug.d" -MT"Float_Bug.o"   -o "Float_Bug.o" ".././Float_Bug.c" 
        Finished building: .././Float_Bug.c
        Building target: Float_Bug.elf
        Invoking: AVR/GNU Linker : 4.8.1
        "C:\Program Files\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.1056\avr8-gnu-toolchain\bin\avr-gcc.exe" -o Float_Bug.elf  Float_Bug.o   -Wl,-Map="Float_Bug.map" -Wl,--start-group -Wl,-lm -Wl,-lprintf_flt  -Wl,--end-group -Wl,--gc-sections -mmcu=atmega32  
        Finished building target: Float_Bug.elf
        "C:\Program Files\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.1056\avr8-gnu-toolchain\bin\avr-objcopy.exe" -O ihex -R .eeprom -R .fuse -R .lock -R .signature -R .user_signatures  "Float_Bug.elf" "Float_Bug.hex"
        "C:\Program Files\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.1056\avr8-gnu-toolchain\bin\avr-objcopy.exe" -j .eeprom  --set-section-flags=.eeprom=alloc,load --change-section-lma .eeprom=0  --no-change-warnings -O ihex "Float_Bug.elf" "Float_Bug.eep" || exit 0
        "C:\Program Files\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.1056\avr8-gnu-toolchain\bin\avr-objdump.exe" -h -S "Float_Bug.elf" > "Float_Bug.lss"
        "C:\Program Files\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.1056\avr8-gnu-toolchain\bin\avr-objcopy.exe" -O srec -R .eeprom -R .fuse -R .lock -R .signature -R .user_signatures "Float_Bug.elf" "Float_Bug.srec"
        "C:\Program Files\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.1056\avr8-gnu-toolchain\bin\avr-size.exe" "Float_Bug.elf"
           text       data        bss        dec        hex    filename
           1788         34          0       1822        71e    Float_Bug.elf
    Done executing task "RunCompilerTask".
    Task "RunOutputFileVerifyTask"
                Program Memory Usage     :    1822 bytes   5.6 % Full
                Data Memory Usage         :    34 bytes   1.7 % Full
    Done executing task "RunOutputFileVerifyTask".
Done building target "CoreBuild" in project "Float_Bug.cproj".
Target "PostBuildEvent" skipped, due to false condition; ('$(PostBuildEvent)' != '') was evaluated as ('' != '').
Target "Build" in file "C:\Program Files\Atmel\Atmel Studio 6.2\Vs\Avr.common.targets" from project "C:\Documents and Settings\Dev5\Desktop\SASI\AVR_STUDIO\AtmelStudio_6.2\Float_Bug\Float_Bug\Float_Bug.cproj" (entry point):
Done building target "Build" in project "Float_Bug.cproj".
Done building project "Float_Bug.cproj".

Build succeeded.
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========

Attachment(s): 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The important line is this one:

 "C:\Program Files\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.1056\avr8-gnu-toolchain\bin\avr-gcc.exe" -o Float_Bug.elf  Float_Bug.o   -Wl,-Map="Float_Bug.map" -Wl,--start-group -Wl,-lm -Wl,-lprintf_flt  -Wl,--end-group -Wl,--gc-sections -mmcu=atmega32  

You can see the -Wl,-lm,-printf_flt in there which is what links to libm.a and liprintf_flt.a but what's missing from that line is the thing I repeatedly asked you about and that is -Wl,-u,-vfprintf

 

Just bear with me while I switch computers and I'll add a screen shot of where you enable that in AS6...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes clawson. I got your point. Okay clawson i am waiting.

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And here's the picture:

 

 

You must have that box ticked if you use libprintf_flt.a or libprintf_min.a as it "turns off" the printf() code that you otherwise get by default (actually what it's really doing is saying to the linker "forget you already saw it")

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I got it clawson. Thank you so much. Now it is working. 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you so much clawson  and js . smiley yes .

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is an argument to say that AS6 could make this a WHOLE lot simpler. In the "linker settings" there should just be two drop lists or collections of radio buttons. One for scanf and one for printf. There would be three options "normal", "minimal" and "floating point". If "minimal" or "floating point" were selected it would both do the -Wl,-lprintf_XXX and the -Wl,-u,vfprintf without the user needing to know these "messy" details. In fact I kind of thought that was the whole point of IDEs.

 

I have made this same suggestion in threads on Freaks in the past (back to AS4 days in fact) but no one at Atmel has ever seen the light :-(

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Really very very sorry clawson. :( . here after i will do clear search. i am very sorry clawson.

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Mind if I borrow this thread?  I'm having the same issue, and I have all the settings mentioned implemented.

 

        "C:\Program Files (x86)\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.1056\avr8-gnu-toolchain\bin\avr-gcc.exe" -o FreeExpression.elf  cli.o dial.o FreeExpression.o hpgl.o keypad.o display.o main.o scale.o shvars.o stepper.o timer.o uart.o usb.o   -Wl,-Map="FreeExpression.map" -Wl,-u,vfprintf -Wl,--start-group -Wl,-lm -Wl,-lscanf_flt -Wl,-lprintf_flt  -Wl,--end-group -Wl,--gc-sections -mrelax -mmcu=atmega1281  
        Finished building target: FreeExpression.elf

 

So I'm kind of at a loss as to why this doesn't work:

 

// scratchpad="500"
sscanf(scratchpad,"%f",&fx);

 

fx=0

?

also tried

sscanf("500.00","%f",&fx);

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

try like this

 

   float fx;

scanf("%f", &fx);

printf("fx: %f", fx);

 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ummm you do know the difference between scanf and sscanf dont you?

If you do, then obviously your proposal doesnt make any sense at all.

Im using sscanf to scan a string to parse out a floating point value.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Good day. Sorry for mistake. 

char scratchpad[]="500.00";
float fx;

sscanf(scratchpad, "%f", &fx);

now it works i think

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN