娇小w搡bbbb搡bbb,《第一次の人妻》,中国成熟妇女毛茸茸,边啃奶头边躁狠狠躁视频免费观看

歷史上的今天

今天是:2024年12月28日(星期六)

2018年12月28日 | STM32外部中斷方式按鍵操作(STM32_09)

發布者:月光男孩 來源: eefocus關鍵字:STM32  外部中斷方式  按鍵操作 手機看文章 掃描二維碼
隨時隨地手機看文章

一、中斷配置的步驟


1、使能中斷


2、設置中斷優先級分組


void NVIC_PriorityGroupConfig(uint32_tNVIC_PriorityGroup);


#define NVIC_PriorityGroup_0         ((uint32_t)0x700) /*0位搶占,4位響應 */


#define NVIC_PriorityGroup_1         ((uint32_t)0x600) /*1位搶占,3位響應 */


#define NVIC_PriorityGroup_2         ((uint32_t)0x500) /*2位搶占,2位響應 */


#define NVIC_PriorityGroup_3         ((uint32_t)0x400) /*3位搶占,1位響應 */


#define NVIC_PriorityGroup_4         ((uint32_t)0x300) /*4位搶占,0位響應 */


    一般選擇NVIC_PriorityGroup_2,即搶占和響應均有4級。


3、配置NVIC_InitTypeDef結構體


void NVIC_Init(NVIC_InitTypeDef*NVIC_InitStruct);


typedef struct


{


  uint8_t NVIC_IRQChannel;   /*指定要使能或要失能的IRQ通道,這個值可以從stm32f10x.h中引用*/


  uint8_t NVIC_IRQChannelPreemptionPriority;  /*指定搶占優先級,0-15 */


  uint8_t NVIC_IRQChannelSubPriority;        /*指定響應優先級,0-15 */


  FunctionalState NVIC_IRQChannelCmd;     /*使能或失能 ENABLE / DISABLE */  


} NVIC_InitTypeDef;


NVIC_IRQChannelPreemptionPriority和NVIC_IRQChannelSubPriority的取值范圍與調用中斷優先級分組函數NVIC_PriorityGroupConfig時的配置相關,如果分組選擇的是NVIC_PriorityGroup_2,則這兩個的取值均為0-3。


stm32f10x.h中定義的中斷通道值(HD):


typedef enum IRQn


