"STM32 Getting Started" TIM output comparison

Introduction to output comparison

  • Output comparison English writing OC (Output Compare) 
  • The output comparison can set the output level high or low or reverse the operation by comparing the relationship between the CNT and CCR register values, and is used to output PWM waveforms with a certain frequency and duty cycle.
  • Common application examples such as: breathing lights, speed control motors, etc.
  • CCR (abbreviation for Capture/Compare Register, and capture/compare register) 

Logical relationship between CNT and CCR

  • CNT counts up automatically, and CCR is a value we give

Introduction to PWM 

  • Pulse Width Modulation Pulse Width Modulation
  • In a system with inertia, the required analog parameters can be equivalently obtained by modulating the width of a series of pulses, which is often used in the field of motor speed control
  • PWM parameters:
  • frequency = \frac{1}{T_{s}} 
  • duty cycle = \frac{T_{on}}{T_{s}}
  • Resolution = duty cycle change step size

How to understand what is said above: PWM can be used to obtain analog parameters equivalently?

In our usual LED lighting experiments, we can only set 1 or 0 for the LED, that is, turn it on or off. But when we turn on and off at a high frequency, turn on and off, turn on and off..., the LED will show a medium brightness, and the specific degree of brightness depends on the duty cycle of the bright time.

In the same way: we continuously power on and off the motor, power on and off, power on and off...then the speed of the motor can be maintained at a medium speed.

The timer output compare module outputs PWM

According to the picture above, we focus on the output mode controller module. Its input is the size relationship between CNT and CCR, and the output is the high and low levels of oc1ref.

There are many output comparison modes in the output mode controller. Generally speaking, the most commonly used and the one to be introduced here is the PWM mode.

 The specific selection of mode 1 or mode 2, up counting or down counting can be configured in the code. These two modes can output waveforms with adjustable frequency and duty cycle.


Next, we will focus on the basic structure of PWM (this figure corresponds to PWM mode 1)

We first focus on the graph in the upper right corner, where the yellow line represents the value of ARR, the blue line represents the value of CNT, and the red line represents the value of CCR

CNT starts to increase from 0, and increases to ARR, which is 99, and then clears to 0, and then continues to increase...the cycle continues

It can be seen that we set the CCR to 30, when the blue line is below the red line, the output is high; when the blue line is above the red line, the output is low, corresponding to the execution logic of PWM mode 1.

Here we find that: when we set the value of CCR high, the time of high level will be extended, and the duty cycle will increase. The phenomenon corresponding to our two examples is that the lights become brighter and the motor rotates faster.

REF in the flowchart refers to a PWM waveform with adjustable frequency and adjustable duty cycle. Finally, after polarity selection and output enable, it leads to the GPIO port.

Parameter calculation 

PWM frequency: Freq = CK_PSC / (PSC + 1) / (ARR + 1) 

PWM duty cycle: Duty = CCR / (ARR + 1) 

PWM resolution: Reso = 1 / (ARR + 1) 

Where CK_PSC is the system frequency 72MHz, PSC is the prescaler coefficient

Due to the needs of personal projects, here is a supplementary introduction to the DC motor and its drive, right as a note. 

DC motor and its drive

motor (left)

Drive circuit (right) 

  • A DC motor has two poles. When the poles are positively connected, the motor rotates forward; reversely, the motor rotates in reverse.
  • The DC motor is a high-power device, and the GPIO port cannot be directly driven, so it needs to be operated with a driving circuit
  • Check the table to control the motor to rotate forward or reverse

Guess you like

Origin blog.csdn.net/m0_54689021/article/details/130032602