TIM timer operation based on STM8---STM8-Chapter 3

Publisher:会飞的笨鱼Latest update time:2019-12-04 Source: eefocusKeywords:STM8  TIM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Overview


  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).


  This article only introduces the STM8S103 chip. The chip has three timers, one for each type of timer, namely advanced control type (TIM1), general type (TIM2), and basic timer (TIM4).


2. About TIM in the STM8S103 manual

  The functions of each timer are introduced in detail, please take a look at it carefully.


3. TIMx detailed explanation


3.1 TIM1 analysis


  TIM1_Prescaler: Prescaler coefficient, 16-bit increasing, decreasing and bidirectional (increasing/decreasing) auto-reload counter with 16-bit prescaler.


  TIM1_CounterMode: Counting mode. There are three counting modes: up counting mode, down counting mode, and center alignment mode (this mode also contains three alignment modes).


  TIM1_Period: Automatic reload value, this item can take any value from 1 to 65535.


  TIM1_RepetitionCounter : Repetition counter value.


 


  Assuming our system clock is 16Mhz and we set the timer to 1 second, then our initialization settings should be:  


  TIM1_TimeBaseInit(15,TIM1_COUNTERMODE_UP,1000,100);  


Calculation method: The timer frequency fcnk = 16Mhz/(15+1) = 1Mhz, which means that one count is 1us, 1000 counts are 1ms, and if the count is repeated 100 times, then one interrupt timing time is 100ms. In order to achieve the effect of 1s, we also need to make a judgment 10 times in the interrupt function. (1MHZ = 1000KHZ = 1000000HZ, equivalent to 1/1000000 = 1us)


3.2 TIM2 analysis


  TIM2_Prescaler: 15-bit prescaler factor, which can be adjusted to a power-of-2 value between 1 and 32768.


  TIM2_Period: Automatic reload value, this item can take any value from 1 to 65535.


Assuming our system clock is 16Mhz and we set the timer to 1 second, then our initialization settings should be: 


TIM2_TimeBaseInit(TIM2_PRESCALER_16, 1000)


Calculation method: The timer frequency fcnk = 16Mhz/(16) = 1Mhz, which means that one count is 1us, 1000 counts are 1ms, and the default repeat count is 1 time. The interrupt time is 1ms. In order to achieve the effect of 1s, we also need to load the interrupt function and perform a judgment 1000 times.


3.3 TIM4 analysis


  TIM4_Prescaler: 8-bit automatically loaded adjustable prescaler factor, the prescaler factor can be adjusted to a power of 2 value between 1 and 128.


  TIM4_Period: Automatic reload value, this item can take any value from 1 to 255. 


Assuming our system clock is 16Mhz and we set the timer to 1 second, then our initialization settings should be:


TIM4_TimeBaseInit(TIM4_PRESCALER_128, 250);


Calculation method: The timer frequency fcnk = 16Mhz/(128) = 0.125Mhz, which means that one count is 8us, 250 counts are 2ms, and the default repeat count is 1 time. The interrupt time is 2ms. In order to achieve the effect of 1s, we also need to load the interrupt function and make a 500-time judgment.


4. Routines


4.1 Compilation Environment


  My compilation environment is IAR, which is the mainstream platform for STM8 and is recommended. However, I plan to wait until STCubeMX is updated with a more convenient version before using Keil5, because when using STM32, I use Keil5 to compile the code, which is indeed very convenient.


4.2 Main Chip


  My main chip is 103 in the STM8S series. Among them, STM8S 003, 005, 103, 105 have the same configuration (peripheral and CPU frequency, FLASH), and can be burned if the code is the same.


4.3 Adding library files


  Our project can be copied in the official routine in IAR. The operation process is: open STM8S_StdPeriph_Lib (this is an official library file, which is included when you download the IAR STM8 package, and contains library files and corresponding routines), copy the Libraries file to the file where your project is located, and add the library file related to ADC to your project list. After adding, you can start writing code (if you add all the library files, and there is a red dot error in the library file after compiling the program, it is because the chip you selected does not have this function, you need to delete it to avoid the error.) as shown in the figure.

4.4 Code

4.4.1 TIM1


initialization:


/*******************************************************************************

* Function Name  : MX_TIM1_Init

* Description    : TIM1 Init

* Input          : None

* Output         : None

* Return         : None

********************************************************************************/

void MX_TIM1_Init(void)

{

  //Timer 1 parameter initialization (15 division, upward counting, number of counts, number of repetitions)

  TIM1_TimeBaseInit(15, TIM1_COUNTERMODE_UP, 1000, 100);

 

  //Clear TIM1 update flag

  TIM1_ClearFlag(TIM1_FLAG_UPDATE);

 

  //Enable update interrupt

  TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);

  TIM1_Cmd(ENABLE);

  

  // Enable interrupt

  enableInterrupts();

}

