以下以stm8s103为例介绍在ST Visual Develop和IAR环境下的中断设置;这两种只是环境不同其实中断向量是不会变的,先看一下数据手册,
再结合芯片手册中中断向量映像表,如下图所示
结合起来的话我们就能够理解的。
在用ST Visual Develop环境时新建一个新的Stm8工程它会自动生成一个main.c和一个stm8_interrupt_vector.c文件。
空白的main.c文件时如下所示的:
[cpp] view plaincopy
/* MAIN.C file
*
* Copyright (c) 2002-2005 STMicroelectronics
*/
main()
{
while (1);
}
新的空白的stm8_interrupt_vector.c是如下面所示的:
[cpp] view plaincopy
/* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
* Copyright (c) 2007 STMicroelectronics
*/
typedef void @far (*interrupt_handler_t)(void);
struct interrupt_vector {
unsigned char interrupt_instruction;
interrupt_handler_t interrupt_handler;
};
@far @interrupt void NonHandledInterrupt (void)
{
/* in order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction
*/
return;
}
extern void _stext(); /* startup routine */
struct interrupt_vector const _vectab[] = {
{0x82, (interrupt_handler_t)_stext}, /* reset */
{0x82, NonHandledInterrupt}, /* trap */
{0x82, NonHandledInterrupt}, /* irq0 */
{0x82, NonHandledInterrupt}, /* irq1 */
{0x82, NonHandledInterrupt}, /* irq2 */
{0x82, NonHandledInterrupt}, /* irq3 */
{0x82, NonHandledInterrupt}, /* irq4 */
{0x82, NonHandledInterrupt}, /* irq5 */
{0x82, NonHandledInterrupt}, /* irq6 */
{0x82, NonHandledInterrupt}, /* irq7 */
{0x82, NonHandledInterrupt}, /* irq8 */
{0x82, NonHandledInterrupt}, /* irq9 */
{0x82, NonHandledInterrupt}, /* irq10 */
{0x82, NonHandledInterrupt}, /* irq11 */
{0x82, NonHandledInterrupt}, /* irq12 */
{0x82, NonHandledInterrupt}, /* irq13 */
{0x82, NonHandledInterrupt}, /* irq14 */
{0x82, NonHandledInterrupt}, /* irq15 */
{0x82, NonHandledInterrupt}, /* irq16 */
{0x82, NonHandledInterrupt}, /* irq17 */
{0x82, NonHandledInterrupt}, /* irq18 */
{0x82, NonHandledInterrupt}, /* irq19 */
{0x82, NonHandledInterrupt}, /* irq20 */
{0x82, NonHandledInterrupt}, /* irq21 */
{0x82, NonHandledInterrupt}, /* irq22 */
{0x82, NonHandledInterrupt}, /* irq23 */
{0x82, NonHandledInterrupt}, /* irq24 */
{0x82, NonHandledInterrupt}, /* irq25 */
{0x82, NonHandledInterrupt}, /* irq26 */
{0x82, NonHandledInterrupt}, /* irq27 */
{0x82, NonHandledInterrupt}, /* irq28 */
{0x82, NonHandledInterrupt}, /* irq29 */
};
如果在程序中没用到中断,stm8_interrupt_vector.c是不需要改的,但是如果用到中断必须在相应的位置做改动,比如如果我们用到TIM4定时器中断,我们对照中断向量表TIM4的序列是23,那么在stm8_interrupt_vector.c的struct interrupt_vector const _vectab[]中对应的/*ireq23*/的NonHandledInterrupt做修改(命名随意),如下所示:
stm8_interrupt_vector.c:
#include "STM8S103F.h"
typedef void @far (*interrupt_handler_t)(void);
struct interrupt_vector {
unsigned char interrupt_instruction;
interrupt_handler_t interrupt_handler;
};
@far @interrupt void TIM4IRQ (void)
{
TIM4_SR=0x00;
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxx(自己根据需要写入内容)
}
}
@far @interrupt void NonHandledInterrupt (void)
{
/* in order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction
*/
return;
}
extern void _stext(); /* startup routine */
struct interrupt_vector const _vectab[] = {
{0x82, (interrupt_handler_t)_stext}, /* reset */
{0x82, NonHandledInterrupt}, /* trap */
{0x82, NonHandledInterrupt}, /* irq0 */
{0x82, NonHandledInterrupt}, /* irq1 */
{0x82, NonHandledInterrupt}, /* irq2 */
{0x82, NonHandledInterrupt}, /* irq3 */
{0x82, NonHandledInterrupt}, /* irq4 */
{0x82, NonHandledInterrupt}, /* irq5 */
{0x82, NonHandledInterrupt}, /* irq6 */
{0x82, NonHandledInterrupt}, /* irq7 */
{0x82, NonHandledInterrupt}, /* irq8 */
{0x82, NonHandledInterrupt}, /* irq9 */
{0x82, NonHandledInterrupt}, /* irq10 */
{0x82, NonHandledInterrupt}, /* irq11 */
{0x82, NonHandledInterrupt}, /* irq12 */
{0x82, NonHandledInterrupt}, /* irq13 */
{0x82, NonHandledInterrupt}, /* irq14 */
{0x82, NonHandledInterrupt}, /* irq15 */
{0x82, NonHandledInterrupt}, /* irq16 */
{0x82, NonHandledInterrupt}, /* irq17 */
{0x82, NonHandledInterrupt}, /* irq18 */
{0x82, NonHandledInterrupt}, /* irq19 */
{0x82, NonHandledInterrupt}, /* irq20 */
{0x82, NonHandledInterrupt}, /* irq21 */
{0x82, NonHandledInterrupt}, /* irq22 */
{0x82, TIM4IRQ}, /* irq23 */
{0x82, NonHandledInterrupt}, /* irq24 */
{0x82, NonHandledInterrupt}, /* irq25 */
{0x82, NonHandledInterrupt}, /* irq26 */
{0x82, NonHandledInterrupt}, /* irq27 */
{0x82, NonHandledInterrupt}, /* irq28 */
{0x82, NonHandledInterrupt}, /* irq29 */
};
下面说一下在IAR下,在IAR下必须要添加iostm8s103k3.h文件,在文件的最后有如下内容:
/*-------------------------------------------------------------------------
* Interrupt vector numbers
*-----------------------------------------------------------------------*/
#define AWU_vector 0x03
#define SPI_TXE_vector 0x0C
#define SPI_RXNE_vector 0x0C
#define SPI_WKUP_vector 0x0C
#define SPI_CRCERR_vector 0x0C
#define SPI_OVR_vector 0x0C
#define SPI_MODF_vector 0x0C
#define TIM1_OVR_UIF_vector 0x0D
#define TIM1_CAPCOM_BIF_vector 0x0D
#define TIM1_CAPCOM_TIF_vector 0x0D
#define TIM1_CAPCOM_CC1IF_vector 0x0E
#define TIM1_CAPCOM_CC2IF_vector 0x0E
#define TIM1_CAPCOM_CC3IF_vector 0x0E
#define TIM1_CAPCOM_CC4IF_vector 0x0E
#define TIM1_CAPCOM_COMIF_vector 0x0E
#define TIM2_OVR_UIF_vector 0x0F
#define TIM2_CAPCOM_CC1IF_vector 0x10
#define TIM2_CAPCOM_TIF_vector 0x10
#define TIM2_CAPCOM_CC2IF_vector 0x10
#define TIM2_CAPCOM_CC3IF_vector 0x10
#define UART1_T_TXE_vector 0x13
#define UART1_T_TC_vector 0x13
#define UART1_R_OR_vector 0x14
#define UART1_R_RXNE_vector 0x14
#define UART1_R_IDLE_vector 0x14
#define UART1_R_PE_vector 0x14
#define UART1_R_LBDF_vector 0x14
#define I2C_ADD10_vector 0x15
#define I2C_ADDR_vector 0x15
#define I2C_OVR_vector 0x15
#define I2C_STOPF_vector 0x15
#define I2C_BTF_vector 0x15
#define I2C_WUFH_vector 0x15
#define I2C_RXNE_vector 0x15
#define I2C_TXE_vector 0x15
#define I2C_BERR_vector 0x15
#define I2C_ARLO_vector 0x15
#define I2C_AF_vector 0x15
#define I2C_SB_vector 0x15
#define ADC1_AWS0_vector 0x18
#define ADC1_AWS1_vector 0x18
#define ADC1_AWS2_vector 0x18
#define ADC1_AWS3_vector 0x18
#define ADC1_AWS4_vector 0x18
#define ADC1_AWS5_vector 0x18
#define ADC1_AWS6_vector 0x18
#define ADC1_EOC_vector 0x18
#define ADC1_AWS8_vector 0x18
#define ADC1_AWS9_vector 0x18
#define ADC1_AWDG_vector 0x18
#define ADC1_AWS7_vector 0x18
#define TIM4_OVR_UIF_vector 0x19
#define FLASH_EOP_vector 0x1A
#define FLASH_WR_PG_DIS_vector 0x1A
其实这是一样的,也是对照中断向量表,如果用到中断,必须自己写中断,比如还是TIM4定时器中断,
#pragma vector=TIM4_OVR_UIF_vector
__interrupt void TIM4_UPD_OVF_IRQHandler (void)
{
TIM4_SR = 0X00;
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxx(自己根据需要写)
}