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)
Previous article:STM8 MCU ADC Application (IAR)
Next article:JTAG/SWD/ISP/SWIM details the four programming methods of MCU
- 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
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Freshman asks for a simple wind tunnel control program
- Analysis of the working principles of seven triode collector DC circuits 1
- A very good introduction to the ble Bluetooth protocol stack
- A UART Circuit Design Based on FPGA
- [RVB2601 Creative Application Development] Flower Watering Indicator
- Understanding the alarm performance in arc fault detectors (fault arcs with 14 or more half cycles occurring within 1 second)
- November 19 live broadcast review: New requirements for connectors in 5G multi-scenario terminal applications and Molex's 5G connection solutions (including video)...
- DP83822I Industrial Ethernet PHY auto-negotiation function and its strap resistor configuration
- I would like to share with you the SDK for secret rooms developed based on STC microcontrollers.
- Analog IC Design and Simulation