cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 USART receiving float number

Posted on October 05, 2016 at 06:20

 

 

The original post was too long to process during our migration. Please click on the attachment to read the original post.
7 REPLIES 7
Posted on October 05, 2016 at 20:38

Not really grasping casting there...

// a test to read accelerometer x axis
uint16_t wrd = ((uint16_t)I2C_Read1Byte(0x68, 0x3B)<<8)|((uint16_t)I2C_Read1Byte(0x68, 0x3C));
USART_Float(USART2, (float)wrd/160f);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 06, 2016 at 03:16

Thanks for your reply. I tried your fix but still the same problem occurs.

here u can find a screenshot of my ouput.

0690X00000605WSQAY.png

Posted on October 06, 2016 at 04:00

void USART_Float(USART_TypeDef *USARTx, float x)
{
char str[80];
sprintf(str, ''%.02f'', x);
USART_putch(USARTx, str);
}

You want the puts version not the putch one.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 06, 2016 at 05:39

I tried both of em, putchar return me those weird char, puts return me nothing and the serial doesnt receive anything.

Posted on October 06, 2016 at 18:24

@upadhyay.bhavesh was there some purpose to the editing of this thread?

Try this

#include ''stm32f4xx.h''
#include ''stm32f4xx_usart.h''
#include ''stm32f4xx_rcc.h''
#include ''stm32f4xx_gpio.h''
#include ''misc.h''
void setup_Peripheral()
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; 
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART2, &USART_InitStruct);
USART_Cmd(USART2, ENABLE);
}
void USART_send_char(USART_TypeDef *USARTx, char c)
{
while(USART_GetITStatus(USARTx,USART_FLAG_TXE) == RESET);
USART_SendData(USARTx, c);
}
void USART_send_string(USART_TypeDef *USARTx, char *str) 
{
while(*str)
{
while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
USART_SendData(USARTx, *str++);
}
}
void USART_send_float(USART_TypeDef *USARTx, float x)
{
char str[32];
sprintf(str, ''%f

'', x);
USART_send_string(USARTx, str);
}
void USART_send_double(USART_TypeDef *USARTx, double x)
{
char str[32];
sprintf(str, ''%lf

'', x);
USART_send_string(USARTx, str);
}
int main(void)
{
setup_Peripheral();
USART_send_string(USART2, ''Testing

'');
USART_send_float(USART2, 156f);
USART_send_double(USART2, 1234789);
while(1);
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 14, 2016 at 09:26

Thanks man, it worked. However, it's weird how it worked and i will be glad if u can explain me how.

The problem was in this function

void USART_puts(USART_TypeDef* USARTx, uint8_t str[])
{
while(*str) USART_putch(USARTx, *str++);
}

and this one is working find for sending any string, yet it didnt work but your function works perfect eventhough i guess both do the same.

void USART_puts(USART_TypeDef* USARTx, char str[])
{
while(*str)
{
while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
USART_SendData(USARTx, *str++);
}
}

Posted on October 14, 2016 at 14:56

Ok, I see the issue now, I should be using GetFlagStatus not GetITStatus, the defines for the latter are different. I'm also front testing that the TX register is empty before writing to it, not after.

void USART_send_char(USART_TypeDef *USARTx, char c)
{
while(USART_GetFlagStatus(USARTx,USART_FLAG_TXE) == RESET);
USART_SendData(USARTx, c);
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..