cancel
Showing results for 
Search instead for 
Did you mean: 

how to convert float to string in stm32f407vgt6 with FPU

abolfazlk873
Associate
Posted on January 17, 2014 at 08:59

hi

I am writing a simple code for converting float point number to the string , but this code just without enabling the FPU . if FPU was enabled , this code doesn’t work.

I am working with KEIL compiler. thank for your helps.


#include <
stm32f4xx.h
>

#include ''stm32f4xx_conf.h''

#include <
stdio.h
>


void Delay(__IO uint32_t nCount);


GPIO_InitTypeDef GPIO_InitStruct;

USART_InitTypeDef USART_InitStruct;



char txt2[11];

char txt1[17]=''error was ocurred'';

uint16_t d=0x0000;

unsigned int size;

float moisture=1655;



int main(void)

{


/* Periph clock enable */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

/////////

GPIO_PinAFConfig( GPIOA , GPIO_PinSource2 , GPIO_AF_USART2 );

GPIO_PinAFConfig( GPIOA , GPIO_PinSource3 , GPIO_AF_USART2 );

////////

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;

GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;

GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOA, &GPIO_InitStruct);

//////

USART_InitStruct.USART_BaudRate = 9600 ;

USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None ;

USART_InitStruct.USART_Mode = USART_Mode_Tx |USART_Mode_Rx ;

USART_InitStruct.USART_Parity = USART_Parity_No ;

USART_InitStruct.USART_StopBits = USART_StopBits_1 ;

USART_InitStruct.USART_WordLength = USART_WordLength_8b ;


USART_Init( USART2 , &USART_InitStruct);

//////

USART_Cmd( USART2 , ENABLE );

//////


while (1)

{


size=snprintf( txt2,11, ''%4.4f\n'', moisture);

for( d=0;d<
size
;d++){

USART_SendData( USART2 , txt2[d]);

while (!(USART2->SR & USART_SR_TC));

}

Delay(0x6FFFFF); 

}

}



void Delay(__IO uint32_t nCount)

{

while(nCount--)

{

}

}


void HardFault_Handler(void)

{

/* Go to infinite loop when Hard Fault exception occurs */

for( d=0;d<
17
;d++){

USART_SendData( USART2 , txt1[d]);

while (!(USART2->SR & USART_SR_TC));

}

while(1);

} 

3 REPLIES 3
frankmeyer9
Associate II
Posted on January 17, 2014 at 09:44

To convert a floating point value into a string, the FPU is not required. It is not even involved.

Much more important - does the printf/snprintf implementation of the C library you use support floating point values ?

I bet it does not.

You could pick another lib (resulting in a larger footprint of you application), or write such a conversion function on your own.

If I remember correctly, with Keil you can enable/disable printf floating point support in the project settings.

BTW: assuming that USART_SendData is working properly when called from the hardfault handler is a brave IMHO.

chen
Associate II
Posted on January 17, 2014 at 10:10

Hi

FM is absolutely right - converting a floating point number has nothing to do the the FPU.

FM is absolutely right -  can depend on the stdlib.h or stdio.h implementation. Keil and IAR both allow you to select small foot print implementations targeted for embedded applications.

If you do not want to use the 'standard' libraries - try searching for code examples.

Try ''convert float to string source code''

Here is an example the search found :

http://stackoverflow.com/questions/4392665/converting-string-to-float-without-atof-in-c

(I am not associated with any of the examples - do no hold me responsible !)

billr1
Associate II
Posted on January 19, 2014 at 18:16

As others have stated, it's likely your stdio library does not include support for %f format. The easiest way to get around this is something like this:

int intpart, fracpart;
intpart = (int)moisture;
fracpart = (int)((moisture - tmp) * 1000);
snprintf(txt2, 11, ''%4d.%04d
'', intpart, fracpart);