Interrupt service function:


Since the interrupts of the library function project template in IAR are all written in the stm8s_it.c library file, this code needs to be written in the chip corresponding to the stm8s_it.c library file to perform interrupt processing.


By initializing the configured parameters and accumulating the count 10 times, the effect of flipping the LED once every 1S is achieved. The flag bit of the timer needs to be cleared after each interrupt.


INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11)

{

  /* In order to detect unexpected events during development,

     it is recommended to set a breakpoint on the following instruction.

  */

  static uint8_t count = 0;

  

  if(count++ == 10)

  {

    //Flip the LED

    GPIO_WriteReverse(Led_Opt_GPIO_Port, Led_Opt_Pin);

    

    count = 0;

  }

  // Clear the flag bit of timer 1

  TIM1_ClearITPendingBit(TIM1_IT_UPDATE);

}

 


4.4.2 TIM2


initialization:


/*******************************************************************************

* Function Name  : MX_TIM2_Init

* Description    : TIM1 Init

* Input          : None

* Output         : None

* Return         : None

********************************************************************************/

void MX_TIM2_Init(void)

{

  //Timer 2 parameter initialization (16 division, count times)

  TIM2_TimeBaseInit(TIM2_PRESCALER_16, 1000);

 

  //Clear TIM2 update flag

  TIM2_ClearFlag(TIM2_FLAG_UPDATE);

 

  //Enable update interrupt

  TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);

  TIM2_Cmd(ENABLE);

  

  // Enable interrupt

  enableInterrupts();

}

Interrupt service function:


Since the interrupts of the library function project template in IAR are all written in the stm8s_it.c library file, this code needs to be written in the chip corresponding to the stm8s_it.c library file to perform interrupt processing.


By initializing the configured parameters and accumulating the count 1000 times, the effect of flipping the LED once every 1S is achieved. The flag bit of the timer needs to be cleared after each interrupt.


INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13)

 {

  /* In order to detect unexpected events during development,

     it is recommended to set a breakpoint on the following instruction.

  */

  static uint16_t count = 0;

  

  if(count++ == 1000)

  {

    //Flip the LED

    GPIO_WriteReverse(Led_Opt_GPIO_Port, Led_Opt_Pin);

    

    count = 0;

  }

  // Clear the flag bit of timer 2

  TIM2_ClearITPendingBit(TIM2_IT_UPDATE);

 }

 


4.4.3 TIM4


initialization:


/*******************************************************************************

* Function Name  : MX_TIM4_Init

* Description    : TIM1 Init

* Input          : None

* Output         : None

* Return         : None

********************************************************************************/

void MX_TIM4_Init(void)

{

 

  // Initialize constant 4 (128 division, count times)

  TIM4_TimeBaseInit(TIM4_PRESCALER_128, 250);

 

  //Clear TIM4 update flag

  TIM4_ClearFlag(TIM4_FLAG_UPDATE);

 

  //Enable update interrupt

  TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);

  TIM4_Cmd(ENABLE);

  

  // Enable interrupt

  enableInterrupts();

}

Interrupt service function:


Since the interrupts of the library function project template in IAR are all written in the stm8s_it.c library file, this code needs to be written in the chip corresponding to the stm8s_it.c library file to perform interrupt processing.


By initializing the configured parameters and accumulating the count 500 times, the effect of flipping the LED once every 1S is achieved. The flag bit of the timer needs to be cleared after each interruption.


INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)

 {

  /* In order to detect unexpected events during development,

     it is recommended to set a breakpoint on the following instruction.

  */

   

  static uint16_t count = 0;

  

  if(count++ == 500)

  {

    //Flip the LED

    GPIO_WriteReverse(Led_Opt_GPIO_Port, Led_Opt_Pin);

    

    count = 0;

  }

  // Clear the flag bit of timer 4

  TIM4_ClearITPendingBit(TIM4_IT_UPDATE);

  

 }

 

5. Conclusion 


  This blog only introduces how to control the LED flipping, but the function of the timer is far more than that. Please learn the details yourself.

  Relatively speaking, the timer function of STM8 is quite useful. I hope this blog can help everyone realize the TIM function.

Keywords:STM8  TIM Reference address:TIM timer operation based on STM8---STM8-Chapter 3

Previous article:IIC protocol based on STM8--Example--Clock module (DS3231) reading
Next article:ADC reading based on STM8---STM8-Chapter 4

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号