cancel
Showing results for 
Search instead for 
Did you mean: 

USART communication problem

acc0094
Associate
Posted on July 21, 2015 at 08:31

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;

    }

  }
3 REPLIES 3
valerio2
Associate II
Posted on July 21, 2015 at 10:17

Hi,

I am experimenting the same issue with an stm32f429 discovery board, STM32Cube_FW_F4_V1.5.0 and UART5, trying to communicate with a gsm module held on an evb board. Transmission is ok but reception is not. Sometimes in the received buffer I have junk data while known information are expected. I am not putting my config/code here below in order to keep the post clear and give the precedence to Arun. But if someone deems that both the problems can be related and needs also my code I will post it asap.

Thank you.

valerio2
Associate II
Posted on July 21, 2015 at 11:29

Ok I solved the problem. Briefly, I was connecting the micro to a breakout board with an rs232 transciever held on it. The breakout has a female DB9 connector so as the gsm module's evb. Thus I made a little bus with two males DB9s. At the beginning I wanted to use RTS/CTS but later I decided for no hardware flow control. Unfortunately I left the corresponding transciever side DB9 pins connected to those of the module DB9 side and this was messing up my connection (and I would say almost obiouvsly) even though the gsm module was not using RTS/CTS lines (but noise still exists). I hope this can help somebody else.

acc0094
Associate
Posted on July 22, 2015 at 11:49

Thank you sir, I fixed the issue now i am able to send and receive the data correcly.