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.
Previous article:IIC protocol based on STM8--Example--Clock module (DS3231) reading
Next article:ADC reading based on STM8---STM8-Chapter 4
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- CCS 7.4.0 imports routines of PDK_c667x_2_0_9
- 11 years of experience in microwave and radio frequency manufacturing
- I need a QFN24 packaged MCU with two ADCs, two serial ports, low power consumption, in stock, GD Zhaoyi Innovation has recommendations...
- Design and implementation of high-speed real-time speech recognition system based on DSP
- What is strong current & weak current? What is the difference between the two? I didn't expect the truth to be like this! ~
- EEWORLD University Hall----Live Replay: Microchip's latest SAM and PIC32 microcontroller software development platform - MPLAB? Harmony V3 introduction
- WiFi will soon face challenges from other technologies or will be replaced
- Help, I have a smart hardware combination of STM32 and SIM800C. I have produced 100 of them and run more than 70 of them. About 7 of them have crashed.
- AD9144-FMC-EBZ ADI data transfer board four-channel digital-to-analog converter evaluation board module conversion
- A40i Platform Application Notes - Huawei - ME909S-4G Module Porting Application