stm32 runtime measurement and interval execution

Publisher:XinmeiLatest update time:2018-12-28 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

Keywords:stm32 Reference address:stm32 runtime measurement and interval execution

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

STM32 IoT TFTP file transfer
Remarks: Focus on IoT application development and share IoT technology experience. Software platform: IAR6.5 TCP/IP protocol stack: LWIP1.4.1 Hardware platform: STM32F103C8T6 wired communication board 1. TCP/IP protocol stack LWIP 1.1. LWIP Introduction        LWIP is a small open source TCP/IP protocol s
[Microcontroller]
Solution for stm32 program failing to enter main function
As shown in the figure, after entering the debugging mode, click Run (F5), there is no response, and the program stops at the red dot BX RO. Click the small yellow arrow pointing to the right next to Run F5 → to enter the assembly run view, as shown in the figure below It stays at 0x08000908 BEAB BKPT 0xAB and
[Microcontroller]
Solution for stm32 program failing to enter main function
In-depth explanation of STM32 NVIC
Friends, if you need to port RTOS on STM32, you must first have a deep understanding of its interrupt system. What is NVIC? It stands for Nested Vectored Interrupt Controller. STM32 has a powerful and convenient NVIC, which is a device belonging to the Cortex core. Non-maskable interrupts (NMI) and external interrupts
[Microcontroller]
STM32 USB Learning Notes 5
Host environment: Windows 7 SP1 Development environment: MDK5.14 Target board: STM32F103C8T6 Development library: STM32F1Cube library and STM32_USB_Device_Library Following the previous article, for the upper-layer application, only the CDC class interface file, usbd_cdc_interface, is left. This file is mainly
[Microcontroller]
STM32 USB Learning Notes 5
Using printf to send strings to the serial port in STM32
Question: When using STM32 for debugging, the serial port is often used to send information. In order to facilitate debugging and sending information through the serial port, the printf() function is used to print information through the serial port. method one: 1. Add the header file containing the printf() funct
[Microcontroller]
Causes and solutions for stm32 chip failure
.The chip 3.3v power supply voltage is unstable 2. If you have a reset circuit and it doesn't work, the chip has no soldering numbers. 3. The program of the board with low external clock configuration was downloaded to the board with high external clock. Because the multiplication and division coefficients make the sy
[Microcontroller]
Comparative analysis of the advantages and disadvantages of stm32 microcontroller and pic microcontroller
stm32 microcontroller The STM32 series of microcontrollers launched by ST are known to all industry friends as being a series of microcontrollers with the highest cost-performance ratio, and they are extremely powerful. They are based on the ARM Cortex-M core designed specifically for embedded applications that requir
[Microcontroller]
Comparative analysis of the advantages and disadvantages of stm32 microcontroller and pic microcontroller
Application of STM32 and CAN bus in temperature monitoring system
  introduction   In the field of modern industrial control, it is usually necessary to measure many signals, convert them into binary signals that can be recognized by computers, and use computers to monitor and record various measured signals. This process involves signal acquisition and processing. CAN bus is a ser
[Microcontroller]
Application of STM32 and CAN bus in temperature monitoring system
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号