STM32F0 UART interrupt problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-06-27 02:38 AM
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
}
}
- Labels:
-
STM32F0 Series
-
UART-USART
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-06-27 09:46 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-06-27 10:13 PM
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
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-06-27 10:26 PM
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.
Up vote any posts that you find helpful, it shows what's working..
