jeudi 12 mars 2015

STM32F4: External interrupt EXTI and LEDs

This  is a simple program to configure an external interrupt exactly the blue boutton in the STM32 card that is linked to the PA0 pin and controlling the functionality of the LEDs.
main program: turn on the LEDs PD12, PD13.
interrupt routine: turn on the LEDs PD14, PD15.



The next picture shows you the interrupt scheme: we will use the EXTI0



/**
  ******************************************************************************
  * @prg     STM32F4
  * @file    External interrupt :Button A0
  * @author  SABRI OUESALTI
  * @site    http://electronicsabriart.blogspot.com/
  *****************************************************************************/  

#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"


void button_A0 (void) {
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE) ;
   GPIO_DeInit(GPIOA) ;
   GPIO_InitTypeDef  GPIO_Initconfiguration ;
   GPIO_Initconfiguration.GPIO_Mode = GPIO_Mode_IN ;
   GPIO_Initconfiguration.GPIO_OType =GPIO_OType_PP ;
   GPIO_Initconfiguration.GPIO_Speed =GPIO_Speed_2MHz ;
   GPIO_Initconfiguration.GPIO_PuPd =GPIO_PuPd_NOPULL ;
   GPIO_Initconfiguration.GPIO_Pin = GPIO_Pin_0 ;
   GPIO_Init(GPIOA ,&GPIO_Initconfiguration) ;
}

void EXTI_init(void) {
   SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource0);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE) ; //clock of interruption//
   EXTI_InitTypeDef EXTI_InitStructure ;
  
   EXTI_InitStructure.EXTI_Line =EXTI_Line0 ;
   EXTI_InitStructure.EXTI_Mode =EXTI_Mode_Interrupt ;
   EXTI_InitStructure.EXTI_Trigger =EXTI_Trigger_Rising_Falling ;
   EXTI_InitStructure.EXTI_LineCmd = ENABLE ;
   EXTI_Init(&EXTI_InitStructure) ; }

void NVIC_init ( void) {
   NVIC_InitTypeDef NVIC_InitStructure ;
   NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn ;
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01 ;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x01 ;
   NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE ;
   NVIC_Init(&NVIC_InitStructure) ;
}



void led_init(void) {
  
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE) ;
  GPIO_DeInit(GPIOD) ;
  GPIO_InitTypeDef  GPIO_Initconfiguration ;
  
   GPIO_Initconfiguration.GPIO_Mode = GPIO_Mode_OUT ;
   GPIO_Initconfiguration.GPIO_OType =GPIO_OType_PP ;
   GPIO_Initconfiguration.GPIO_Speed =GPIO_Speed_2MHz ;
   GPIO_Initconfiguration.GPIO_PuPd =GPIO_PuPd_NOPULL ;
   GPIO_Initconfiguration.GPIO_Pin =          GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15 ;
   GPIO_Init(GPIOD ,&GPIO_Initconfiguration) ;
}

int main(void)
{

led_init() ;
button_A0() ;
EXTI_init() ;
NVIC_init() ;
  while (1)
  {
    GPIO_SetBits(GPIOD,GPIO_Pin_12) ;  
    GPIO_SetBits(GPIOD,GPIO_Pin_13) ;  
    GPIO_ResetBits(GPIOD,GPIO_Pin_14) ;
    GPIO_ResetBits(GPIOD,GPIO_Pin_15) ;

  }
}


======================================================
in the interrupt program stm32f4xx_it. you must write the following function
======================================================

#include "stm32f4xx_it.h"
void EXTI0_IRQHandler(void) {
  
//pending that interrupt flag is set//
  if(EXTI_GetITStatus(EXTI_Line0) != RESET){
  GPIO_SetBits(GPIOD,GPIO_Pin_14) ;
  GPIO_SetBits(GPIOD,GPIO_Pin_15) ;
  GPIO_ResetBits(GPIOD,GPIO_Pin_12) ;
  GPIO_ResetBits(GPIOD,GPIO_Pin_13) ;
  Delay1(8000000);
  } 
    EXTI_ClearITPendingBit(EXTI_Line0) ;  //clear interrupt flag//
}


Aucun commentaire:

Enregistrer un commentaire