cancel
Showing results for 
Search instead for 
Did you mean: 

USART interrupt problem

vinhbx1989
Associate
Posted on August 19, 2011 at 07:52

Hi Guys, I'm working on Usart programming in IAR environment. But I got a problem. The USART interrupt handler seemed not to be called. Here's my code:

void initUART2()

{

  USART_InitTypeDef  uart2Init;

    USART_ClockInitTypeDef uart2ClockInit;

  GPIO_InitTypeDef gpioA;

  NVIC_InitTypeDef NVIC_InitStructure;

  RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2, ENABLE );

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

  ///////////SetUart2 GPIO

  gpioA.GPIO_Pin=GPIO_Pin_2;

  gpioA.GPIO_Speed=GPIO_Speed_50MHz;

  gpioA.GPIO_Mode=GPIO_Mode_AF_PP;

  GPIO_Init( GPIOA, &gpioA);

   gpioA.GPIO_Pin = GPIO_Pin_3;

   gpioA.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_Init(GPIOA, &gpioA);

  //////////////////////////InitUART

  uart2Init.USART_BaudRate=9600;

  uart2Init.USART_WordLength=USART_WordLength_8b;

  uart2Init.USART_StopBits=USART_StopBits_1;

  uart2Init.USART_Parity=USART_Parity_No;

  uart2Init.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;

  uart2Init.USART_HardwareFlowControl=USART_HardwareFlowControl_None;        

  USART_Init(USART2,&uart2Init);

  ////////////////////////////InitUARTClock

     uart2ClockInit.USART_Clock=0;//USART_Clock_Enable;

   uart2ClockInit.USART_CPOL=0;//USART_CPOL_Low;

   uart2ClockInit.USART_CPHA=0;//USART_CPHA_1Edge;

   uart2ClockInit.USART_LastBit=0;//USART_LastBit_Disable;

  USART_ClockInit(USART2,&uart2ClockInit); 

  

   /////////////////InterruptConfig

  

NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =3;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init( &NVIC_InitStructure );

USART_ITConfig( USART2, USART_IT_RXNE, ENABLE );

    USART_ClearFlag(USART1,USART_FLAG_TC);

    //////////////////Enable UART 

    USART_Cmd(USART2,ENABLE);

}

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

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

void USART2_IRQHandler(void)

{

  if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)

  {

    /* Read one byte from the receive data register */

    RxBuffer2[RxCounter++] = USART_ReceiveData(USART2);         

    /* Clear the USART2 Receive interrupt */

    USART_ClearITPendingBit(USART2, USART_IT_RXNE);

    if(RxCounter == 100)

    {

      /* Disable the USART2 Receive interrupt */

      USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);

    }

  }

}

Are there problems in my source code? Plz, help me! Thanks!

1 REPLY 1
Posted on August 19, 2011 at 15:08

Well, I'd suggest you initialize the NVIC immediately after the RCC.

With the NVIC, be sure to set the vector table for your environment and the priority group.

Don't bother with the USART clock initialization, if you must use it, put it before the USART init, but for regular serial comms the default peripheral settings are fine.

Interrupts do work properly on all three USARTs.

Confirm you are actually receiving data properly without interrupts.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..