STM32--Basic Timer && General Timer

1. Timer overview

Timers are divided into basic timers, general timers, and advanced timers.

For example: STM32F10x series contains 4 general-purpose timers (TIM2 ~ TIM5 but STM32F103Rx series only have 3 general-purpose timers (TIM2 ~ TIM4)). These general-purpose timers are completely independent and do not share any resources. Each timer contains a 16-bit counter (calculation range: 0~65535), and these timers can divide the input clock source through the pre-allocator (frequency division is to reduce the counting frequency) to meet Longer timing requirements. In addition to basic timing functions, general-purpose timers also have functions such as input capture (measure the pulse length of the input signal) and output comparison (generate output waveform).

2. Feature comparison

  Basic timer Universal timer
counter 16-bit auto-reload in increments counter 16-bit auto-reload increment, decrement or increment and decrement counter
Prescaler Divide the input clock source
Auto reload counter The maximum value of the counting cycle of the counter (determines the number of counts of the counter in a working cycle
                                                                                                    The above three constitute the timer time base unit
Clock source Internal clock source CK_INT

Internal clock source CK_INT

Internal trigger input (timer cascade)

External clock mode 1 (edge ​​detection signal from TIx, used for Hall motor speed measurement)

External clock mode 2 (source external trigger input ETR)

Features

Timing delay

Trigger DAC drive circuit to provide drive clock for DAC

Generate an interrupt or DMA request

Timing, delay

Input capture, output comparison, PWM, single pulse output (only output one cycle)

Generate an interrupt or DMA request

Support Hall sensor circuit for motor speed detection and other applications. 

aisle Single channel Four channels
Generate an interrupt or DMA request condition Update event Update events, trigger events, input capture, and output comparison

 

 

3. Block diagram comparison and explanation

1) Basic timer

Write the counting period (that is, the maximum value of the counter) to the automatic reloading counter, and write the frequency division coefficient to the prescaler counter (how long is the number of times to count). The counter starts counting from 0 (assuming to count up). When the count reaches the maximum value we write, an update event and interrupt or DMA request will be generated.

Why is there an update event? For programmers, we can access the upper register, which is inaccessible to the CPU. The CPU can access the shadow register. Therefore, we need to associate the upper register with the shadow register. The function of the update event is to write the values ​​of the automatic reload counter and the prescaler counter into the shadow register to achieve the purpose of being accessible by the CPU. Of course, the shadow register must be updated every time an update event is generated.

2) General purpose timer

All in the map

 

4. General-purpose timer clock source

  1. Internal clock (CK_INT): The input clock source of the timer comes from the internal system bus clock of the chip with a frequency of 72M.
  2. External clock mode 1: The input clock source of the timer comes from the edge input signal (x: TI1FP1, TI2FP2) on the external input pin (TIx). Hall sensor circuit detects motor speed
  3. External clock mode 2: The input clock source of the timer comes from the external input signal on the timer external trigger pin (TIMx_ETR) on the external trigger input (ETR).
  4. Internal trigger input (ITRx): The input clock source of the timer comes from the internal output (TRGO) of a timer to the input clock signal of ITRx (x: ITR0~3). Timer cascade

5. General-purpose timer output comparison

Output comparison workflow: first output the value written in the comparison register CCRx (similar to the reload value), CNT starts counting, and compares the CNT value with the CCRx value ==> output valid level, invalid level, level inversion, etc. When the value of CNT is greater than CCRx, the corresponding interrupt and flag bit will be generated.

1) Forced output

The output of the general-purpose timer is directly set by the software to force the output comparison signal (OxRef and OCx) to an effective level, an invalid level or the output remains unchanged. No need to consider any comparison result between capture/compare register (CCRx) and counter (CNT)

2) Comparison output

The output result of the comparison output mode is related to the comparison result between the capture/compare register (CCRx) and the counter (CNT). When these two values ​​are equal, the output result is an effective level, an invalid level or a level inversion. If the value of the counter (CNT) is greater than or equal to the value in the capture/compare register (CCRx), a corresponding flag or interrupt will be generated. The output comparison mode can generate different frequencies in the four channels. CCRx determines the initial phase