{


/******  Cortex-M3 Processor Exceptions Numbers ************/


  NonMaskableInt_IRQn         = -14,    /*!< 2 Non Maskable Interrupt                         */


  MemoryManagement_IRQn       = -12,    /*!< 4 Cortex-M3 Memory Management Interrupt        */


  BusFault_IRQn               = -11,    /*!< 5 Cortex-M3 Bus Fault Interrupt                    */


  UsageFault_IRQn             = -10,    /*!< 6 Cortex-M3 Usage Fault Interrupt                  */


  SVCall_IRQn                 = -5,     /*!< 11 Cortex-M3 SV Call Interrupt                    */


  DebugMonitor_IRQn           = -4,     /*!< 12 Cortex-M3 Debug Monitor Interrupt              */


  PendSV_IRQn                 = -2,     /*!< 14 Cortex-M3 Pend SV Interrupt                   */


  SysTick_IRQn                = -1,     /*!< 15 Cortex-M3 System Tick Interrupt                 */


 


/******  STM32 specific Interrupt Numbers ***********************************/


  WWDG_IRQn                   = 0,      /*!< Window WatchDog Interrupt                   */


  PVD_IRQn                    = 1,      /*!< PVD through EXTI Line detection Interrupt         */


  TAMPER_IRQn                 = 2,      /*!< Tamper Interrupt                             */


  RTC_IRQn                    = 3,      /*!< RTC global Interrupt                            */


  FLASH_IRQn                  = 4,      /*!< FLASH global Interrupt                        */


  RCC_IRQn                    = 5,      /*!< RCC global Interrupt                           */


  EXTI0_IRQn                  = 6,      /*!< EXTI Line0 Interrupt                           */


  EXTI1_IRQn                  = 7,      /*!< EXTI Line1 Interrupt                           */


  EXTI2_IRQn                  = 8,      /*!< EXTI Line2 Interrupt                           */


  EXTI3_IRQn                  = 9,      /*!< EXTI Line3 Interrupt                           */


  EXTI4_IRQn                  = 10,     /*!< EXTI Line4 Interrupt                           */


  DMA1_Channel1_IRQn          = 11,     /*!< DMA1 Channel 1 global Interrupt                */


  DMA1_Channel2_IRQn          = 12,     /*!< DMA1 Channel 2 global Interrupt                 */


  DMA1_Channel3_IRQn          = 13,     /*!< DMA1 Channel 3 global Interrupt                 */


  DMA1_Channel4_IRQn          = 14,     /*!< DMA1 Channel 4 global Interrupt                 */


  DMA1_Channel5_IRQn          = 15,     /*!< DMA1 Channel 5 global Interrupt                 */


  DMA1_Channel6_IRQn          = 16,     /*!< DMA1 Channel 6 global Interrupt                 */


  DMA1_Channel7_IRQn          = 17,     /*!< DMA1 Channel 7 global Interrupt                 */


#ifdef STM32F10X_HD


  ADC1_2_IRQn             = 18,   /* ADC1 and ADC2 global Interrupt                  */


  USB_HP_CAN1_TX_IRQn   = 19, /* USB Device High Priority or CAN1 TX Interrupts       */


  USB_LP_CAN1_RX0_IRQn  = 20,     /* USB Device Low Priority or CAN1 RX0 Interrupts  */


  CAN1_RX1_IRQn          = 21, /* CAN1 RX1 Interrupt                               */


  CAN1_SCE_IRQn          = 22,/* CAN1 SCE Interrupt                                */


  EXTI9_5_IRQn             = 23,     /* External Line[9:5] Interrupts                    */


  TIM1_BRK_IRQn          = 24,     /* TIM1 Break Interrupt                           */


  TIM1_UP_IRQn            = 25,     /* TIM1 Update Interrupt                         */


  TIM1_TRG_COM_IRQn     = 26,     /* TIM1 Trigger and Commutation Interrupt          */


  TIM1_CC_IRQn            = 27,     /* TIM1 Capture Compare Interrupt                 */


  TIM2_IRQn                = 28,     /* TIM2 global Interrupt                          */


  TIM3_IRQn                = 29,     /* TIM3 global Interrupt                          */


  TIM4_IRQn                = 30,     /* TIM4 global Interrupt                           */


  I2C1_EV_IRQn             = 31,     /* I2C1 Event Interrupt                            */


  I2C1_ER_IRQn             = 32,     /* I2C1 Error Interrupt                             */


  I2C2_EV_IRQn             = 33,     /* I2C2 Event Interrupt                             */


  I2C2_ER_IRQn             = 34,     /* I2C2 Error Interrupt                             */


  SPI1_IRQn                = 35,     /* SPI1 global Interrupt                             */


  SPI2_IRQn                = 36,     /* SPI2 global Interrupt                             */


  USART1_IRQn             = 37,     /* USART1 global Interrupt                         */


  USART2_IRQn             = 38,     /* USART2 global Interrupt                         */


  USART3_IRQn             = 39,     /* USART3 global Interrupt                         */


  EXTI15_10_IRQn           = 40,     /* External Line[15:10] Interrupts                    */


  RTCAlarm_IRQn            = 41,     /* RTC Alarm through EXTI Line Interrupt             */


  USBWakeUp_IRQn          = 42,   /* USB Device WakeUp from suspend through EXTI Line Interrupt */


  TIM8_BRK_IRQn           = 43,     /* TIM8 Break Interrupt                          */


  TIM8_UP_IRQn             = 44,     /* TIM8 Update Interrupt                          */


  TIM8_TRG_COM_IRQn      = 45,     /* TIM8 Trigger and Commutation Interrupt         */


  TIM8_CC_IRQn             = 46,     /* TIM8 Capture Compare Interrupt                  */


  ADC3_IRQn                = 47,     /* ADC3 global Interrupt                          */


  FSMC_IRQn                = 48,     /* FSMC global Interrupt                           */


  SDIO_IRQn                 = 49,     /* SDIO global Interrupt                            */


  TIM5_IRQn                 = 50,     /* TIM5 global Interrupt                            */


  SPI3_IRQn                  = 51,     /* SPI3 global Interrupt                             */


  UART4_IRQn                = 52,     /* UART4 global Interrupt                          */


  UART5_IRQn                = 53,     /* UART5 global Interrupt                          */


  TIM6_IRQn                  = 54,     /* TIM6 global Interrupt                            */


  TIM7_IRQn                  = 55,     /* TIM7 global Interrupt                             */


  DMA2_Channel1_IRQn        = 56,     /* DMA2 Channel 1 global Interrupt                  */


  DMA2_Channel2_IRQn        = 57,     /* DMA2 Channel 2 global Interrupt                  */


  DMA2_Channel3_IRQn        = 58,     /* DMA2 Channel 3 global Interrupt                  */


  DMA2_Channel4_5_IRQn      = 59      /* DMA2 Channel 4 and Channel 5 global Interrupt     */


} IRQn_Type;


 


 


