STM8L Discovery Kit Study Notes - TIM Timer (V)

Publisher:荒火Latest update time:2015-12-21 Source: eefocusKeywords:STM8L Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
STM8L timer points: advanced timer TIM1, general timer TIM2/3/5, basic timer TIM4. Among them, the advanced timer and general timer are both 16-bit timers. The difference is that the advanced timer has DMA function, and the basic timer is 8-bit timer. For more detailed functions, please refer to the instruction manual.

Today we use the basic timer TIM4 to do a delay task, which is implemented in two ways: query and interrupt.

1 The query method is to query whether the UIF bit (update interrupt flag) in the status register is set. The main code is:

void delay_ms(u16 n_ms)

{

/* Init TIMER 4 */

  CLK_PeripheralClockConfig(CLK_Peripheral_TIM4, ENABLE);

 

/* Init TIMER 4 prescaler: / (2^6) = /64 */

  TIM4->PSCR = 6;

 

/* HSI div by 1 --> Auto-Reload value: 16M / 64 = 1/4M, 1/4M / 1k = 250*/

  TIM4->ARR = 250;

 

/* Counter value: 2, to compensate the initialization of TIMER*/

  TIM4->CNTR = 2;

 

/* clear update flag */

  TIM4->SR1 &= ~TIM4_SR1_UIF;

 

/* Enable Counter */

  TIM4->CR1 |= TIM4_CR1_CEN;

 

  while(n_ms--)

  {

    while((TIM4->SR1 & TIM4_SR1_UIF) == 0) ;

    TIM4->SR1 &= ~TIM4_SR1_UIF;

  }

 

/* Disable Counter */

  TIM4->CR1 &= ~TIM4_CR1_CEN;

}

Keywords:STM8L Reference address:STM8L Discovery Kit Study Notes - TIM Timer (V)

Previous article:STM8L Exploration Kit Study Notes - RTC Real-time Clock (VI)
Next article:STM8L Discovery Kit Study Notes-CLK Clock Control (IV)

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号