STM32 ordinary timer realizes delay function

Publisher:星辰古泉Latest update time:2017-01-17 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/* SystemFrequency / 1000 1ms interrupt once

 * SystemFrequency / 100000 10us interruption

 * SystemFrequency / 1000000 1us interrupt once

 */


#define SYSTICKPERIOD                    0.000001

#define SYSTICKFREQUENCY            (1/SYSTICKPERIOD)


/**

  * @brief Initialization of timer 2, timing period 1ms

  * @param None

  * @retval None

  */

void TIM2_Init(void)

{

    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;


    /*AHB = 72MHz, PPRE1 of RCC_CFGR = 2, so APB1 = 36MHz, TIM2CLK = APB1*2 = 72MHz */

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    

    /* Time base configuration */         

    TIM_TimeBaseStructure.TIM_Period = 999;

    TIM_TimeBaseStructure.TIM_Prescaler = SystemCoreClock/SYSTICKFREQUENCY -1;

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    

    TIM_ARRPreloadConfig(TIM2, ENABLE);

    

    /* Set the update request source to generate an interrupt only when the counter overflows or underflows*/

    TIM_UpdateRequestConfig(TIM2,TIM_UpdateSource_Global); 

    TIM_ClearFlag(TIM2, TIM_FLAG_Update);

}


/**

  * @brief ms delay program, 1ms as a unit

  * @param  

  * @arg nTime: Delay_ms( 10 ) The delay achieved is 10 * 1ms = 10ms

  * @retval None

  */

void Delay_ms(__IO uint32_t nTime)

{     

    /* Clear the counter and enable the tick timer */  

    TIM2->CNT   = 0;  

    TIM_Cmd(TIM2, ENABLE);     


    for( ; nTime > 0 ; nTime--)

    {

     /* Wait for the end of a delay unit*/

     while(TIM_GetFlagStatus(TIM2, TIM_FLAG_Update) != SET);

     TIM_ClearFlag(TIM2, TIM_FLAG_Update);

    }


    TIM_Cmd(TIM2, DISABLE);

}


Keywords:STM32 Reference address:STM32 ordinary timer realizes delay function

Previous article:LCD virtual display test of S3C2440
Next article:How to get the running time of microcontroller code

Recommended ReadingLatest update time:2024-11-15 07:15

STM32 low power consumption--stop mode
The shutdown mode is selected according to the project needs After entering stop mode, any external interrupt can wake up the low power consumption, but the clock needs to be reconfigured, otherwise the system will run with the default clock (without multiplication). 1. Enter low power consumption RCC_APB1Peri
[Microcontroller]
STM32 delay program, us level delay
According to predecessors, the most taboo thing in embedded system development is program delays and wasted timers, so when writing programs, we try very hard to avoid millisecond-level delays. In addition, general RTOS requires a time base, so we try to use only systick to complete the system time base, milliseconds,
[Microcontroller]
STM32 self-built engineering process based on 3.5 library MDK
Creating a library-based project from scratch first step:   Step 2: The other is to generate hex file, list file path, emulator selection. The following is listed separately because it is not set. The generated file cannot be burned. The third step is to try to compile the project. If an error occu
[Microcontroller]
STM32 self-built engineering process based on 3.5 library MDK
STM32 MCU timer debugging square wave output
Today I tried to make the STM32 timer output a 50% duty cycle signal. I wrote a square wave initialization function according to the routine. The routine used the STM32's own library function. Because it was troublesome, I wrote a simple one myself, using timer 1 for output. As a result, there was no response at the be
[Microcontroller]
STM32 Quadrature Encoder Demo
Encoder classification:  According to the working principle: photoelectric, magnetoelectric and contact brush  type According to the hole engraving method of the code disk: incremental and absolute  Since the blogger's contact surface is not very wide, he has used two types of encoders in total, both of which are phot
[Microcontroller]
STM32 Quadrature Encoder Demo
STM32 bootloader IAP programming
Many people want to upgrade the firmware of a product without disassembling it, which is not only convenient but also saves energy and cost. So how to complete this task? The Bootloader introduced below can complete this task, and complete the firmware upgrade through the Bootloader. Let's briefly analyze the STM32 Bo
[Microcontroller]
STM32 PWM output realizes LED light gradually turning on and off
/*  *illustrate:  *PA0:KEY1;PA1:KEY2;  *PA2:LED1;PA3:LED2;  *PA9:USART1_TX;PA10:USART1_RX  */   #include "stm32f10x.h"   #include "stm32f10x_rcc.h"   #include "stm32f10x_gpio.h"   #include "stm32f10x_tim.h"   #include "stm32f10x_pwr.h"   #include "stm32f10x_exti.h"   #include "system_stm32f10x.h"   #incl
[Microcontroller]
The crystal oscillator and clock stability of STM32 should be taken seriously!!!
 I have read many application cases online recently, and found that many of them have failed due to the STM32 crystal oscillator problem. I have also encountered it once myself. It was because the capacitor value was wrong. A netizen ourdev said: his device has system problems every few days and the system clock slo
[Microcontroller]
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号