简述:当中断触发时,会保存当前运行状态,然后跳转到中断向量处执行(可嵌套),中断完成后再返回保存的运行状态执行。
中断服务函数并非用户(写程序的人)调用,而是预先写入中断向量(相应中断执行首地址),中断发生后自动执行,所以源码中只需定义实现自己的中断服务函数而不需要考虑在mian中调用的时。
那么问题的关键便是一个普通的c函数是如何实现写入中断向量。mcu的地址最低区是用作中断向量表的,即中断触发后,将执行相应中断向量(字)处的指令(一般为跳转指令)然后跳转到相应的中断服务函数执行,那么中断向量是如何跳转到中断服务函数的地址的呢。
首先我们要看中断服务函数的声明,库文件中唯一的中断服务函数标示符只存在startup.s中,废话少说上代码:
;******************** (C) COPYRIGHT 2014 STMicroelectronics ********************
;* File Name : startup_stm32f051.s
;* Author : MCD Application Team
;* Version : V1.4.0
;* Date : 24-July-2014
;* Description : STM32F051 devices vector table for MDK-ARM toolchain.
;* This module performs:
;* - Set the initial SP
;* - Set the initial PC == Reset_Handler
;* - Set the vector table entries with the exceptions ISR address
;* - Configure the system clock
;* - Branches to __main in the C library (which eventually
;* calls main()).
;* After Reset the CortexM0 processor is in Thread mode,
;* priority is Privileged, and the Stack is set to Main.
;* <<< Use Configuration Wizard in Context Menu >>>
;*******************************************************************************
; @attention
;
; Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
; You may not use this file except in compliance with the License.
; You may obtain a copy of the License at:
;
; http://www.st.com/software_license_agreement_liberty_v2
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
;
;*******************************************************************************
;
; Amount of memory (in bytes) allocated for Stack
; Tailor this val