stm32按键中断点灯

原理学习:

 

中断总结:

1.中断是由硬件产生的   2.中断要有中断服务程 3.

 

中断理解:

郭工正在上课(cpu正在运行)→有人来敲门(EXIT外部中断)→郭工把正在执行的程序放下保存现在的现场(cpu记住程序执行到哪,执行到exti了,记住现场)→郭工去开门(执行中断处理程序→返回现场(返回中断之前的代码执行)

 

中断分级理解:

 

有很多个门。1号门敲门,在执行开1号门途中,2号门VIP敲门,停止1号门处理程序,去开2号门;3号门超级VIP敲门,停止2号门处理程序,去开3号门;处理完3号门回去开2号门,开完2号门回去开1号门,再返回课堂现场。

 

Stack_Size      EQU     0x00000200                           ;//定义堆栈大小
                AREA    STACK, NOINIT, READWRITE, ALIGN=3    ;//定义一个数据段 按8字节对齐
Stack_Mem       SPACE   Stack_Size                           ;//保留Stack_Size大小的堆栈空间
__initial_sp                                                 ;//标号,代表堆栈顶部地址,后面有用

;//  Heap Configuration
;//     Heap Size (in Bytes) <0x0-0xFFFFFFFF:8>
;// 
Heap_Size       EQU     0x00000020                            ;//定义堆空间大小
                AREA    HEAP, NOINIT, READWRITE, ALIGN=3      ;//定义一个数据段,8字节对齐
__heap_base
Heap_Mem        SPACE   Heap_Size                             ;//保留Heap_Size的堆空间
__heap_limit                                                  ;//标号,代表堆末尾地址,后面有用

                PRESERVE8                                     ;//指示编译器8字节对齐
                THUMB                                         ;//指示编译器为THUMB指令

; Vector Table Mapped to Address 0 at Reset
                AREA    RESET, DATA, READONLY                  ;//定义只读数据段,其实放在CODE区,位于0地址
                
                EXTERN  NMIException
                EXTERN  HardFaultException
                EXTERN  MemManageException
                EXTERN  BusFaultException
                EXTERN  UsageFaultException
                EXTERN  SVCHandler
                EXTERN  DebugMonitor
                EXTERN  PendSVC
                EXTERN  SysTickHandler                         ;//声明这些符号在外部定义,同C
				                                               ;//在××it.c中实现这些函数 ,中断就能自动调用了
                EXPORT  __Vectors
__Vectors       DCD     __initial_sp              ;//Cotex-M要求此处为堆栈顶部地址 0X0000  0000
                DCD     Reset_Handler             ;//一上电就产生复位信号  0X0000  0004
                DCD     NMIException              ;//不可屏蔽中断         0X0000  0008
                DCD     HardFaultException        ;//硬件失效             0X0000  000C
                DCD     MemManageException        ;//存储管理             0X0000  0010            
                DCD     BusFaultException         ;//总线错误             0X0000  0014
                DCD     UsageFaultException       ;//错误应用             0X0000  0018
                DCD     0                         ;//保留位       
                DCD     0                         ;  
                DCD     0                         ; 
                DCD     0                         ;//保留位
                DCD     SVCHandler                ;// SWT指令的系统服务调用0X0000  002B
                DCD     DebugMonitor              ;//调试监控器           0X0000  002C
                DCD     0                         ;//保留位
                DCD     PendSVC                   ;//PendSV Handler
                DCD     SysTickHandler            ; //系统滴答定时器
                              
   ; External Interrupts                          //一大堆的异常处理函数地址
                EXTERN  WWDG_IRQHandler            //窗口定时器中断             
                EXTERN  PVD_IRQHandler            //连到EXTI电源电压检测中断    
                EXTERN  TAMPER_IRQHandler            //侵入检测中断
                EXTERN  RTC_IRQHandler            //实时时钟全局中断  
                EXTERN  FLASH_IRQHandler            //闪存全局终端
                EXTERN  RCC_IRQHandler            //复位和时钟控制RCC中断  
                EXTERN  EXTI0_IRQHandler            //EXTI线0中断    
                EXTERN  EXTI1_IRQHandler            //EXTI线1中断 
                EXTERN  EXTI2_IRQHandler            //EXTI线2中断 
                EXTERN  EXTI3_IRQHandler            //EXTI线3中断 
                EXTERN  EXTI4_IRQHandler            //EXTI线4中断(一共五组外部中断) 
                EXTERN  DMAChannel1_IRQHandler
                EXTERN  DMAChannel2_IRQHandler
                EXTERN  DMAChannel3_IRQHandler
                EXTERN  DMAChannel4_IRQHandler
                EXTERN  DMAChannel5_IRQHandler
                EXTERN  DMAChannel6_IRQHandler
                EXTERN  DMAChannel7_IRQHandler
                EXTERN  ADC_IRQHandler
                EXTERN  USB_HP_CAN_TX_IRQHandler
                EXTERN  USB_LP_CAN_RX0_IRQHandler
                EXTERN  CAN_RX1_IRQHandler
                EXTERN  CAN_SCE_IRQHandler
                EXTERN  EXTI9_5_IRQHandler
                EXTERN  TIM1_BRK_IRQHandler
                EXTERN  TIM1_UP_IRQHandler
                EXTERN  TIM1_TRG_COM_IRQHandler
                EXTERN  TIM1_CC_IRQHandler
                EXTERN  TIM2_IRQHandler
                EXTERN  TIM3_IRQHandler
                EXTERN  TIM4_IRQHandler
                EXTERN  I2C1_EV_IRQHandler
                EXTERN  I2C1_ER_IRQHandler
                EXTERN  I2C2_EV_IRQHandler
                EXTERN  I2C2_ER_IRQHandler
                EXTERN  SPI1_IRQHandler
                EXTERN  SPI2_IRQHandler
                EXTERN  USART1_IRQHandler
                EXTERN  USART2_IRQHandler
                EXTERN  USART3_IRQHandler
                EXTERN  EXTI15_10_IRQHandler
                EXTERN  RTCAlarm_IRQHandler
                EXTERN  USBWakeUp_IRQHandler                    ;//同上,
                
                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     DMAChannel1_IRQHandler    ; DMA Channel 1
                DCD     DMAChannel2_IRQHandler    ; DMA Channel 2
                DCD     DMAChannel3_IRQHandler    ; DMA Channel 3
                DCD     DMAChannel4_IRQHandler    ; DMA Channel 4
                DCD     DMAChannel5_IRQHandler    ; DMA Channel 5
                DCD     DMAChannel6_IRQHandler    ; DMA Channel 6
                DCD     DMAChannel7_IRQHandler    ; DMA Channel 7
                DCD     ADC_IRQHandler            ; ADC
                DCD     USB_HP_CAN_TX_IRQHandler  ; USB High Priority or CAN TX
                DCD     USB_LP_CAN_RX0_IRQHandler ; USB Low  Priority or CAN RX0
                DCD     CAN_RX1_IRQHandler        ; CAN RX1
                DCD     CAN_SCE_IRQHandler        ; CAN 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   ;//同上

                AREA    |.text|, CODE, READONLY        ;//定义代码段

; Reset Handler
Reset_Handler   PROC                                        ;//Rset_Handler的实现
                EXPORT  Reset_Handler             [WEAK]    ;//在外部没有定义该符号时导出该符号,见HELP中[WEAK]
                IMPORT  __main                              ;//导入符号,__main为 运行时库提供的函数;完成堆栈,堆的初始话
                LDR     R0, =__main                         ;//等工作,会调用下面定义的__user_initial_stackheap;
                BX      R0                                  ;//跳到__main,进入C的世界
                ENDP

                ALIGN

; User Initial Stack & Heap
                IF      :DEF:__MICROLIB                     ;//如果使用micro lib,micro lib 描述见armlib.chm
                
                EXPORT  __initial_sp
                EXPORT  __heap_base
                EXPORT  __heap_limit                        ;//只导出几个定义
                
                ELSE                                        ;//如果使用默认C运行时库
                
                IMPORT  __use_two_region_memory
                EXPORT  __user_initial_stackheap
__user_initial_stackheap                                    ;//则进行堆栈和堆的赋值,在__main函数执行过程中调用。
                LDR     R0, =  Heap_Mem
                LDR     R1, =(Stack_Mem + Stack_Size)
                LDR     R2, = (Heap_Mem +  Heap_Size)
                LDR     R3, = Stack_Mem
                BX      LR
                ALIGN
                ENDIF

                END                                         EXTERN  EXTI0_IRQHandler            //EXTI线0中断    
                EXTERN  EXTI1_IRQHandler            //EXTI线1中断 
                EXTERN  EXTI2_IRQHandler            //EXTI线2中断 
                EXTERN  EXTI3_IRQHandler            //EXTI线3中断 
                EXTERN  EXTI4_IRQHandler            //EXTI线4中断(一共五组外部中断) 
                EXTERN  DMAChannel1_IRQHandler
                EXTERN  DMAChannel2_IRQHandler
                EXTERN  DMAChannel3_IRQHandler
                EXTERN  DMAChannel4_IRQHandler
                EXTERN  DMAChannel5_IRQHandler
                EXTERN  DMAChannel6_IRQHandler
                EXTERN  DMAChannel7_IRQHandler
                EXTERN  ADC_IRQHandler
                EXTERN  USB_HP_CAN_TX_IRQHandler
                EXTERN  USB_LP_CAN_RX0_IRQHandler
                EXTERN  CAN_RX1_IRQHandler
                EXTERN  CAN_SCE_IRQHandler
                EXTERN  EXTI9_5_IRQHandler
                EXTERN  TIM1_BRK_IRQHandler
                EXTERN  TIM1_UP_IRQHandler
                EXTERN  TIM1_TRG_COM_IRQHandler
                EXTERN  TIM1_CC_IRQHandler
                EXTERN  TIM2_IRQHandler
                EXTERN  TIM3_IRQHandler
                EXTERN  TIM4_IRQHandler
                EXTERN  I2C1_EV_IRQHandler
                EXTERN  I2C1_ER_IRQHandler
                EXTERN  I2C2_EV_IRQHandler
                EXTERN  I2C2_ER_IRQHandler
                EXTERN  SPI1_IRQHandler
                EXTERN  SPI2_IRQHandler
                EXTERN  USART1_IRQHandler
                EXTERN  USART2_IRQHandler
                EXTERN  USART3_IRQHandler
                EXTERN  EXTI15_10_IRQHandler
                EXTERN  RTCAlarm_IRQHandler
                EXTERN  USBWakeUp_IRQHandler                    ;//同上,
                
                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     DMAChannel1_IRQHandler    ; DMA Channel 1
                DCD     DMAChannel2_IRQHandler    ; DMA Channel 2
                DCD     DMAChannel3_IRQHandler    ; DMA Channel 3
                DCD     DMAChannel4_IRQHandler    ; DMA Channel 4
                DCD     DMAChannel5_IRQHandler    ; DMA Channel 5
                DCD     DMAChannel6_IRQHandler    ; DMA Channel 6
                DCD     DMAChannel7_IRQHandler    ; DMA Channel 7
                DCD     ADC_IRQHandler            ; ADC
                DCD     USB_HP_CAN_TX_IRQHandler  ; USB High Priority or CAN TX
                DCD     USB_LP_CAN_RX0_IRQHandler ; USB Low  Priority or CAN RX0
                DCD     CAN_RX1_IRQHandler        ; CAN RX1
                DCD     CAN_SCE_IRQHandler        ; CAN 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   ;//同上

                AREA    |.text|, CODE, READONLY        ;//定义代码段

; Reset Handler
Reset_Handler   PROC                                        ;//Rset_Handler的实现
                EXPORT  Reset_Handler             [WEAK]    ;//在外部没有定义该符号时导出该符号,见HELP中[WEAK]
                IMPORT  __main                              ;//导入符号,__main为 运行时库提供的函数;完成堆栈,堆的初始话
                LDR     R0, =__main                         ;//等工作,会调用下面定义的__user_initial_stackheap;
                BX      R0                                  ;//跳到__main,进入C的世界
                ENDP

                ALIGN

; User Initial Stack & Heap
                IF      :DEF:__MICROLIB                     ;//如果使用micro lib,micro lib 描述见armlib.chm
                
                EXPORT  __initial_sp
                EXPORT  __heap_base
                EXPORT  __heap_limit                        ;//只导出几个定义
                
                ELSE                                        ;//如果使用默认C运行时库
                
                IMPORT  __use_two_region_memory
                EXPORT  __user_initial_stackheap
__user_initial_stackheap                                    ;//则进行堆栈和堆的赋值,在__main函数执行过程中调用。
                LDR     R0, =  Heap_Mem
                LDR     R1, =(Stack_Mem + Stack_Size)
                LDR     R2, = (Heap_Mem +  Heap_Size)
                LDR     R3, = Stack_Mem
                BX      LR
                ALIGN
                ENDIF

                END                                         

 

 

 

 

stm32按键中断点灯_第1张图片

EXTI16(0-15)组外部中断:EXTI 0-4 有独立的中断服务处理程序

  stm32按键中断点灯_第2张图片

EXTI5-9用的是一个中断服务处理程序

EXTI10-15用的是一个中断服务处理程序

 

举例:

stm32按键中断点灯_第3张图片

清除中断标志位的意思是:关中断。

【EXIT与GPIO连接图】

注意:一个中断只能同时被一个引脚使用,eg:对于EXTI0,PA0、PB0引脚不能同时作为中断的输入模式,因为他们的中断向量号都一样,都是EXTI0,无法判断优先级。就不知道是哪个管脚产生中断啦。

stm32按键中断点灯_第4张图片

 

...

...

stm32按键中断点灯_第5张图片

【电路图】

stm32按键中断点灯_第6张图片

 

【中断有4种触发方式】

1.电平触发 :高电平触发:电平=1,产生中断 ;  低电平触发:电平=0,产生中断

2.沿触发:上升沿触发:电平0->1,产生中断; 下降沿触发:电平1->0,产生中断

 

【分析:涉及按键KEY,应该选择哪种触发呢?】

1.高电平触发,没按按键时,上拉电阻拉为高电平,一直产生中断×

2.低电平触发,按下按键,接GND,变为低电平,一直产生中断×

3.上升沿触发,按键释放时,0->1,此时产生中断,按键已经释放,就无法消抖了。×

4.下降沿触发默认高电平,当按键按下,1->0与GND连通变低电平,产生中断(下降沿触发)√

 

 

你可能感兴趣的:(stm32)