Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 1 month 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
7 years, 1 month 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