cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with USART1 Interrupt

huonglq91
Associate II
Posted on May 15, 2014 at 15:13

Hi all.

I have a problem with USART1 Interrupt.

Actually, I use usart1 to receive data from my PC via terminal. But I didn't receive any data.

Could u show me the problem in my program.

P/s: I use led 7 segments to view received data from PC and led_nunmber[] is an array to save data for displaying in led 7 segments.

Here is my code for usart1:

void USART_Configuration(){

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOA, &GPIO_InitStructure);

USART1_InitStructure.USART_BaudRate = 9600;

  USART1_InitStructure.USART_WordLength = USART_WordLength_8b;

  USART1_InitStructure.USART_StopBits = USART_StopBits_1;

  USART1_InitStructure.USART_Parity = USART_Parity_No;

  USART1_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

  USART1_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USART1, &USART1_InitStructure);

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

//USART_ITConfig(USART1, USART_IT_TXE, ENABLE);

// Enable UART1

USART_Cmd(USART1, ENABLE);

}

void USART1_IRQHandler(void)

{

uint8_t i = 0;

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

{

RxBuffer1[RxIndex1++] = USART_ReceiveData(USART1);

if(RxIndex1 == 4)

{

  USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);

}

for (i = 0; i<= RxIndex1; i++){

led_number[i] = RxBuffer1[i];

}

}

if (RxIndex1 >= 4)

{

RxIndex1 = 0;

}

}
6 REPLIES 6
Posted on May 15, 2014 at 18:22

What board/chip are you using? ST makes dozens

How have you got the serial connection wired up to the PC?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
huonglq91
Associate II
Posted on May 16, 2014 at 01:21

I use STM32F0308 Discovery Kit.

And I use PL2303 to connect this Kit to my computer using TX,RX and GND wire.

Where is my problem?

Thank in advance.

Posted on May 16, 2014 at 01:32

Where is my problem?

Hard to tell, your code is incomplete, I don't see you enabling the interrupt in the NVIC Something like...

NVIC_InitTypeDef NVIC_InitStructure;
/* Enable and set USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
huonglq91
Associate II
Posted on May 16, 2014 at 03:50

I don't enable NVIC for USART because in stm32f0xx_usart.c file, I see this paragraph:

''Enable the NVIC and the corresponding interrupt using the function USART_ITConfig() if you need to use interrupt mode.''

So, following this instruction, I used USART_ITConfig() function but nothing happen.

I can't understand this problem. You see this problem?

But, I also use EXTI interrupt with NVIC priority is 0x00 and 0x01. So, if I use NVIC for USART, it's priority will be 0x02?

Thank for your help.

Posted on May 16, 2014 at 04:50

Look I'm not here to argue with you, the NVIC needs to be enabled, the priority is pretty irrelevant in the scale of things, you could have them all the same and it would function. If the NVIC is not enabled for your interrupt, then the processor isn't going to call your IRQHandler.

I have an STM32F0 example (

USART IRQ TX/RX Loop

) about 25-33% down [DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/UART%20example%20code%20for%20STM32F0&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=12033]this thread.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
huonglq91
Associate II
Posted on May 16, 2014 at 07:05

Clive,

Thank for your example. I'm following your code for STM32F0 with interrupt, but only transmit interrupt work. I check received data in string StringLoop by this way: When I send datas from my PC to the Kit, data will be saved to StringLoop to replace the old one. Then, I send the modified data in StringLoop to PC usingUSART_SendData(USART1, StringLoop[x]) in a for loop. But nothing display in terminal. Why can't I send data to the PC? And data will be save in the StringLoop? I only modified this paragraph of code:

StringLoop[rx_index++] = USART_ReceiveData(USART1);

for (x = 0; x< rx_index; x++ ){ USART_SendData(USART1, StringLoop[x]); } Many thanks.