Skip to main content
Vijay Krishna
Associate II
June 22, 2020
Solved

Can anyone tell me how to configure external interrupts on stm32f103c8t6? is there anything wrong, my IRQHandler is not functioning properly...(actually, i wanted my LED(PA1) to glow when i interrupted it with push button(PB3) but it is not doing so.

  • June 22, 2020
  • 3 replies
  • 2181 views

#include "stm32f10x.h"

void EXTI3_IRQ_IRQHandler(void)

{

if(EXTI->PR & EXTI_PR_PR3)

{

 EXTI->PR |= EXTI_PR_PR3;

 GPIOA-> BRR |= GPIO_BRR_BS1;

}

}

int main(void)

{

RCC ->APB2ENR |= RCC_APB2ENR_IOPAEN;   //LED(OUTPUT PIN)(PA1)

GPIOA->CRL &= ~(GPIO_CRL_CNF1);

GPIOA->CRL |= GPIO_CRL_MODE1_0;

GPIOA->CRL &= ~(GPIO_CRL_MODE1_1);

RCC ->APB2ENR |= RCC_APB2ENR_IOPBEN;   //PUSH BUTTON(INPUT PIN)(PB3)

GPIOB->CRL |= GPIO_CRL_CNF3_0;

 GPIOB->CRL &= ~(GPIO_CRL_CNF3_1);

GPIOB->CRL &= ~(GPIO_CRL_MODE3);

RCC ->APB2ENR |= RCC_APB2ENR_AFIOEN;  //FOR ALTERNATE FUNCTION

EXTI->IMR |= EXTI_IMR_MR3;      //INTERRUPT MASK REGISTER(0:MASKING, 1:NON-MASKING)

EXTI->RTSR |= EXTI_RTSR_TR3;     // RISING EDGE TRIGGERED INTERRUPT

AFIO->EXTICR[0] = AFIO_EXTICR1_EXTI3_PB;

NVIC_EnableIRQ(EXTI3_IRQn);

NVIC_SetPriority(EXTI3_IRQn,0);

while(1)

{

  

GPIOA-> BSRR |= GPIO_BSRR_BR1;

}

}

This topic has been closed for replies.
Best answer by Vijay Krishna

Thanks, i detected the glitch/error, and you are right: i had to only add a delay (not a breakpoint exactly, but a delay function) before the line: GPIOA-> BSRR |= GPIO_BSRR_BR1; in the while loop because the interrupt handler function returns to the while loop after handling the interrupt so maybe my code looks like this:

void EXTI3_IRQHandler(void)
 
{
 
 
if(EXTI->PR & EXTI_PR_PR3)
 
{
 
 EXTI->PR |= EXTI_PR_PR3;
 
 GPIOA-> BRR |= GPIO_BRR_BS1;
 
 
}
 
 
}
 
 
 
int main(void)
 
{
 
 
RCC ->APB2ENR |= RCC_APB2ENR_IOPAEN; //LED(OUTPUT PIN)(PA1)
 
GPIOA->CRL &= ~(GPIO_CRL_CNF1);
 
GPIOA->CRL |= GPIO_CRL_MODE1_0;
 
GPIOA->CRL &= ~(GPIO_CRL_MODE1_1);
 
 
RCC ->APB2ENR |= RCC_APB2ENR_IOPBEN; //PUSH BUTTON(INPUT PIN)(PB3)
 
GPIOB->CRL |= GPIO_CRL_CNF3_0;
 
 GPIOB->CRL &= ~(GPIO_CRL_CNF3_1);
 
GPIOB->CRL &= ~(GPIO_CRL_MODE3);
 
 
RCC ->APB2ENR |= RCC_APB2ENR_AFIOEN; //FOR ALTERNATE FUNCTION
 
EXTI->IMR |= EXTI_IMR_MR3; //INTERRUPT MASK REGISTER(0:MASKING, 1:NON-MASKING)
 
EXTI->RTSR |= EXTI_RTSR_TR3; // RISING EDGE TRIGGERED INTERRUPT
 
AFIO->EXTICR[0] = AFIO_EXTICR1_EXTI3_PB; //ALTERNATE FUNCTION CONFIGURATION FOR-PB3) 
 
 
 
NVIC_EnableIRQ(EXTI3_IRQn);
 
NVIC_SetPriority(EXTI3_IRQn,0);
 
 
while(1)
 
{
 
 
for(volatile int i=0; i<=10000000; i++);
GPIOA-> BSRR |= GPIO_BSRR_BR1;
 
 
}
 
}

3 replies

TDK
June 22, 2020

Put a breakpoint in EXTI3_IRQ_IRQHandler and see if it's getting there.

Could be that the LED lights up for only a few clock cycles and your eye can't see it.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Vijay Krishna
Vijay KrishnaAuthorBest answer
Associate II
June 23, 2020

Thanks, i detected the glitch/error, and you are right: i had to only add a delay (not a breakpoint exactly, but a delay function) before the line: GPIOA-> BSRR |= GPIO_BSRR_BR1; in the while loop because the interrupt handler function returns to the while loop after handling the interrupt so maybe my code looks like this:

void EXTI3_IRQHandler(void)
 
{
 
 
if(EXTI->PR & EXTI_PR_PR3)
 
{
 
 EXTI->PR |= EXTI_PR_PR3;
 
 GPIOA-> BRR |= GPIO_BRR_BS1;
 
 
}
 
 
}
 
 
 
int main(void)
 
{
 
 
RCC ->APB2ENR |= RCC_APB2ENR_IOPAEN; //LED(OUTPUT PIN)(PA1)
 
GPIOA->CRL &= ~(GPIO_CRL_CNF1);
 
GPIOA->CRL |= GPIO_CRL_MODE1_0;
 
GPIOA->CRL &= ~(GPIO_CRL_MODE1_1);
 
 
RCC ->APB2ENR |= RCC_APB2ENR_IOPBEN; //PUSH BUTTON(INPUT PIN)(PB3)
 
GPIOB->CRL |= GPIO_CRL_CNF3_0;
 
 GPIOB->CRL &= ~(GPIO_CRL_CNF3_1);
 
GPIOB->CRL &= ~(GPIO_CRL_MODE3);
 
 
RCC ->APB2ENR |= RCC_APB2ENR_AFIOEN; //FOR ALTERNATE FUNCTION
 
EXTI->IMR |= EXTI_IMR_MR3; //INTERRUPT MASK REGISTER(0:MASKING, 1:NON-MASKING)
 
EXTI->RTSR |= EXTI_RTSR_TR3; // RISING EDGE TRIGGERED INTERRUPT
 
AFIO->EXTICR[0] = AFIO_EXTICR1_EXTI3_PB; //ALTERNATE FUNCTION CONFIGURATION FOR-PB3) 
 
 
 
NVIC_EnableIRQ(EXTI3_IRQn);
 
NVIC_SetPriority(EXTI3_IRQn,0);
 
 
while(1)
 
{
 
 
for(volatile int i=0; i<=10000000; i++);
GPIOA-> BSRR |= GPIO_BSRR_BR1;
 
 
}
 
}

EGeno.1
Associate
June 22, 2020

I'm using STM32F10x_StdPeriph_Lib_V3.5.0 and the programme becomes more readable.

/**

 * @brief Configure the EXTI 4.

 *        The external signal is connected to EXTI4 pin (PB.4)

 *        The Rising edge is used as active edge

 * @param None

 * @retval None

 */

void EXTI4_Configuration() {

 EXTI_InitTypeDef EXTI_InitStructure;

 //select EXTI line4

 EXTI_InitStructure.EXTI_Line = EXTI_Line4;

 //select interrupt mode

 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;

 //generate interrupt on rising edge

 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;

 //enable EXTI line

 EXTI_InitStructure.EXTI_LineCmd = ENABLE;

 //send values to registers

 EXTI_Init(&EXTI_InitStructure);

}

/**

 * @brief This function calculates left motor speed.

 * @param None

 * @retval None

 */

void EXTI4_IRQHandler(void) { 

short ms;  

__IO short h;

   //Check if EXTI_Line4 is asserted

   if(EXTI_GetITStatus(EXTI_Line4) == SET) {

     //we need to clear line pending bit manually

     EXTI_ClearITPendingBit(EXTI_Line4);

....

}

In main:

 GPIO_InitTypeDef GPIO_InitStructure;

 GPIO_StructInit (& GPIO_InitStructure);

 // EXTI4 configuration

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

 GPIO_Init(GPIOB, &GPIO_InitStructure);

 GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource4);  

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);

 // Enable the EXTI4 Interrupt

 NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;

 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 7;

 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

 NVIC_Init(&NVIC_InitStructure);