4、編寫中斷服務函數


中斷服務函數有固定的函數名,在startup_stm32f10x_hd.s文件中已經定義:


__Vectors       DCD     __initial_sp               ; Top of Stack


                DCD     Reset_Handler              ; Reset Handler


                DCD     NMI_Handler                ; NMI Handler


                DCD     HardFault_Handler          ; Hard Fault Handler


                DCD     MemManage_Handler          ; MPU Fault Handler


                DCD     BusFault_Handler           ; Bus Fault Handler


                DCD     UsageFault_Handler         ; Usage Fault Handler


                DCD     0                          ; Reserved


                DCD     0                          ; Reserved


                DCD     0                          ; Reserved


                DCD     0                          ; Reserved


                DCD     SVC_Handler                ; SVCall Handler


                DCD     DebugMon_Handler           ; Debug Monitor Handler


                DCD     0                          ; Reserved


                DCD     PendSV_Handler             ; PendSV Handler


                DCD     SysTick_Handler            ; SysTick Handler


 


                ; External Interrupts


                DCD     WWDG_IRQHandler            ; Window Watchdog


                DCD     PVD_IRQHandler             ; PVD through EXTI Line detect


                DCD     TAMPER_IRQHandler          ; Tamper


                DCD     RTC_IRQHandler             ; RTC


                DCD     FLASH_IRQHandler           ; Flash


                DCD     RCC_IRQHandler             ; RCC


                DCD     EXTI0_IRQHandler           ; EXTI Line 0


                DCD     EXTI1_IRQHandler           ; EXTI Line 1


                DCD     EXTI2_IRQHandler           ; EXTI Line 2


                DCD     EXTI3_IRQHandler           ; EXTI Line 3


                DCD     EXTI4_IRQHandler           ; EXTI Line 4


                DCD     DMA1_Channel1_IRQHandler   ; DMA1 Channel 1


                DCD     DMA1_Channel2_IRQHandler   ; DMA1 Channel 2


                DCD     DMA1_Channel3_IRQHandler   ; DMA1 Channel 3


                DCD     DMA1_Channel4_IRQHandler   ; DMA1 Channel 4


                DCD     DMA1_Channel5_IRQHandler   ; DMA1 Channel 5


                DCD     DMA1_Channel6_IRQHandler   ; DMA1 Channel 6


                DCD     DMA1_Channel7_IRQHandler   ; DMA1 Channel 7


                DCD     ADC1_2_IRQHandler          ; ADC1 & ADC2


                DCD     USB_HP_CAN1_TX_IRQHandler  ; USB High Priority or CAN1 TX


                DCD     USB_LP_CAN1_RX0_IRQHandler ; USB Low  Priority or CAN1 RX0


                DCD     CAN1_RX1_IRQHandler        ; CAN1 RX1


                DCD     CAN1_SCE_IRQHandler        ; CAN1 SCE


                DCD     EXTI9_5_IRQHandler         ; EXTI Line 9..5


                DCD     TIM1_BRK_IRQHandler        ; TIM1 Break


                DCD     TIM1_UP_IRQHandler         ; TIM1 Update


                DCD     TIM1_TRG_COM_IRQHandler    ; TIM1 Trigger and Commutation


                DCD     TIM1_CC_IRQHandler         ; TIM1 Capture Compare


                DCD     TIM2_IRQHandler            ; TIM2


                DCD     TIM3_IRQHandler            ; TIM3


                DCD     TIM4_IRQHandler            ; TIM4


                DCD     I2C1_EV_IRQHandler         ; I2C1 Event


                DCD     I2C1_ER_IRQHandler         ; I2C1 Error


                DCD     I2C2_EV_IRQHandler         ; I2C2 Event


                DCD     I2C2_ER_IRQHandler         ; I2C2 Error


                DCD     SPI1_IRQHandler            ; SPI1


                DCD     SPI2_IRQHandler            ; SPI2


                DCD     USART1_IRQHandler          ; USART1


                DCD     USART2_IRQHandler          ; USART2


                DCD     USART3_IRQHandler          ; USART3


                DCD     EXTI15_10_IRQHandler       ; EXTI Line 15..10


                DCD     RTCAlarm_IRQHandler        ; RTC Alarm through EXTI Line


                DCD     USBWakeUp_IRQHandler       ; USB Wakeup from suspend


                DCD     TIM8_BRK_IRQHandler        ; TIM8 Break


                DCD     TIM8_UP_IRQHandler         ; TIM8 Update


                DCD     TIM8_TRG_COM_IRQHandler    ; TIM8 Trigger and Commutation


                DCD     TIM8_CC_IRQHandler         ; TIM8 Capture Compare


                DCD     ADC3_IRQHandler            ; ADC3


                DCD     FSMC_IRQHandler            ; FSMC


                DCD     SDIO_IRQHandler            ; SDIO


                DCD     TIM5_IRQHandler            ; TIM5


                DCD     SPI3_IRQHandler            ; SPI3


                DCD     UART4_IRQHandler           ; UART4


                DCD     UART5_IRQHandler           ; UART5


                DCD     TIM6_IRQHandler            ; TIM6


                DCD     TIM7_IRQHandler            ; TIM7


                DCD     DMA2_Channel1_IRQHandler   ; DMA2 Channel1


                DCD     DMA2_Channel2_IRQHandler   ; DMA2 Channel2


                DCD     DMA2_Channel3_IRQHandler   ; DMA2 Channel3


                DCD     DMA2_Channel4_5_IRQHandler ; DMA2 Channel4 & Channel5


