AnsweredAssumed Answered

External Interrupt

Question asked by saeed taherianfard on Oct 16, 2017
Latest reply on Oct 16, 2017 by Clive One

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 PD12.

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);
     }
}

Outcomes