STM8 learning summary 3: TIM1 timer usage

Publisher:知识智慧Latest update time:2018-07-24 Source: eefocusKeywords:STM8  TIM1  Timer Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This article mainly summarizes the meaning of each statement when developing library functions of STM8.


(1) TIM1 initialization configuration function


The main function of the function is to set the parameters and working mode of the counter and enable it;


void Tim1_Init(void)

{

  TIM1_TimeBaseInit(16,TIM1_COUNTERMODE_UP,1000,0);

              //(Timer frequency: 16-division is 1/16 of the system frequency, which is 1M, counting up, timer overflow value, reload count starts from 0)

  TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE); //Interrupt configuration and enable function

  TIM1_ARRPreloadConfig(ENABLE); //Enable the automatic reload function

  TIM1_Cmd(ENABLE); //Start timer

}

About the calculation of timer overflow value: timer overflow value = scheduled timing time (us) * timer frequency


(2) TIM1 interrupt vector function


The main function of the function: call the interrupt execution function and clear the counter flag;


//Interrupt vector function

#pragma vector=0xD

__interrupt void TIM1_UPD_OVF_TRG_BRK_IRQHandler(void)

{

  Tim1_Execut(); //Call interrupt execution function

  TIM1_ClearITPendingBit(TIM1_IT_UPDATE); //Clear update counter flag

}

(3) Interrupt execution function


The main function of the function is to perform timing according to your needs and call related function execution;


Note that the count variable must be declared as a static type, which is related to the life cycle of the count variable;


static u16 i=0;

void Tim1_Execute(void)

{

  i++;

  if(i==1000)

  {

    i=0;

    Led_Revese(); //Function

  }

}

(4) Note that the general interrupt must be enabled in the main function


__enable_interrupt();


Keywords:STM8  TIM1  Timer Reference address:STM8 learning summary 3: TIM1 timer usage

Previous article:stm8s timer output compare mode
Next article:STM8S105 TIM1 TIM2 timer timing initialization

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号