__Vectors_End


二、外部中斷/事件控制器EXTI


互聯網型產品有20個EXTI,其他的有19個,與IO口引腳相連的有16個(EXTI0-EXTI15),還有PVD(EXTI線16),RTCAlarm(EXTI線17),USB喚醒(EXTI線18),以及互聯網型的ETH_WKUP(EXTI線19)。EXTI0~EXTI15可以被映射到GPIOA-GPIOG的引腳上。


EXTI結構框圖

三、外部中斷配置


配置外部中斷使用STM32外設庫函數,涉及"stm32f10x_exti.h"、"stm32f10x_exti.c"、"stm32f10x_gpio.h"和"stm32f10x_gpio.c"四個文件。


在本程序中將開發板上的四個按鍵均配置為外部中斷源,分別是GPIOA_0對應EXTI_Line0,GPIOE_2、GPIOE_3、和GPIOE_4分別對應EXTI_2、EXTI_3和EXTI_4。


1、使能IO口時鐘,配置IO口為輸入模式


2、開啟AFIO時鐘


RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE );


3、設置IO口與中斷線的映射關系


使用GPIO_EXTILineConfig函數("stm32f10x_gpio.h"和"stm32f10x_gpio.c")該函數原型為:void GPIO_EXTILineConfig( uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);


其中GPIO_PortSource取值如下:


#define GPIO_PortSourceGPIOA       ((uint8_t)0x00)


#define GPIO_PortSourceGPIOB       ((uint8_t)0x01)


#define GPIO_PortSourceGPIOC       ((uint8_t)0x02)


#define GPIO_PortSourceGPIOD       ((uint8_t)0x03)


#define GPIO_PortSourceGPIOE       ((uint8_t)0x04)


#define GPIO_PortSourceGPIOF       ((uint8_t)0x05)


#define GPIO_PortSourceGPIOG       ((uint8_t)0x06)


GPIO_PinSource取值如下:


#define GPIO_PinSource0            ((uint8_t)0x00)


#define GPIO_PinSource1            ((uint8_t)0x01)


#define GPIO_PinSource2            ((uint8_t)0x02)


#define GPIO_PinSource3            ((uint8_t)0x03)


#define GPIO_PinSource4            ((uint8_t)0x04)


#define GPIO_PinSource5            ((uint8_t)0x05)


#define GPIO_PinSource6            ((uint8_t)0x06)


#define GPIO_PinSource7            ((uint8_t)0x07)


#define GPIO_PinSource8            ((uint8_t)0x08)


#define GPIO_PinSource9            ((uint8_t)0x09)


#define GPIO_PinSource10           ((uint8_t)0x0A)


#define GPIO_PinSource11           ((uint8_t)0x0B)


#define GPIO_PinSource12           ((uint8_t)0x0C)


#define GPIO_PinSource13           ((uint8_t)0x0D)


#define GPIO_PinSource14           ((uint8_t)0x0E)


#define GPIO_PinSource15           ((uint8_t)0x0F)


這里四個按鍵對應中斷線映射:


GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0 );


GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource2 );


GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource3 );


GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource4 );


4、配置中斷分組(NVIC),使能中斷


voidNVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup);


voidNVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct);


這里如下調用:


NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);


//對GPIO_0,映射到EXTI0


