AnsweredAssumed Answered

how to convert float to string in stm32f407vgt6 with FPU

Question asked by k873.abolfazl on Jan 17, 2014
Latest reply on Jan 19, 2014 by randle.bill

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.

01.#include <stm32f4xx.h>
02.#include "stm32f4xx_conf.h"
03.#include <stdio.h>
04. 
05.void Delay(__IO uint32_t nCount);
06. 
07.GPIO_InitTypeDef  GPIO_InitStruct;
08.USART_InitTypeDef  USART_InitStruct;
09. 
10. 
11.char txt2[11];
12.char txt1[17]="error was ocurred";
13.uint16_t d=0x0000;
14.unsigned int size;
15.float moisture=1111.655;
16. 
17. 
18.int main(void)
19.{
20. 
21.  /* Periph clock enable */
22.    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
23.    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
24.    /////////
25.    GPIO_PinAFConfig( GPIOA , GPIO_PinSource2  , GPIO_AF_USART2 );
26.    GPIO_PinAFConfig( GPIOA , GPIO_PinSource3 , GPIO_AF_USART2 );
27.    ////////
28.    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
29.    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
30.    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
31.    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
32.    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
33.    GPIO_Init(GPIOA, &GPIO_InitStruct);
34.    //////
35.    USART_InitStruct.USART_BaudRate = 9600 ;
36.    USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None ;
37.    USART_InitStruct.USART_Mode =  USART_Mode_Tx |USART_Mode_Rx ;
38.    USART_InitStruct.USART_Parity = USART_Parity_No ;
39.    USART_InitStruct.USART_StopBits = USART_StopBits_1 ;
40.    USART_InitStruct.USART_WordLength = USART_WordLength_8b ;
41.     
42.    USART_Init( USART2 , &USART_InitStruct);
43.    //////
44.    USART_Cmd( USART2 , ENABLE );
45.    //////
46.     
47.    while (1)
48.    {
49.             
50.        size=snprintf( txt2,11, "%4.4f\n", moisture);
51.        for( d=0;d<size;d++){
52.                USART_SendData( USART2 , txt2[d]);
53.                while (!(USART2->SR & USART_SR_TC));
54.        }
55.        Delay(0x6FFFFF);       
56.    }
57.}
58. 
59. 
60.void Delay(__IO uint32_t nCount)
61.{
62.  while(nCount--)
63.  {
64.  }
65.}
66. 
67.void HardFault_Handler(void)
68.{
69.  /* Go to infinite loop when Hard Fault exception occurs */
70.  for( d=0;d<17;d++){
71.        USART_SendData( USART2 , txt1[d]);
72.        while (!(USART2->SR & USART_SR_TC));
73. }
74.  while(1);
75.}

Outcomes