STM32 timer time calculation method

  

The timer in STM32 has many usages:

(1) System clock (SysTick)

The setting is very simple, the following is to generate 1ms interrupt. Set, and generate a 10ms delay function:

void RCC_Configuration(void){RCC_ClocksTypeDef RCC_ClockFreq;SystemInit();//From the system_stm32f10x.c file, you only need to call this function to complete the RCC configuration .RCC_GetClocksFreq (& RCC_ClockFreq);

//SYSTICK --1ms dividing the system clock interrupt if (SysTick_Config (SystemFrequency /1000)) {while (1); //Capture error}}

void SysTick_Handler(void)//Program in interrupt handler {while(tim){tim--;}}

//Invoke program: Delay_Ms(10);

Of course, the premise is to set up, the variable tim should be set to volatile type.

(2) The second involves timer count time (TIMx)

TIM_TimeBaseStructure.TIM_Prescaler = 2; //prescaler (clock division) 72M/(2+1) = 24MTIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //count up TIM_TimeBaseStructure.TIM_Period = 65535; //load values ​​of 18k /144 = 125hzTIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0; TIM_TimeBaseInit (TIM3, & TIM_TimeBaseStructure);

regular time calculated: TIM_TimeBaseStructure.TIM_Prescaler = 2; //divider 2 72M //2=24MHzTIM_TimeBaseStructure.TIM_Period = 65535 (2 + 1); //count value 65535 ((1 + TIM_Prescaler) /72M) * (1 +TIM_Period )=((1+2)/72M)*(1+65535)=0.00273 sec=366.2Hz */

Note two points (from prawn net, untested) (1) TIMx (1-8), in the case of the default setting of the library, it is 72M clock; (2) TIM_TimeBaseStructure.TIM_RepetitionCounter=0; is the repeat count, that is, how many times the overflow overflows gives you an overflow interrupt, its corresponding register Call TIM1 RCR. If this value is not configured, When the power is on, the register value can be random. If it is interrupted once every 1 second, it may become an N second interrupt, which makes you super big!

Assuming the system clock is 72Mhz, TIM1 is obtained by PCLK2 (72MHz). TIM2-7 is obtained by PCLK1

The key is to set the clock prescaler, the value of the auto-reload register period

Basic settings of the timer

1, TIM_TimeBaseStructure .TIM_Prescaler = 7199;//Clock prescaler: For example:

clock frequency=72/(clock prescaler+1)

2, TIM_TimeBaseStructure.TIM_Period = 9999; //The value of the auto-reload register cycle (timing

time) generates an update or interrupt after accumulating 0xFFFF frequencies (also said timing time is up)

3, TIM_TimeBaseStructure.TIM_CounterMode = TIM1_CounterMode_Up; //Timer

mode up count

4, TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //time split value

5, TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);//Initialization Timer 2

6, TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //Open interrupt overflow interrupt

7, TIM_Cmd( TIM2, ENABLE);//Open timer

or:

TIM_TimeBaseStructure.TIM_Prescaler = 35999;//Zone 35999 72M/

(35999+1)/2 = 1Hz 1 second interrupt overflow time

TIM_TimeBaseStructure.TIM_Period = 2000; //count value 2000

((1 + TIM_Prescaler) /72M) * (1 + TIM_Period) = ((1+ 35999)/72M)*(1+2000)=1 sec*/

The basic timer function of the STM32 general-purpose timer is flashing.

#include "stm32f10x.h"#include "misc.h"

void RCC_Configuration(void);void NVIC_Configuration(void);void GPIO_Configuration(void);void TIM3_Configuration(void);

int main(void){RCC_Configuration( NVIC_Configuration();GPIO_Configuration();TIM3_Configuration();

TIM_ClearFlag(TIM3, TIM_FLAG_Update);TIM_ARRPreloadConfig(TIM3, DISABLE);

TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);TIM_Cmd (TIM3, ENABLE);

while (1) {;}}

void TIM3_Configuration (void) {

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructure.TIM_Period = 9999; TIM_TimeBaseStructure.TIM_Presca ler = 7199; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit (TIM3, & TIM_TimeBaseStructure);}

void RCC_Configuration (void) {SystemInit ();

RCC_APB1PeriphClockCmd ( RCC_APB1Periph_TIM3, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

}


void NVIC_Configuration(void){NVIC_InitTypeDef NVIC_InitStructure;

NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init (& NVIC_InitStructure);}

void GPIO_Configuration (void) {GPIO_InitTypeDef GPIO_InitStructure;
; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz; GPIO_Init (GPIOC, & GPIO_InitStructure);

}

#include " stm32f10x_it .h "

void TIM3_IRQHan dler (void) {if (! TIM_GetITStatus (TIM3, TIM_IT_Update) = RESET) {

TIM_ClearITPendingBit (TIM3, TIM_IT_Update); GPIO_WriteBit (GPIOC, GPIO_Pin_7, (BitAction) (1 - GPIO_ReadOutputDataBit (GPIOC, GPIO_Pin_7)) );}}

Copyright © Windows knowledge All Rights Reserved