How to write STVD interrupt function

Publisher:数字思维Latest update time:2020-09-08 Source: eefocusKeywords:STVD Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Register development method


1) Taking timer TIM4 as an example, first initialize the relevant registers of TIM4 and enable the interrupt of TIM4. Note that in the development environment of STVD+COSMIC, the statement _asm("rim") is used to enable the general interrupt;


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) Next, write the interrupt service routine of TIM4, which can be written directly in main.c. Note that @far @interrupt must be added before the function. The name of the interrupt service function can be chosen by yourself, such as TIM4_Interrupt here;


@far @interrupt void TIM4_Interrupt(void)

{

    TIM4_SR&=0xfe;

    

    cnt_num4++;

    if(cnt_num4>10)

    {

        cnt_num4=0;

        flag_adc=1;

    }

}


3) Next, declare the interrupt function in the stm8_interrupt_vector.c file, and modify the corresponding interrupt service function name in the interrupt vector table to the name we defined, such as TIM4_Interrupt is the 23rd interrupt vector;


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 */   


2. Library Function Development Method


1) Taking external interrupt as an example, it can be triggered by a button. First, initialize the corresponding pin, turn on external interrupt and general interrupt, and turn on general interrupt using statement _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) Next, find the interrupt service routine corresponding to the interrupt vector in the stm8s_it.c file and add the code in it;


/**

  * @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);

}


Keywords:STVD Reference address:How to write STVD interrupt function

Previous article:STM8 Pitfall Avoidance—PB4 and PB5
Next article:Solution to insufficient space on STM8

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号