2016-10-04 09:20 PM
2016-10-05 11:38 AM
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);
2016-10-05 06:16 PM
Thanks for your reply. I tried your fix but still the same problem occurs.
here u can find a screenshot of my ouput.2016-10-05 07:00 PM
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.
2016-10-05 08:39 PM
I tried both of em, putchar return me those weird char, puts return me nothing and the serial doesnt receive anything.
2016-10-06 09:24 AM
@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);
}
2016-10-14 12:26 AM
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 functionvoid 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++);
}
}
2016-10-14 05:56 AM
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);
}