AnsweredAssumed Answered

USART issue

Question asked by joyo.kamran on Oct 5, 2013
Latest reply on Oct 6, 2013 by Clive One
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);


}



Outcomes