3) PWM output

The PWM output mode is a special case of output comparison. The output result is affected by the comparison value between the capture/compare register (CCRx) and the counter (CNT). The output result is valid and invalid. The frequency generated by the four channels of the pwm mode on the corresponding timer is synchronized

6. General timer configuration process

  1. Clock enable
  2. Configure GPIO port
  3. Configure the timer common counter module: timer clock source (internal, external, trigger), counting method (add, subtract, first add and then subtract), counting frequency (prescaler), cycle or not, counting cycle (reload value) Wait.
  4. Configure timing PWM comparison output mode: PWM mode, specific effective level, channel working mode, duty cycle, etc.
  5. Enable timer output and enable timer module.

7. Code

The control of the steering gear generally requires a time base pulse of about 20Ms. The high-level part of the pulse is generally the angle control pulse part in the range of 0.5Ms~2.5Ms, with a total interval of 2Ms.

Taking the 180° steering gear as an example, the corresponding control relationship is as follows:

0.5Ms————0°              1.0Ms————45°               1.5Ms————90°            2.0Ms————135°        2.5Ms————180°

The 360° steering gear is different from other ordinary steering gears. It is actually equivalent to a stepless speed reduction motor, which can control the speed and direction, but it does not have the function of 0-360° angle control. The control direction is the same as the control signal of a general steering gear:

0.5Ms————Maximum rotation speed in forward direction 1.5Ms————Speed ​​is 0 2.5Ms————Maximum rotation speed in reverse direction

//PB0 -- pwm--TIM3_CH3---20ms时基---72000-1分频---arr:20000-1 == 20ms
//不分频:定时1s需要计数器加72*10^9
//72000-1分频:  定时1s需要计数器加1*10^6   减一:计数器时钟频率 = f_ckpsc / (psc[15:0]+1)
//20ms就是计数器计数:20000-1              减一:如果想计数100次则写99,因为99加一次就是0刚好100
//	PWM_Init1(20000-1,72);
void PWM_Init1(u16 arr,u16 psc)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef   TIM_OCInitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIO口,相当于GPIOB_Pin_初始化
	
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	//置定时器时钟(CK_INT)频率与数字滤波器(ETR,TIx)使用的采样频率之间的分频比例的(与输入捕获相关),0表示滤波器的频率和定时器的频率是一样的。
	TIM_TimeBaseStructure.TIM_Period = arr;
	TIM_TimeBaseStructure.TIM_Prescaler = psc * 1000 - 1;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;   
	//TIM_RepetitionCounter  是再次重装载值,正常情况下当计数溢出后清零进入中断,该寄存器配置1后,就是计数器溢出两次才进入中断
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);//初始化定时器
	
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//CNT < CCRX 输出有效电平
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // 开启OC*输出到对应引脚
	TIM_OCInitStructure.TIM_Pulse = 0;  // TIM3->CCR32 = TIM_OCInitStruct->TIM_Pulse = TIM_SetCompare3(TIM3,800); 此值决定电机转速
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//输出极性:有效电平是高电平

	/***********高级定时器********************/
//	TIM_OCInitStructure.TIM_OutputNState =  TIM_OutputNState_Enable;          // 互补输出使能。开启OC*N输出到对应的引脚  	
//	TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;      //互补输出极性     高  
//  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;       //在空闲状态下的TIM输出比较引脚的状态。仅供TIM1和TIM8有效     高  
//  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;    //在空闲时互补输出 低 

	TIM_OC3Init(TIM3,&TIM_OCInitStructure);//TIM3通道3初始化
	TIM_ARRPreloadConfig(TIM3,ENABLE);  //初始化预装载值。
	TIM_Cmd(TIM3, ENABLE);  //使能TIM3定时器
}

 

 

Guess you like

Origin blog.csdn.net/qq_45604814/article/details/114208810