Question
how to convert float to string in stm32f407vgt6 with FPU
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);
}