STM32 Chapter VI - Timer Comments

Timer (the Timer) is the time of the most basic function, such as the timing USART transmit data, the timing data and the like acquired AD. If the timer is used, then combine with the GPIO can be achieved very rich functionality, can measure the pulse width of the input signal, the output waveform can be produced. Production PWM motor control timer status is common in industrial control method, this knowledge is necessary insight.
STM32F4xx series controller has two advanced control timers, 10 general-purpose timers and two basic timer.
Here Insert Picture Description
Here the common timer clock frequency divided by the frequency dividing ratio determined APB1, if the pre-APB1 division ratio 1 is the clock frequency, the clock frequency is equal to the common timer APB1 is otherwise APB1 clock twice.

Clock Source

To achieve the timer must count clock source, substantially only timer clock from the internal clock, general purpose timers and advanced control can also select an external clock source or directly from the other timer standby mode . We can configure the RCC dedicated clock register (RCC_DCKCFGR) setting all timers TIMPRE bit clock frequency, we generally set to the default value of the bit 0, so that the table selectable maximum timer clock of 90MHz, i.e., the basic timer internal clock (CK_INT) frequency of 90MHz . The basic timer can only use the internal clock , and when TIM6 TIM7 control register 1 (TIMx_CR1) a CEN position 1, the basic timer starts, the clock source and prescaler is CK_INT. For advanced clock source control general purpose timers may come to an external clock controller, timers, etc. Other patterns, more complex.
Here Insert Picture Description
Use SystenInit initialization of the function, each of the clock frequency is as follows:
the SYSCLK = 72M
the AHB clock 72M =
APB1 clock = 36M
so APB1 division factor = AHB / APB1 = 2
clock frequency is thereby available CK_INT 2 * 36M = 72M.
the final frequency counter need to go through PSC prescaler calculated to give
Here Insert Picture Description

counter

Basic timer counting process involves three main register contents, which are counter register (as TIMx_CNT) , prescaler register (TIMx_PSC) , auto-reload register (in TIMx_ARR) , the three registers are 16 significant digits, i.e., value can be set from 0 to 65,535.
Here Insert Picture Description

Timer period is calculated

A timing event generated by the main time TIMx_PSC and TIMx_ARR determined two register values, i.e. the period of the timer. For example, we need a timer 1s cycle, two specific register values How to set up?
Suppose, we first set TIMx_ARR register value 9999, i.e., when counted from 0 TIMx_CNT, just equal to the generated event 9999, a total of 10,000 counts, then if the clock source at this time to obtain a period of 100us in the timing period exactly 1s.
The next question is disposed so that the register value TIMx_PSC CK_CNT output 100us period (10000Hz) clock. CK_PSC prescaler input clock is 90MHz, so that the prescaler value (9000-1) can be satisfied.

Detailed timer initialization structure

typedef struct
 {
		uint16_t TIM_Prescaler;              // 预分频器
		uint16_t TIM_CounterMode;        // 计数模式
		uint32_t TIM_Period;                   // 定时器周期
		uint16_t TIM_ClockDivision;       // 时钟分频
		uint8_t TIM_RepetitionCounter; // 重复计算器
} TIM_TimeBaseInitTypeDef;

(1) TIM_Prescaler: Timer prescaler is provided, through which the clock prescaler is the source clock timer, which sets the value of the register TIMx_PSC. May be set in the range 0-65535, 1-65536 division achieved. Why engage in a prescaler, it is because the system clock frequency is too fast, 90MHZ ah, that most people can not stand the timer so fast, so divide and let his clock frequency to the timer a little less, nothing more.
(2) TIM_CounterMode: timer counting, but for the counting up and counting down the center of three kinds of alignment modes. The basic timer is counting up only, i.e. only TIMx_CNT incremented from 0, and need not be initialized.
(3) TIM_Period: timer period, the actual value is set to automatically reload register, update to the shadow register when the event generation. It may be set in the range 0-65535.
Automatic reload register value: for example, you want to turn on the water inside the bucket, filled with water after him drained. That water over how much water needs to do? Give him a set value, drop drop drop was over 100,000, take it drained. After drained, the reset drop 10,000 drops, drained and then filled ...
(. 4) TIM_ClockDivision: clock frequency, the clock timer setting CK_INT digital filter sampling clock frequency and the frequency division ratio, this feature is not basic timer, not set.
(5) TIM_RepetitionCounter: repeat counter, registers are advanced control dedicated register bit, which can be very easily controlled using the number of PWM outputs. Here not set.

