STVD中断函数写法
一、寄存器开发方式
1)以定时器TIM4为例,首先对TIM4的相关寄存器进行初始化,打开TIM4的中断,注意在STVD+COSMIC的开发环境下,开总中断使用语句_asm("rim");
void TIM4_init(void) //TIM4 init { TIM4_CR1=0x80; TIM4_PSCR=0x07; //clock div : 128 (T=8us) TIM4_ARR=125; //interrupt per 1ms TIM4_IER=0x01; //enable update interrupt _asm("rim"); //enable EA TIM4_EGR=0x01; //update case TIM4_CR1|=0x01; //enable count }
2)接下来编写TIM4的中断服务程序,可以直接写在main.c中,注意函数前一定要加@far @interrupt,中断服务函数的名称可以自己取,比如此处的TIM4_Interrupt;
@far @interrupt void TIM4_Interrupt(void) { TIM4_SR&=0xfe; cnt_num4++; if(cnt_num4>10) { cnt_num4=0; flag_adc=1; } }
3)接下来在stm8_interrupt_vector.c文件中对中断函数进行声明,并修改中断向量表中对应的中断服务函数名称为我们定义的名称,如TIM4_Interrupt为中断向量的第23个;
extern void _stext(); /* startup routine */ extern @far @interrupt void TIM4_Interrupt(void);
{0x82, NonHandledInterrupt}, /* irq21 */ {0x82, NonHandledInterrupt}, /* irq22 */ {0x82, TIM4_Interrupt}, /* irq23 */ {0x82, NonHandledInterrupt}, /* irq24 */ {0x82, NonHandledInterrupt}, /* irq25 */
二、库函数开发方式
1)以外部中断为例,可用按键触发,首先初始化对应引脚,开启外部中断和总中断,开总中断使用语句_asm("rim");
void main(void) { CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); GPIO_Init(GPIOA,GPIO_PIN_3,GPIO_MODE_OUT_PP_HIGH_FAST); GPIO_Init(GPIOA,GPIO_PIN_2,GPIO_MODE_IN_PU_IT); EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOA,EXTI_SENSITIVITY_FALL_ONLY); _asm("rim"); /* Infinite loop */ while (1) { ; } }
2)接下来在stm8s_it.c文件中,找到对应中断向量的中断服务程序,在里面添加代码即可;
/** * @brief External Interrupt PORTA Interrupt routine. * @param None * @retval None */ INTERRUPT_HANDLER(EXTI_PORTA_IRQHandler, 3) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ GPIO_WriteReverse(GPIOA,GPIO_PIN_3); }