在STM32单片机的编程当中,中断函数的名称都是xxx_IRQHandler格式的。那当编译程序的时候是怎么知道这是一个中断函数的呢?
每个中断都是有入口地址的。程序在执行过程中遇到中断的时候,会先判断这个中断的类型,根据类型再到中断向量表中查找,找到之后,根据表中指明的地址,程序就跳到该地址开始执行代码。
ST提供的固件库当中,有提供一个stm32f10x_it.c文件,打开之后会发现里面放的都是些空函数,但是名字都是“xxx_Handler()”格式,需要使用某个中断的时候,在代码中使能该中断,并到这个文件中将中断服务函数添加上,就可以了。
而这些名字就是一个个的中断函数入口地址。在启动代码中便有这些定义。如下图所示:
可以看到,里面定义了一个个名称,这些名称就是中断函数入口的地址,用这些名称来代替了具体的地址值。
其中DCD是一条数据定义伪指令,用于分配一片连续的字存储单元并用指定的数据初始化。当配置好相应外设的控制寄存器中断使能时,当发生中断(异常)时,该异常被Cortex-M3内核接受,对应的异常Handler就会执行。而这个响应过程都是硬件来完成的,当然为了决定Handler的入口地址,Cortex-M3使用了“向量表查表机制”。startup_stm32f10x_hd.s启动文件已经为相应外设的中断服务函数赋予了相应的地址。所以在异常发生后,CPU进入异常模式,同时程序计数器PC自动指向异常入口地址,进而执行中断服务函数中的应用。
相关文章









