stm32中断服务函数的机制

本文介绍了STM32中断服务函数的工作机制。当中断触发时,系统保存当前状态并跳转到中断向量执行。中断服务函数不是由用户直接调用,而是通过中断向量自动执行。中断向量如何指向中断服务函数的地址是关键,这通常通过在启动文件中定义标号并在中断向量中写入对应的跳转指令实现。通过在C代码中定义同名的中断服务函数,可以实现在中断向量到函数间的跳转。
摘要由CSDN通过智能技术生成

  简述:当中断触发时,会保存当前运行状态,然后跳转到中断向量处执行(可嵌套),中断完成后再返回保存的运行状态执行。


  中断服务函数并非用户(写程序的人)调用,而是预先写入中断向量(相应中断执行首地址),中断发生后自动执行,所以源码中只需定义实现自己的中断服务函数而不需要考虑在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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值