cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0 UART interrupt problem

SShar.7
Associate

Hi,

I am using STM32F051 controller. I have done UART initialization coding but I am not getting interrupt for it. Is there any problem with alternate function register configuration. I am using Pin PB6 and PB7 for Uart. Also I tried finding example code for UART interrupt but didn't code for UART (STM32F051). Kindly help.

void UART_config(void){

//-----Enable USART Clock----------------------------------------

RCC->APB2ENR |= RCC_APB2ENR_USART1EN;

// Configure PB6 (Tx) and PB7 (Rx)

    GPIOB->AFR[0] |= 0x00000000;    //AFRL, PB6, UART1

    GPIOB->AFR[0] |= 0x00000000;     //AFRL, PB7, UART1

    GPIOB->MODER  &= ~0x0000F000;       //Clear PB6 & PB7

    GPIOB->MODER  |= GPIO_MODER_MODER6_1;    //Alternate Fxn mode

    GPIOB->MODER  |= GPIO_MODER_MODER7_1;    //Alternate Fxn mode

    GPIOB->OTYPER &= ~GPIO_OTYPER_OT_6;    //Output Push/Pull

    GPIOB->OTYPER &= ~GPIO_OTYPER_OT_7;    //Output Push/Pull

    GPIOB->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR6_1; //PB6 high speed

    GPIOB->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR7_1; //PB7 high speed

    GPIOB->PUPDR  |= GPIO_PUPDR_PUPDR6_0;     //PB6 Pull up

    GPIOB->PUPDR  |= GPIO_PUPDR_PUPDR7_0;     //PB7 Pull up

USART1->CR1 = 0x00000000;

USART1->BRR = (SystemCoreClock / 9600);

USART1->CR2 = 0x00000000; //To make number of stop bits 1

USART1->CR1 |=USART_CR1_TE | USART_CR1_UE | USART_CR1_RE | USART_CR1_RXNEIE | USART_CR1_TCIE;

}

void USART1_IRQHAndler(void){

if (USART1->ISR & USART_ISR_RXNE) {         // read interrupt

//for read operation

}

if (USART1->ISR & USART_ISR_TXE) {

//for transmit

}

}

3 REPLIES 3

Make sure the GPIOB clock is enabled before you start writing to its registers.

Note, that USART1TX/RX at PB6 and PB7 is AF0 !!!

The ISR name must match that in the interrupt vectors (startup file), case sensitive:

void USART1_IRQHandler(void) {

You need to enable the interrupt also in NVIC.

 NVIC_EnableIRQ(USART1_IRQn);

JW

GPIOB clock is enabled, ISR name is also changed and NVIC_EnableIRQ(USART1_IRQn); is done still facing the same problem. Its not going to interrupt. What could be the problem ?

void UART_config(void){

RCC->APB2ENR |= RCC_APB2ENR_USART1EN;

RCC->AHBENR |= RCC_AHBENR_GPIOBEN;

 GPIOB->MODER  &= ~0x0000F000;       //Clear PB6 & PB7

    GPIOB->MODER  |= GPIO_MODER_MODER6_1;    //Alternate Fxn mode

    GPIOB->MODER  |= GPIO_MODER_MODER7_1;   //Alternate Fxn mode

GPIOB->AFR[0] = 0x00000000;   //AFRL, PB6, UART1

    GPIOB->AFR[0] = 0x00000000;    //AFRL, PB7, UART1

    GPIOB->OTYPER &= ~GPIO_OTYPER_OT_6;    //Output Push/Pull

    GPIOB->OTYPER &= ~GPIO_OTYPER_OT_7;    //Output Push/Pull

    GPIOB->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR6_1; //PB6 high speed

    GPIOB->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR7_1; //PB7 high speed

    GPIOB->PUPDR  |= GPIO_PUPDR_PUPDR6_0;     //PB6 Pull up

    GPIOB->PUPDR  |= GPIO_PUPDR_PUPDR7_0;     //PB7 Pull up

USART1->CR1 = 0x00000000;

USART1->BRR = (SystemCoreClock / 9600);

USART1->CR2 = 0x00000000; //To make number of stop bits 1

USART1->CR1 |= USART_CR1_UE | USART_CR1_RE | USART_CR1_RXNEIE | USART_CR1_TE;

NVIC_EnableIRQ(USART1_IRQn);

}

void USART1_IRQHandler(void){

if (USART1->ISR & USART_ISR_RXNE) {         // receive

}

if (USART1->ISR & USART_ISR_TXE) { //transmit

}

}

Perhaps it is not actually receiving anything?

Check if any Error/Status flagging.

Check if viable in a polled mode.

Try having it service TXE by sending an 0x55 character, and scope that for bit timing verification.

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