NVIC_InitTypeDef NVIC_Struct;


NVIC_Stuct.NVIC_IRQChannel = EXTI0_IRQn;


NVIC_Stuct.NVIC_IRQChannelPreemptionPriority = 2;


NVIC_Stuct.NVIC_IRQChannelSubPriority = 3;


NVIC_Stuct.NVIC_IRQChannelCmd = ENABLE;


NVIC_Init( &NVIC_Struct );


 


5、初始化EXTI,選擇觸發方式


調用EXTI庫函數EXTI_Init實現,該函數原型為:


voidEXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct);


在"stm32f10x_exti.h"文件中定義了以下結構體和宏:


typedef struct


{


  uint32_t EXTI_Line;                  /*指定要使能或失能的中斷或事件線*/


  EXTIMode_TypeDef EXTI_Mode;       /*指定中斷線的模式*/


  EXTITrigger_TypeDef EXTI_Trigger;     /*設置觸發方式*/


  FunctionalState EXTI_LineCmd;         /*使能或失能 ENABLE or DISABLE */


}EXTI_InitTypeDef;


 


#define EXTI_Line0       ((uint32_t)0x00001)  /* External interrupt line 0 */


#define EXTI_Line1       ((uint32_t)0x00002)  /* External interrupt line 1 */


#define EXTI_Line2       ((uint32_t)0x00004)  /* External interrupt line 2 */


#define EXTI_Line3       ((uint32_t)0x00008)  /* External interrupt line 3 */


#define EXTI_Line4       ((uint32_t)0x00010)  /* External interrupt line 4 */


#define EXTI_Line5       ((uint32_t)0x00020)  /* External interrupt line 5 */


#define EXTI_Line6       ((uint32_t)0x00040)  /* External interrupt line 6 */


#define EXTI_Line7       ((uint32_t)0x00080)  /* External interrupt line 7 */


#define EXTI_Line8       ((uint32_t)0x00100)  /* External interrupt line 8 */


#define EXTI_Line9       ((uint32_t)0x00200)  /* External interrupt line 9 */


#define EXTI_Line10      ((uint32_t)0x00400)  /* External interrupt line 10 */


#define EXTI_Line11      ((uint32_t)0x00800)  /* External interrupt line 11 */


#define EXTI_Line12      ((uint32_t)0x01000)  /* External interrupt line 12 */


#define EXTI_Line13      ((uint32_t)0x02000)  /* External interrupt line 13 */


#define EXTI_Line14      ((uint32_t)0x04000)  /* External interrupt line 14 */


#define EXTI_Line15      ((uint32_t)0x08000)  /* External interrupt line 15 */


#define EXTI_Line16      ((uint32_t)0x10000)  /* External interrupt line 16 Connected to the PVD Output */


#define EXTI_Line17      ((uint32_t)0x20000)  /* External interrupt line 17 Connected to the RTC


Alarm event */


#define EXTI_Line18      ((uint32_t)0x40000)  /* External interrupt line 18 Connected to the


USB Device/USB OTG FS    Wakeup from suspend event */


#define EXTI_Line19      ((uint32_t)0x80000)  /* External interrupt line 19 Connected to the


Ethernet Wakeup event */


 


typedef enum


{


  EXTI_Mode_Interrupt = 0x00,   //設置EXTI線路為中斷請求


  EXTI_Mode_Event = 0x04      //設置EXTI線路為事件請求


}EXTIMode_TypeDef;


 


typedef enum


{


  EXTI_Trigger_Rising = 0x08,        //設置輸入線路上升沿為中斷請求


  EXTI_Trigger_Falling = 0x0C,       //設置輸入線路下降沿為中斷請求


  EXTI_Trigger_Rising_Falling = 0x10  //設置輸入線路上升沿和下降沿為中斷請求


}EXTITrigger_TypeDef;


這里分別對0,2,3,4四個外部中斷進行設置:


對K_UP鍵中斷設置,根據電路連接,因為該鍵設置為下拉方式,在沒有按下時為低電平,按下時為高電平,所以應設為上升沿觸發。


EXTI_InitTypeDef EXTI_Struct;


EXTI_Struct.EXTI_Line = EXTI_Line0;


EXTI_Struct.EXTI_Mode = EXTI_Mode_Interrupt;


EXTI_Struct.EXTI_Trigger = EXTI_Trigger_Rising;


EXTI_Struct.EXTI_LineCmd = ENABLE;


EXTI_Init( &EXTI_Struct );


另外3個類似操作。


 


6、設置中斷處理函數


