stm8s development (V) Use of TIMER: timing!

Publisher:太白山人Latest update time:2020-08-31 Source: eefocusKeywords:stm8s  TIMER  timing Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  STM8S provides three types of TIM timers: advanced control type (TIM1), general type (TIM2/TIM3/TIM5) and basic timer (TIM4/TIM6). Although they have different functions, they are all based on a common architecture. This common architecture makes it very easy and convenient to design applications using various timers (same register mapping, same basic functions).

    

 

  Using timing can determine a time slice, which is convenient for controlling the sending rate, sampling rate, and other tasks that require a high time. These operations can be executed in the timer interrupt. In this example, the timing is 1s, and the LED light flips once, achieving the effect of flashing once every 2s. Since the timing operation is simple, we use the basic timer: TIMER 4

  void Init_Timer4(void)

  {

  //128 division, 256 counts, at 16MHz, an interrupt every 2.048ms!

  //128 division, 256 counts, at 128KHz, an interruption occurs every 256ms!

  TIM4_CR1=0x00; //turn off the counter

  //TIM4_IER=0x00;

  TIM4_IER=0x01; //Update interrupt enable

  TIM4_EGR=0x01;

  TIM4_CNTR=255; //Counter value

  TIM4_ARR=255; //Automatic reload value

  TIM4_PSCR=0x07; //Frequency division value

  TIM4_CR1=0x01; //Enable counter

  }

  It is worth noting that if we use 16M as the main clock, through the maximum frequency division and maximum count, we can also achieve a maximum timing time of 2.048ms. Similarly, if we use 128K as the main clock, the maximum timing time can be 256ms.

  Next is the timer terminal function, in which we can do other operations:

  u16 i=0;

  #pragma vector = TIM4_OVR_UIF_vector //0x19

  __interrupt void TIM4_OVR_UIF_IRQHandler(void)

  {

  i++;

  TIM4_SR=0x00;

  if(i==488) //2.048*488=1000ms

  {

  LED_Reverse();

  i=0;

  }

  }

  Here, the variable i is used to calculate the number of times the interrupt is entered. If we need to flip the LED once per second, we need to enter the interrupt 488 times.

  When the interrupt returns, the timer will automatically reload: (TIM4_ARR=255; // value for automatic reload)

  The timer timing is related to the counter value: (TIM4_CNTR=255; //counter value)

  It is also related to the frequency division value: (TIM4_PSCR=0x07; // frequency division value)

  To open or close the register, you only need to modify the TIM4_CR1 register: (TIM4_CR1=0x00; // turn off the counter TIM4_CR1=0x01; // enable the counter)


Keywords:stm8s  TIMER  timing Reference address:stm8s development (V) Use of TIMER: timing!

Previous article:STM8 MCU ADC Application (IAR)
Next article:JTAG/SWD/ISP/SWIM details the four programming methods of MCU

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号