6 years, 3 months ago.

How to write a code to use External Interrupts in STM32F103 MCU

I'm new to this, and I want to write a code to read External interrupt and glow an LED. I'm using STM32F103 Discovery & Nucleo boards. And generating the code using STMCubeMX and compiling it using Keil IDE. Please help me with a sample code to kick start it.

1 Answer

6 years, 3 months ago.

Hi Naresh, You could use the InterruptIn interface for the buttons on your board.

I provided you with a draft that you can use to fine tune further.

#include <mbed.h>
/**
* See mbed-os/targets/TARGET_STM/TARGET_STM32F1/TARGET_NUCLEO_F103RB/PinNames.h 
* For Target Sepcific Pins.
*/

DigitalOut led1(LED1);
DigitalOut led2(LED2);
InterruptIn button(USER_BUTTON);

/* Toggle led1 */
void toggle_led(void)
{
	led1 = !led1;
}

int main()
{
	/* Register a rising edge interrupt handler*/
	button.rise(&toggle_led);

	/* Sit and wait while we await the interrupt.
	 Toggle led2 while doing this, upon the button press,
         execute the handler */

	while(1) {
		led2 = !led2;
		/* 1 sec wait */
		wait(1);
	};
}

Please let us know if you have any further questions.

Thanks,

Naveen, team mbed

Thanks for the reply Naveen. But, where is the External interrupt handler / ISR? how to define it?

posted by naresh dhommata 13 Sep 2018

Hi Naresh, Your InterruptIn object's rise() lets you specify the handler that get's called on the rising edge.

posted by Naveen Kaje 13 Sep 2018