中斷服務函數有固定的函數名,在startup_stm32f10x_hd.s文件中已經定義,這里的四個中斷的函數名分別為:EXTI0_IRQHandler、EXTI2_IRQHandler、EXTI3_IRQHandler和EXTI4_IRQHandler。


void EXTI0_IRQHandler( void )


{


         if( EXTI_GetITStatus( EXTI_Line0 ) ==1)


         {


                  delay_ms(10);


                  if(K_UP==1)


                  {


                            Sound(262);


                  }


         }


         EXTI_ClearITPendingBit( EXTI_Line0 );


}


四、項目實現


1、本項目以使用STM32GPIO讀取按鍵實現按鍵操作為基礎(代碼下載地址)。


2、由于本項目需要用到EXTI相關庫函數,所以將"stm32f10x_exti.h"、"stm32f10x_exti.c"兩個文件分別復制到項目文件夾的"Lib/inc"和"Lib/src"子文件夾下,并將"stm32f10x_exti.c"文件加入到項目的"Lib "組中。


3、在項目文件夾下的"User"子文件夾下新建"Exti"子文件夾,在Keil5中新建"Exti.h"和"Exti.c"文件,并將"Exti.c"文件加入到項目的"User "組中,然后修改"Include Paths",添加".\User\Exti"路徑,以便項目中其他程序能夠引用"Exti.h"。


4、在"Exti.h"文件中添加相應的宏定義和函數聲明,內容如下:


#ifndef __EXTI__H


#define __EXTI__H


#include "system.h"


#include "stm32f10x_exti.h"


void KEY_EXTI_Init(void);


#endif


5、在"Exti.c"文件中實現KEY_EXTI_Init函數和四個中斷函數。


#include "exti.h"


#include "key.h"


#include "stm32f10x_rcc.h"


#include "misc.h"


#include "beep.h"


#include "systick.h"


void KEY_EXTI_Init(void)


{


         NVIC_InitTypeDef NVIC_Struct;


         EXTI_InitTypeDef  EXTI_Struct;


         //1、使能IO口時鐘,配置IO口為輸入模式


         Key_Init(); //調用key.c中的初始化按鍵GPIO的函數


         //2、開啟AFIO時鐘


         RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE );


         //3、設置IO口與中斷線的映射關系


         GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0 );


         GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource2 );


         GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource3 );


         GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource4 );


         //4、配置中斷分組(NVIC),使能中斷


         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2 );


        


         NVIC_Struct.NVIC_IRQChannel = EXTI0_IRQn;


         NVIC_Struct.NVIC_IRQChannelPreemptionPriority = 2;


         NVIC_Struct.NVIC_IRQChannelSubPriority = 3;


         NVIC_Struct.NVIC_IRQChannelCmd = ENABLE;


         NVIC_Init( &NVIC_Struct );


 


         NVIC_Struct.NVIC_IRQChannel = EXTI2_IRQn;


         NVIC_Struct.NVIC_IRQChannelPreemptionPriority = 2;


         NVIC_Struct.NVIC_IRQChannelSubPriority = 3;


         NVIC_Struct.NVIC_IRQChannelCmd = ENABLE;


         NVIC_Init( &NVIC_Struct );


 


         NVIC_Struct.NVIC_IRQChannel = EXTI3_IRQn;


         NVIC_Struct.NVIC_IRQChannelPreemptionPriority = 2;


         NVIC_Struct.NVIC_IRQChannelSubPriority = 3;


         NVIC_Struct.NVIC_IRQChannelCmd = ENABLE;


         NVIC_Init( &NVIC_Struct );


 


         NVIC_Struct.NVIC_IRQChannel = EXTI4_IRQn;


         NVIC_Struct.NVIC_IRQChannelPreemptionPriority = 2;


         NVIC_Struct.NVIC_IRQChannelSubPriority = 3;


         NVIC_Struct.NVIC_IRQChannelCmd = ENABLE;


         NVIC_Init( &NVIC_Struct );


         //5、使能EXTI,選擇觸發方式


         //K_UP設為上升沿觸發


         EXTI_Struct.EXTI_Line = EXTI_Line0;


         EXTI_Struct.EXTI_Mode = EXTI_Mode_Interrupt;


         EXTI_Struct.EXTI_Trigger = EXTI_Trigger_Rising;


         EXTI_Struct.EXTI_LineCmd = ENABLE;


         EXTI_Init( &EXTI_Struct );


        


         //K_LEFT,K_DOWN,K_RIGHT設為下降沿觸發


         EXTI_Struct.EXTI_Line = EXTI_Line2|EXTI_Line3|EXTI_Line4;


         EXTI_Struct.EXTI_Mode = EXTI_Mode_Interrupt;


         EXTI_Struct.EXTI_Trigger = EXTI_Trigger_Rising;


         EXTI_Struct.EXTI_LineCmd = ENABLE;


         EXTI_Init( &EXTI_Struct );


}


 


