cancel
Showing results for 
Search instead for 
Did you mean: 

USART issue

sky_kamran12
Associate II
Posted on October 05, 2013 at 21:02

I am getting faulty data while using USART. This is the program that i have written but what i get is the garbage values.

/* Includes */

#include ''stm32f4xx.h''

#include ''stm32f4xx_gpio.h''

#include ''stm32f4xx_tim.h''

#include ''stm32f4xx_usart.h''

/* Private macro */

/* Private variables */

/* Private function prototypes */

void Delay(__IO uint32_t nCount);

/* Private functions */

GPIO_InitTypeDef  GPIO_InitStructure;

TIM_TimeBaseInitTypeDef    TIM_InitStruct;

NVIC_InitTypeDef NVIC_InitStructure;

USART_InitTypeDef USART_InitStructure;

/**

 **===========================================================================

 **

 **  Abstract: main program

 **

 **===========================================================================

 */

int i;

char b;

void Delay(__IO uint32_t nCount)

{

while(nCount--)

{

}

}

int main(void)

{

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);

/* GPIOA clock enable */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

/* GPIOA Configuration:  USART3 TX on PA2 */

GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_11 | GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;

GPIO_Init(GPIOB, &GPIO_InitStructure);

/* Connect USART3 pins to AF2 */

// TX = PA2

GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);

GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);

USART_InitStructure.USART_BaudRate = 115200;

USART_InitStructure.USART_WordLength = USART_WordLength_8b;

USART_InitStructure.USART_StopBits = USART_StopBits_1;

USART_InitStructure.USART_Parity = USART_Parity_No;

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx ;

USART_Init(USART3, &USART_InitStructure);

USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt

NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; // we want to configure the USART1 interrupts

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;// this sets the priority group of the USART1 interrupts

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // this sets the subpriority inside the group

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // the USART1 interrupts are globally enabled

NVIC_Init(&NVIC_InitStructure); // the properties are passed to the NVIC_Init function which takes care of the low level stuff

USART_Cmd(USART3, ENABLE); // enable USART3

/* Infinite loop */

while (1)

{

}

}

void USART3_IRQHandler(void)

{

// check if the USART1 receive interrupt flag was set

USART_ClearFlag(USART3, USART_FLAG_RXNE);

b = USART_ReceiveData(USART3);

USART_SendData(USART3, b);

USART_ClearITPendingBit(USART3, USART_IT_RXNE);

}

4 REPLIES 4
Posted on October 05, 2013 at 21:46

Is this with an STM32F4-Discovery board?

Could there perhaps be a discrepancy in the 8 MHz crystal, and the setting for HSE_VALUE withing your project (25 MHz)?

void USART3_IRQHandler(void)
{
if (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) != RESET)
{
char b;
b = USART_ReceiveData(USART3); // Clears RXNE and interrupt
USART_SendData(USART3, b); // assumes TXE is set
}
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sky_kamran12
Associate II
Posted on October 06, 2013 at 13:44

I don't know how to set the HSE and HSI proper settings for using USART. I haven't configured these clock settings. 

sky_kamran12
Associate II
Posted on October 06, 2013 at 13:53

and yes it is a STM32f4 discovery board

Posted on October 06, 2013 at 14:57

If you are using a popular tool chain you might want to review the ''template'' project within the firmware examples. See how the project is constructed, the files used, the include paths specified, and the defines passed to the compiler.

Within your project you should ensure the definition of HSE_VALUE matches that of your crystal, and the review the clock/pll settings within system_stm32f4xx.c (SystemInit) code.

Consider also sending a continuous stream of characters (0x55 'U') and confirm the bit timing on  a scope.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..