AnsweredAssumed Answered

Tx-Rx problem with USART in STM32L- Discovery !

Question asked by kumar.bipin.001 on Apr 25, 2012
Latest reply on May 8, 2013 by Clive One
This is my code for Transmission :

void USART3_tx(void)
{
     USART_InitTypeDef USART_InitStructure;
     GPIO_InitTypeDef GPIO_InitStructure;
 
       /* Enable GPIOB and USART3 clock */
       RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
 
       GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
       GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
 
       /* Configure USART Tx and Rx as alternate function push-pull */
       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
       GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
       GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
       GPIO_Init(GPIOB, &GPIO_InitStructure);
 
       /* USARTx configured as follow:
       - BaudRate = 9600 baud  
       - Word Length = 8 Bits
       - One Stop Bit
       - No parity
       - Hardware flow control not enabled (RTS and CTS signals)
       - Receive and transmit enabled
       */
     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_No;
     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
     USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
     USART_Init(USART3, &USART_InitStructure);
 
     USART_Cmd(USART3, ENABLE);
 }

int main(void)
{
     GPIO_InitTypeDef GPIO_InitStructure;

     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;                     
       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;                     
       GPIO_Init(GPIOB, &GPIO_InitStructure);

     USART3_tx();

     USART_SendData(USART3, 0xFF);
     while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);

     GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_SET);
}


This is the code for Reception :


void USART3_rx(void)
{
     USART_InitTypeDef USART_InitStructure;
     GPIO_InitTypeDef GPIO_InitStructure;
 
       RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  
       GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
       GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
 
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
       GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
       GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
       GPIO_Init(GPIOB, &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_No;
     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
     USART_Init(USART3, &USART_InitStructure);
 
     USART_Cmd(USART3, ENABLE);
}

int main(void)
{
     GPIO_InitTypeDef GPIO_InitStructure;

     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;                     
       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;                     
       GPIO_Init(GPIOB, &GPIO_InitStructure);

     GPIO_WriteBit(GPIOB, GPIO_Pin_7, Bit_SET);
     USART3_rx();
     while(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) != RESET);
     while(USART_ReceiveData(USART3)!= 0xFF);
     GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_SET);
     GPIO_WriteBit(GPIOB, GPIO_Pin_7, Bit_RESET);
}

I am using two Xbee modules with two STM32L-Discovery boards to realize this Tx - Rx.

Kindly Help!

Outcomes