This program uses the stm32 ordinary timer to form the execution time measurement function and the interval execution function. The usage is as follows. The two functions are reused and selected by #define
//#define StopWatch in execution time test mode
//use:
#include "Runtime.h"
Runtime_init();
while(1){
Runtime_start();
delay_ms(1);
Runtime_stop();
delay_ms(1000);
}
//setInterval mode#define setInterval
setInterval(fun,1000); //Input callback, period 1.024*1000ms
c file
#include "Runtime.h"
unsigned int nTime = 0;
void Runtime_init(void) {
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStu;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); //Enable TIM6 clock
#if defined(StopWatch)
/*Basic settings, the maximum measurable time is set to 8.192ms*/
TIM_TimeBaseStructure.TIM_Period =
65536 - 1; // arr is enlarged to achieve the maximum measurement range
#endif
#if defined(setIntervalMODE)
/*1.024ms overflow*/
TIM_TimeBaseStructure.TIM_Period =
8192 - 1; // arr is enlarged to achieve the maximum measurement range
#endif
TIM_TimeBaseStructure.TIM_Prescaler = 9 - 1; //Prescaler
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //Count up
TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE); //Enable TIM6 interrupt
TIM_Cmd(TIM6, ENABLE); //Enable timer 6
NVIC_InitStu.NVIC_IRQChannel = TIM6_IRQn; //External interrupt line, timer 6
NVIC_InitStu.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStu.NVIC_IRQChannelPreemptionPriority = 1; //Preemption priority
NVIC_InitStu.NVIC_IRQChannelSubPriority = 1; //Subpriority
NVIC_Init(&NVIC_InitStu);
}
#if defined(StopWatch)
void TIM6_IRQHandler(void) {
//Judge whether it is the update interrupt of timer 6
if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET) {
nTime++;
//Note to clear the interrupt flag
TIM_ClearITPendingBit(TIM6, TIM_IT_Update);
}
}
void Runtime_start(void) {
nTime = 0; //clear times
TIM_SetCounter(TIM6, 0); //Clear the CNT of the timer
}
void Runtime_stop(void) {
unsigned int count = TIM6->CNT; // TIM_GetCounter(TIM6);
TIM_ITConfig(TIM6, TIM_IT_Update, DISABLE); //Disable TIM6 interrupt
printf("run time:%f us %f ms\n", (float)count / 8 + 8192 * nTime,
(float)count / 8000 + 8.192 * nTime);
TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE); //Enable TIM6 interrupt
}
#endif
#if defined(setIntervalMODE)
callbackType callback = NULL;
unsigned int intervalTime=0;
//Initialize and set setInterval
void setInterval(callbackType cb,unsigned int time){
callback=cb;
intervalTime=time;
Runtime_init();
}
void TIM6_IRQHandler(void) {
//Judge whether it is the update interrupt of timer 6
if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET) {
nTime++;
if(nTime>=intervalTime){
TIM_ITConfig(TIM6, TIM_IT_Update, DISABLE); //Disable TIM6 interrupt
nTime=0;
//Logic that needs to be executed regularly
if(callback){
callback();
}
TIM_SetCounter(TIM6, 0); //Clear the CNT of the timer
TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE); //Enable TIM6 interrupt
}
//Note to clear the interrupt flag
TIM_ClearITPendingBit(TIM6, TIM_IT_Update);
}
}
#endif
h file
#ifndef __RUNTIME__
#define __RUNTIME__
////////MODE SET/////////
//#define StopWatch
#define setIntervalMODE
/////////////////////////
#include "sys.h"
#include "usart.h"
typedef void (*callbackType)(void);
void Runtime_init(void); //initialization
#if defined(StopWatch)
void Runtime_start(void); //Start execution time test
void Runtime_stop(void); //End the execution time test and print the result to the serial port
#endif
#if defined(setIntervalMODE)
void setInterval(callbackType cb,unsigned int time); //Initialize and set setInterval
#endif
#endif
Previous article:Wireless receiving decoding program based on STM32 and EV1527
Next article:STM32: Basic timer details
Recommended ReadingLatest update time:2024-11-16 14:58
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Qinheng benefits are here, evaluation boards are given away! Choose from three models: CH549, CH559, and CH554 for free!
- How should I find the development board schematics and PCBs of major manufacturers?
- EEWORLD University ---- Wireless Power 101
- TI mmWave Radar MIMO (2TX4RX) Setup
- [STM32MP157C-EV1] After the first unboxing and evaluation, I will run rtt on it and share other
- MSP430 Program Library---Button
- EEWORLD University ---- Live Replay: How Littelfuse Improves the Safety and Reliability of Electronic Equipment in Smart Buildings in the Internet of Things Era
- [NXP Rapid IoT Review] + Kit Modification-External Lithium Battery
- 【Zero Knowledge ESP8266】HTTP WEB Server Example
- 【K210 Series】4. MicroPython is still not perfect