void EXTI0_IRQHandler(void)


{


         u8 i;


         if( EXTI_GetITStatus( EXTI_Line0 ) == 1)


         {


                   delay_ms(10);


                   if(K_UP==1)


                   {


                            for(i=0; i<100;i++)


                                     Sound(262);


                   }


         }


         EXTI_ClearITPendingBit(EXTI_Line0);


}


 


void EXTI2_IRQHandler(void)


{


         u8 i;


         if( EXTI_GetITStatus( EXTI_Line2 ) == 1)


         {


                   delay_ms(10);


                   if(K_LEFT==0)


                   {


                            for(i=0; i<100;i++)


                                     Sound(294);


                   }


         }


         EXTI_ClearITPendingBit(EXTI_Line2);


}


 


void EXTI3_IRQHandler(void)


{


         u8 i;


         if( EXTI_GetITStatus( EXTI_Line3 ) == 1)


         {


                   delay_ms(10);


                   if(K_DOWN==0)


                   {


                            for(i=0; i<100;i++)


                                     Sound(330);


                   }


         }


         EXTI_ClearITPendingBit(EXTI_Line3);


}


 


void EXTI4_IRQHandler(void)


{


         u8 i;


         if( EXTI_GetITStatus( EXTI_Line4 ) == 1)


         {


                   delay_ms(10);


                   if(K_RIGHT==0)


                   {


                            for(i=0; i<100;i++)


                                     Sound(349);


                   }


         }


         EXTI_ClearITPendingBit(EXTI_Line4);


}


 


6、修改"main.c"文件,內容如下:


#include "SysTick.h"


#include "exti.h"


#include "beep.h"


int main()


{


         SysTick_Init(72);


         BEEP_Init();


         KEY_EXTI_Init();


         while(1)


         {


         }


}


7、編譯,下載到開發板


關鍵字:STM32  外部中斷方式  按鍵操作 引用地址: STM32外部中斷方式按鍵操作(STM32_09)

上一篇:STM32的中斷優先級和庫函數的 開、關總中斷
下一篇:STM32IAP程序和APP相互跳轉卡死問題

推薦閱讀

高通(QCOM)公司經歷了多年的增長停滯。股價長期保持在區間波動,因為短期樂觀情緒爆發最后總是以失望告終。   高通的頹勢可能很快就要結束了。高通將在未來將5G推向市場方面發揮關鍵作用。因此,分析師已經開始對高通營收和利潤增長進行預測。   分析師們預計,隨著5G的不斷推動,高通財務狀況料將得到改善,高通股價有望回歸持續增長。   2010年...
能源領域的變革對一個國家經濟和社會發展的具有重要意義,隨著新一代信息通信技術對能源領域持續滲透,各國紛紛能源互聯網作為實現能源轉型的突破口。我國作為世界最大的能源生產和消費國,也高度重視能源互聯網的發展,不斷推動能源互聯網新型應用,促進能源產業轉型升級。但目前,我國能源互聯網仍存在基礎設施薄弱、技術裝備不足、標準匱乏、應用有限...
本周華為剛剛發布了全新升級的智能家居戰略和全屋智能ALL IN ONE解決方案,魅族又宣布智能家居品牌Lipro,還將于1月5日發布智能家居新品,手機廠商紛紛轉移競爭賽道,入局智能家居。前布局AIoT多年的小米,后推出全屋智能ALL IN ONE解決方案華為,魅族的智能家居品牌Lipro還有機會嗎?智能家居概念提出多年,為何一直沒有真正發展起來?從2014年提出這...
中國儲能網訊:“加快數字化改造,促進傳統產業升級。”日前召開的中央經濟工作會議對明年的經濟工作進行了全面部署,明確要求堅持穩中求進工作總基調,并指出要加快數字化改造。這不僅充分凸顯了數字化轉型作為經濟社會驅動“引擎”的重要作用,更為全行業在新發展階段貫徹新發展理念、構建新發展格局、推動高質量發展提供了根本遵循。 當前,世界經濟...

史海拾趣

問答坊 | AI 解惑

市面上現在熱銷的P10_16X32LED點陣原理圖,獨家放送!