Program settings

General purpose timer setting, and generates a corresponding interrupt, divided into the following steps (in TIM3 example)

  1. TIM3 clock enable
  2. TIM3_ARR and the setting value TIM3_PSC
  3. Set TIM3_DIER allowed to update interrupt
  4. TIM3 work permit
  5. TIM3 interrupt group settings
  6. Write interrupt service routine
void TIM3_Int_Init()
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	NVIC_InitTypeDef    NVIC_InitStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
	TIM_TimeBaseStructure.TIM_Prescaler =7199;     //设置用来作为TIMx时钟频率除数的预分频值  10Khz的计数频率  
	TIM_TimeBaseStructure.TIM_Period  = 4999;        //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	 计数到5000为500ms
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;    //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 
	TIM_ITConfig(TIM3, TIM_IT_Update,ENABLE);
	
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM3中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //先占优先级0级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //从优先级3级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  //IRQ通道被使能
	NVIC_Init(&NVIC_InitStructure);     //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
	TIM_Cmd(TIM3, ENABLE);  //使能TIMx外设							 
}

You must turn the timer clock before using the timer, the basic timer belong APB1 bus peripherals. APB1 peripheral bus clock = 72M.
We set the clock automatically reload timer value register 4999 arr provided psc clock prescaler register value 7199, the driving of the counter: CK_CNT = APB1Periph / (7199 + 1) = 72M / 7200 = 10K, a time counter is equal to: 1 / CK_CNT = 0.0001s = 0.1ms = 100us, when the counter counts from 0 to 4999, the interrupt is generated, the interrupt time time: 100us 5000 0.0001s = 5000 = 0.5s = 500ms is half a second.

void TIM3_IRQHandler(void)   //TIM3中断
{
	if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 
		{
		TIM_ClearITPendingBit(TIM3, TIM_IT_Update  );  //清除TIMx的中断待处理位:TIM 中断源 
		LED1=!LED1;
		}
}

This interrupt service routine to start with if statements, TIM_GetITStatus () function to determine whether TIM3 interrupt occurs, the interrupt occurs if cleared TIM3 interrupt flag. Let LED1 lights reverse.

 int main(void)
 {	
	delay_init();	    	 //延时函数初始化
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
	LED_Init();		  	//初始化与LED连接的硬件接口
	TIM3_Int_Init();   //10Khz的计数频率,计数到5000为500ms  
   	while(1)
	{
		LED0=!LED0;
		delay_ms(200);		   
	}
}

First, the main function of delay function Initialize interrupt priority packets 2, initializes hardware interface connected to the LED, the timer 3 is initialized. So far LED0 will flip about every 0.5 seconds. At the same time we have to make the comparison in 0.2 seconds while LED1 lamp function flip it to do comparison.
Now we use keil simulation to see if is not LED0 is 0.2ms flip it, LED1 is 0.5ms flip it.
1. Configure keil simulation debugging tools.
Here Insert Picture Description
2. Open the commissioning, after entering the debugging interface, logic analysis window open, and set the PWM output pin
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
3. Click run at full speed, observe the oscilloscope
Here Insert Picture Description
Here Insert Picture Description
can clearly see LED0 is 0.5ms to flip once.
4. Another configuration LED1 same way and found to be about 0.2ms inverted, and the same configuration program.
Here Insert Picture Description
Summary: We use a timer when mainly to clear the division ratio and reload value. The two decided timed how long you want the other stm32 Should you want to use a timer, a timer function, then timed how long it needs to do the appropriate action, directly in the interrupt service function inside to write your code on the line, with a break too, to be doing something to do directly in your service function of the response on the line. There are timers and counters, timers, counters that ah. You first count, count to a certain extent overflow is not achieved timed it? So beginners do not tangle.

Published 34 original articles · won praise 22 · views 9521

Guess you like

Origin blog.csdn.net/qq_39400113/article/details/105245441