AnsweredAssumed Answered

recieving data from usart

Question asked by rota90 on Apr 13, 2013
Latest reply on Apr 14, 2013 by rota90
Hi,
I am trying to execute a simple program that sends "ABC " from a pin  to another via usart. i can send but can not recieve and while debugging the cursor is blocked in this line :
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET) 
it means that the USART's  register RD doesn't recieve any data? can you help me to solve this problem?
this is mi code 


#include "stm32f4_discovery.h"




#define TxBufferSize   (countof(TxBuffer))
#define countof(a)   (sizeof(a) / sizeof(*(a)))


/* Declare the GPIO structure */
GPIO_InitTypeDef  GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
uint8_t TxBuffer[] = "ABC";
uint8_t RxBuffer[TxBufferSize];
int i;
/* Private function prototypes -----------------------------------------------*/
void Delay(__IO uint32_t nCount);


////////////////////




///////////////


int main(void)
{
  /* GPIOD Periph clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  


   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_OType =GPIO_OType_PP;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
  
  USART_InitStructure.USART_BaudRate = 9600; 
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_Even;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  
  /* Configure USARTy */
  USART_Init(USART2, &USART_InitStructure);
  
  /* Enable the USARTy */
  USART_Cmd(USART2,ENABLE);






  /* Enable the USART Receive interrupt */
 
 /* Initialize Leds LD3 and LD4 mounted on STM32VLDISCOVERY board */
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);
  
 STM_EVAL_LEDOn(LED4); 
  Delay(0xAFFFF);Delay(0xAFFFF);Delay(0xAFFFF);
  STM_EVAL_LEDOff(LED4);
  STM_EVAL_LEDOn(LED4); 
  i=0;
  while(i<TxBufferSize)
  {
    USART_SendData(USART2, TxBuffer[i]);
    while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
    {
    }
    while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
    {
    }
    RxBuffer[i] = USART_ReceiveData(USART2);
    i++;
  }
    
    
  while (1)
  {
     if(RxBuffer[0]=='A')
    {
    STM_EVAL_LEDOn(LED3);
   STM_EVAL_LEDOn(LED4);    
    Delay(0xAFFFF);
    STM_EVAL_LEDOff(LED3);
   STM_EVAL_LEDOff(LED4);
    Delay(0xAFFFF);
    }
  }
}

Outcomes