# Make a Interrupt on a stm32f4xx

I'm new to Cortex M4 and programming on a microcontroller. I'm still learning the basics.

I'm using this board (stm32f411re), here is the reference manual.

I am trying to make an interrupt on the press of a button (the user button which is connected to the "PC13" port (pin 2)). First, I'm trying to initialize the button so when I click it, it will fire one exception.

Here is my code :

void button_init(void)
{
<======== PART THAT I'M TRYING TO UNDERSTAND ========>

// Enable GPIOC clock because BUTTON is connected to GPIOC
RCC->AHB1ENR |= 0x04; // (page 114)

// Configure the mode
GPIOC->MODER &= ~0x3; // GPIO port mode register (page 153)
GPIOC->PUPDR  &= ~0x3; // GPIO port pull-up/pull-down register (page 156)

<======== PART THAT I'M TRYING TO UNDERSTAND ========>

//System configuration controller clock enable (page 118)
RCC->APB2ENR |= 0x00004000;

SYSCFG->EXTICR[3] |= (0x02 << 4); // (page 139)

//configure the interrupt for exti 13 line
// EXTI->IMR |= (0x01 << 13)// enable interrupt on EXTI 13
EXTI->IMR |= 0x01;
// EXTI->RTSR |= 0X01;
// EXTI->FTSR |= (0X01 << 13) // sensitive to falling edge
EXTI->FTSR |= 0X01;

//nvic configuration
NVIC->IP[EXTI15_10_IRQn] = 0x00;
NVIC_EnableIRQ(EXTI15_10_IRQn); // EXTI15_10_IRQn must be 40.. please confirm
}

• Here are my questions :

Am I right with this calculation ~0x3 = ~(0000 0011) = 1111 1100 ?

So when I do GPIOC->MODER &= ~0x3;, am I setting the first to bits of this register to 00 (input mode)?

Should I set other two bits of this register ? How can I know wich two bits of this register are linked with my button ?

Same questions with GPIOC->PUPDR &= ~0x3;

Finally, I want to enable a clock so I do RCC->APB2ENR |= 0x00004000; : 0x00004000 = 0100 0000 0000 0000 => I set the 16th bit of this register which is "TIM9 clock enable". Is that what we want to do ? Why TIM9 and not TIM10 or TIM11 ?

Thank you very much!

• You should look into the STM Cube software that helps you skip a lot of this. Jun 1 '16 at 10:02
• There are waaaaay too many magic numbers in your Code. ST provides header files so you can access all those bits by their names. Jun 1 '16 at 10:17
• @TurboJ I wanted to do everything by hand to make sure I understand everything. Because I really don't like using things that I don't understand. But of course, when I'll get the concept, I'll try to use more aliases. Thank you for the advise! Jun 1 '16 at 10:31

Am I right with this calculation ~0x3 = ~(0000 0011) = 1111 1100 ?

Almost, stm32 is a 32 bit processor so it will be: 1111 1111 1111 1111 1111 1111 1111 1100

So when I do GPIOC->MODER &= ~0x3;, am I setting the first to bits of this register to 00 (input mode)?

For the description of each register you have to look hat this Reference Manual. For the MODER register of PORTC check page 153. It shows that each 2 bit group of the register sets the properties of one specific pin on that port. '00' is for input as stated on the next page. Your write of GPIOC->MODER &= ~0x3; will result in PC0 being set to input. What you want is GPIOC->MODER &= ~(0x3 << 26); This will set bit 26 and 27 to 0 which relates to pin 13.

Should I set other two bits of this register ?

For just this one pin you don't have to write more to this specific register.

How can I know which two bits of this register are linked with my button ?

On Page 154 it states

Bits 2y:2y+1 MODERy[1:0]: Port x configuration bits (y = 0..15)

this give you the relation of pin to bits in the register

[0:1] for pin 0, [2:3] for pin 1, ... [30,31] for pin 15

Note: With pin I mean the pin of a port, not the actual physical pin on the chip.

Why TIM9 and not TIM10 or TIM11 ?

TIM 9 10 and 11 are all identical. You chose which one you want. STM32s usually have 3 different type of timers (TIM1, TIM2-5, TIM9-11) which have slightly different features. You have to know what you need and which one supports that.

Finally, I want to enable a clock so I do RCC->APB2ENR |= 0x00004000; : 0x00004000 = 0100 0000 0000 0000 => I set the 16th bit of this register which is "TIM9 clock enable". Is that what we want to do ?

Yes this is part of setting up the timer. This will enable the clock for the timer peripheral so it is able to run at all. Although bit 16 is 0x10000 in hex or 0001 0000 0000 0000 0000 in binary. The timer will still not run at this moment. You have to do more setup. How you setup the timer is described on page 366 and following.