市面上現在熱銷的P10_16X32LED點陣原理圖,獨家放送!由于廠家保密不給原理圖和源程序,為了方便自己和大家手工制作,熬夜繪制的詳細原理圖,麻煩大蝦編個顯示漢字的程序給我, 不勝感激! 放圖:…

查看全部問答∨

功率因素與視在功率與無用功功率區別

無論是LED、螢光燈還是白熾燈具,我們都要了解其中的一個重要參數——功率因素(PFC),下我們來了解下什么是功率因素。1、功率因素功率因數表征著燈具輸出有功功率的能力。功率是能量的傳輸率的度量,在直流電路中它是電壓V和電流A和乘積。在交流 ...…

查看全部問答∨

新手求wifi開發的步驟

我是新手剛剛開始實習,要學著用SPI協議,開發wifi。 其實代碼是別人已經寫好的,可惜我沒學過任何有關嵌入式開發的東西(之前都是學桌面應用程序開發的) 在WM系統下的,AKU是6.1的。 希望前輩們指點下wifi開發過程中的一些流程和代碼編寫的經驗 ...…

查看全部問答∨

Wince與Oracle問題,大家一起搞定他

wince 連接PC Oracle都能通過什魔方式?如何做?這問題我看很多人都問過,大家把想法意見都列出來,徹底解決他.^_^…

查看全部問答∨

基于STM32的最新論文

傳三篇基于STM32應用的最新的博士論文 基于STM32處理器和PC主機的USB通信協議的實現.pdf (3.05 MB) 下載次數:271 2010-12-23 19:22 2009 山東大學 博士論文 基于STM32處理器和PC主機的USB通信協議的實現 ...…

查看全部問答∨

【設計工具】說明ISE+timing+constrain+的用法

  wConstraints Guide: Quick Start Guide appendix H. wUsing Timing Constraints: Developmental System Reference Guide chapter 6. wTiming Analyzer: Timing Analyzer Reference/User Guide. wTRCE: Developmental System Refer ...…

查看全部問答∨

MSP430不能下載成功

顯示的錯誤信息,跟不接學習板,采用硬件仿真的提示錯誤信息一樣。 為: fatal error unknown exception in driver(#E1) session aborted!…

查看全部問答∨

關于移植后編寫數據采集任務的問題

各位:       現有一個問題問一下大家,我在28335上移植了ucos2,現在想用它采集數據,但ucos的時鐘比較慢,無法使用,于是我想嘗試使用28335的另外一個定時器中斷采集數據,但是這個定時器中斷的頻率要遠高于ucos的定時 ...…

查看全部問答∨

atmel zigbee協議棧移植(zigduino開發板)

剛接觸zigbee,用的板子是zigduino開發板,MCU型號是Atmel公司的Atmega128RFA1,想移植zigbee協議棧上去,但不知道從何下手。 看了開發板的用戶手冊,支持atmel的bitCloud SDK,看了相關資料,還是一頭霧水。。。 想問問大家怎么移植bitCloud到板 ...…

查看全部問答∨

CAN數據傳輸的一個問題

一般MCU或者ARM或者DSP有32個報文對象或者32個郵箱,每個報文對象或者郵箱都配置一個ID,而每一個CAN數據幀最大長度為8個字節,那么微控制器一共能傳輸32*8=256個字節的數據,如果系統中物理量很多,字節數大于256時,應該怎么處理?…

查看全部問答∨
小廣播
設計資源 培訓 開發板 精華推薦

最新單片機文章
何立民專欄 單片機及嵌入式寶典

北京航空航天大學教授,20余年來致力于單片機與嵌入式系統推廣工作。

 
EEWorld訂閱號

 
EEWorld服務號

 
汽車開發圈

 
機器人開發圈

電子工程世界版權所有 京ICP證060456號 京ICP備10001474號-1 電信業務審批[2006]字第258號函 京公網安備 11010802033920號 Copyright ? 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
主站蜘蛛池模板: 太湖县| 迁西县| 新和县| 疏勒县| 嘉鱼县| 郁南县| 台湾省| 宿州市| 乐山市| 上林县| 全州县| 博白县| 什邡市| 布尔津县| 额尔古纳市| 留坝县| 城步| 耿马| 出国| 玛沁县| 贵南县| 桂平市| 贵港市| 荥阳市| 吉安市| 长泰县| 四子王旗| 盘山县| 行唐县| 高尔夫| 疏勒县| 台前县| 全南县| 兰坪| 永平县| 甘孜县| 永兴县| 无为县| 凤山市| 凤凰县| 广灵县|