Skip to main content
mauro2
Associate II
December 30, 2015
Question

USART interrupt problem

  • December 30, 2015
  • 3 replies
  • 847 views
Posted on December 30, 2015 at 12:18

I'm using a stm32f10x micorcontroller (steval-mki121v1 evaluation board) and i want to enable an interrupt event when a data is receive through the USART.

The problem is that the interrupt never occurs after the data is send.

here the code:

void USARTInit(void)

{

USART_InitTypeDef USART_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

NVIC_InitTypeDef nvicStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);

/* Configure USART Tx as alternate function push-pull */

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure USART Rx as input floating */

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &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_Tx | USART_Mode_Rx;

USART_Init(USART2, &USART_InitStructure);

USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

//USART_ClearITPendingBit(USART2,USART_IT_RXNE); need this?

//NVIC_EnableIRQ(USART2_IRQn); need this?

nvicStructure.NVIC_IRQChannel = USART2_IRQn;

nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;

nvicStructure.NVIC_IRQChannelSubPriority = 0;

nvicStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&nvicStructure);

USART_Cmd(USART2,ENABLE);

}

void USART2_IRQHandler() // never go here

{

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

{

char data = 0;

data = USART_ReceiveData(USART2);

USART_ClearITPendingBit(USART2,USART_IT_RXNE);

}

}

Edit: the trasmission working good instead. it is just not receiving data. 

  
    This topic has been closed for replies.

    3 replies

    Tesla DeLorean
    Guru
    December 30, 2015
    Posted on December 30, 2015 at 15:01

    Does it actually receive anything? ie no reception, no interrupt

    Does a simple polling/echo back loop work? ie  wait on RXNE, get char, wait on TXE, send back

    Is this in a .CPP file?

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    mauro2
    mauro2Author
    Associate II
    December 30, 2015
    Posted on December 30, 2015 at 17:49

    problem solved. the two xbee module i'm using for communication were not configured correctly.

    another question: when i send a string like, for example, ''Hello123'', i receive only the first letter (''H''). why? do i need a loop into the Handler function?

    Tesla DeLorean
    Guru
    December 30, 2015
    Posted on December 30, 2015 at 18:06

    It's rather hard to see what you are doing.

    When sending a string, you need to wait for TXE before sending each byte, the USART doesn't have any buffering beyond the single holding register. The interrupt routine would be dealing with one byte at a time, you shouldn't dwell there. Instead buffer the data, and serve a byte at a time at the TXE interrupt, when you are out of data to send turn off the TXE interrupt. A polled method to output a string would look like this

    void OutString(char *s)
    {
    while(*s)
    {
    while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
    USART_SendData(USART2, *s++); // Send Char
    }
    }

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