Created
June 16, 2015 15:24
-
-
Save rheum/881d60fecad58089ef0b to your computer and use it in GitHub Desktop.
STM32F051 External Interrupt Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stm32f0xx_conf.h" | |
/* | |
External interrupt example for STM32F051 | |
Turn on a LED when rising edge is detected. | |
- LED is on GPIOC Pin8 | |
- Button is on GPIOA Pin0 | |
-> This is interrupt Line 0 | |
-> Interrupt Line0 is handled in NVIC | |
together with Line1 on Channel0_1 | |
*/ | |
void EXTI0_1_IRQHandler(void) | |
{ | |
if(EXTI_GetITStatus(EXTI_Line0) != RESET){ | |
EXTI_ClearITPendingBit(EXTI_Line0); | |
GPIO_SetBits(GPIOC, GPIO_Pin_8); | |
} | |
} | |
int main(void) | |
{ | |
GPIO_InitTypeDef gpio_conf; | |
// Configure LED on GPIOC Pin8 | |
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); | |
gpio_conf.GPIO_Pin = GPIO_Pin_8; | |
gpio_conf.GPIO_Mode = GPIO_Mode_OUT; | |
gpio_conf.GPIO_OType = GPIO_OType_PP; | |
gpio_conf.GPIO_PuPd = GPIO_PuPd_NOPULL; | |
gpio_conf.GPIO_Speed = GPIO_Speed_Level_1; | |
GPIO_Init(GPIOC, &gpio_conf); | |
// Configure Button on GPIOA Pin0 | |
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); | |
gpio_conf.GPIO_Pin = GPIO_Pin_0; | |
gpio_conf.GPIO_Mode = GPIO_Mode_IN; | |
gpio_conf.GPIO_PuPd = GPIO_PuPd_NOPULL; | |
GPIO_Init(GPIOA, &gpio_conf); | |
// Configure Interrupt | |
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); | |
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0); | |
EXTI_InitTypeDef EXTI_InitStructure; | |
EXTI_StructInit(&EXTI_InitStructure); | |
EXTI_InitStructure.EXTI_Line = EXTI_Line0; | |
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; | |
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; | |
EXTI_InitStructure.EXTI_LineCmd = ENABLE; | |
EXTI_Init(&EXTI_InitStructure); | |
// Enable Interrupt in NVIC | |
NVIC_InitTypeDef NVIC_InitStructure; | |
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn; | |
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x03; | |
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; | |
NVIC_Init(&NVIC_InitStructure); | |
// Ensure LED is turned off | |
GPIO_ResetBits(GPIOC, GPIO_Pin_8); | |
while(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment