cancel
Showing results for 
Search instead for 
Did you mean: 

External Interrupt

saeed taherianfard
Associate II
Posted on October 16, 2017 at 10:20

Hi

I'm new to ARM and i'm using stm32f103vet. I can work with GPIOs and turn LED on/off!

I have problem with external interrupts.I want to use PC13 to turn on LED.

PC13 is connected to a button and the LED is connected to PD

Thanks in advance

here is my code:

#include 'stm32f10x.h'
uint8_t ext_flag = 0;
void led_init(){
 GPIO_InitTypeDef GPIO_InitStructure;
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; // | GPIO_Pin_6;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 GPIO_Init(GPIOD,&GPIO_InitStructure);
 GPIOD -> ODR ^= GPIO_Pin_12;
}
void ext_init(){
 GPIO_InitTypeDef GPIO_InitStructure;
 EXTI_InitTypeDef EXTI_InitStructure;
 NVIC_InitTypeDef NVIC_InitStructure;
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
 GPIO_Init(GPIOC,&GPIO_InitStructure);
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
 GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource13);
 EXTI_InitStructure.EXTI_Line = EXTI_Line13;
 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
 EXTI_InitStructure.EXTI_LineCmd = ENABLE;
 EXTI_Init(&EXTI_InitStructure);
 NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0X0F;
 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0X0F;
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 NVIC_Init(&NVIC_InitStructure);
 NVIC_EnableIRQ(EXTI15_10_IRQn);
}
void delay(uint32_t delay_count){
 while (delay_count) delay_count--;
}
void EXT15_10_IRQHandler(){
 if(EXTI_GetITStatus(EXTI_Line13) != RESET)
 {
 ext_flag = 1;
 EXTI_ClearITPendingBit(EXTI_Line13);
 }
}
void main(){
 led_init();
 ext_init();
 while(1){
 if(ext_flag == 1)
 {
 GPIOD -> ODR ^= GPIO_Pin_12;
 ext_flag = 0;
 }
 delay(500000);
 }
}
�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

1 REPLY 1
Posted on October 16, 2017 at 10:50

If the button is to ground you might what to use the input pull up rather than pull down mode. 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..