# stm32 external interrupt

I am working with a stm32f103 and am trying to get the external interrupt done. My code is:

void delay(unsigned int counts);

//---------------------------------------------------------------------------------------
int main(int argc, char* argv[])
{

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

GPIO_Led.GPIO_Pin = LED_PIN;
GPIO_Led.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Led.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LED_PORT, &GPIO_Led);
LED_OFF;

delay(500);

LED_ON;

// Enable Clock for AFIOEN
RCC->APB2ENR |= RCC_APB2ENR_AFIOEN;
// Enable Clock for IOPBEN
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;

// Set PB1 as input with pullup
GPIOB->CRL &= ~((1 << 4) | (1 << 5));
GPIOB->CRL |= (1 << 7);
GPIOB->CRL &= ~(1 << 6);
GPIOB->ODR |= (1 << 1);

// Source input for EXTI1 is PB1
AFIO->EXTICR[0] |= AFIO_EXTICR1_EXTI1_PB;

// Interrupt request from Line 1 is not masked => enabled
EXTI->IMR |= (1 << 1);
// Rising edge
EXTI->RTSR |= (1 << 1);

NVIC_InitTypeDef NVIC_InitStruct;
// Add IRQ vector to NVIC
// PB1 is connected to EXTI_Line1, which has EXTI1_IRQn vector
NVIC_InitStruct.NVIC_IRQChannel = EXTI1_IRQn;
// Set priority
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x0f;
// Set sub priority
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x0f;
// Enable interrupt
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);

delay(500);

LED_OFF;

// Infinite loop
while (1)
{
LED_TOGGLE;
delay(500);
}
}

//---------------------------------------------------------------------------------------
void delay(unsigned int counts)
{
volatile unsigned int i = 0, j = 0;

while (i < counts)
{
j = 0;
while (j < 0x1AFF)
{
j++;
}
i++;
}
}

//---------------------------------------------------------------------------------------
void EXTI1_IRQHandler()
{
/* Make sure that interrupt flag is set */
if (EXTI_GetITStatus(EXTI_Line1) != RESET)
{
LED_TOGGLE;
/* Clear interrupt flag */
EXTI_ClearITPendingBit(EXTI_Line1);
}
}


That is the main part of my code. LED_ON, LED_OFF and LED_TOGGLE are only some defines. They work fine.

The problem is: If there is a rising edge at pin PB1, the stm32 does not jump to the IRQ subroutine. So the stm32 got stuck.

Does anyone know what is the problem? I don't have any ideas.

• "not jump to the IRQ subroutine. So the stm32 got stuck." I'm new to programming, but maybe there is a problem in vector table. – Long Pham Aug 16 '18 at 17:21
• I'd review this tutorial and make sure you're not missing any EXTI configurations: stm32f4-discovery.net/2014/08/… – Catsunami Aug 16 '18 at 18:39
• @LongPham what do you mean with problem in the vector table? – Lukas_M94 Aug 16 '18 at 21:55
• @LongPham First i thought that the problem is something like an undefault jump, but I do not really know And yes, this is my next step I will do, to buy a debugger :-D – Lukas_M94 Aug 17 '18 at 6:26
• @LongPham, I never suggested that this is what he should do instead of finding the cause of the problem. I meant that he can confirm that it's in the default handler since he obviously doesn't have a debugger to do it the easy way. Toggling an LED within the default handler would confirm that the handler should be looked at. – Catsunami Aug 17 '18 at 16:30