AnsweredAssumed Answered

USART communication problem

Question asked by a.arun.003 on Jul 21, 2015
Latest reply on Jul 22, 2015 by a.arun.003
hi friends,

I am beginner for firmware programming. I am using stm32f3 discovery kit. I tried to communicate my discovery kit to my PC its communicating but i am not able to get the correct data. Whenever i transmit and receive it gives some junk value. Can anyone give me a solution to get a correct data. And my code is 


#include "main.h"


USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;  
extern uint32_t NbrOfDataToTransfer;
extern uint32_t NbrOfDataToRead;
extern __IO uint32_t TxCounter;
extern __IO uint32_t RxCounter;


static void NVIC_Config(void);
static uart_config(void);

int main(void)
{
//   NVIC_Config();
    uart_config();
   NVIC_Config();
    while (1);
    
}

static void NVIC_Config(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable the USART1 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

static uart_config(void)
{
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);


     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStructure.GPIO_PuPd =  GPIO_PuPd_NOPULL ;
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
 


    
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
    
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

     USART_InitStructure.USART_BaudRate = 4800;
     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(USART1,&USART_InitStructure);

   USART_Cmd(USART1, ENABLE);

     USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
    
    
}

#ifdef  USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)
{
 
  while (1)
  {
  }
}
#endif


void USART1_IRQHandler(void)
{
  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  {
    /* Read one byte from the receive data register */
    Rx_Buffer[RxCounter++] = (USART_ReceiveData(USART1) & 0x7F);
    
 

    if(RxCounter == NbrOfDataToRead)
    {
                USART_ITConfig(USART1, USART_IT_TXE, ENABLE);

            RxCounter =0;
    }
  }

  if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
  {   
    /* Write one byte to the transmit data register */
    USART_SendData(USART1, Tx_Buffer[TxCounter++] & 0x7F);
        
        if(TxCounter == NbrOfDataToTransfer)
    {

      USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
          USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

            TxCounter = 0;
    }
  }

Outcomes