Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the distinction between Output Compare and Pulse-Width Modulation functions?

I am brushing up on microcontrollers, using the STM32 series. (Specifically using the STM32F072BDISCOVERY board).

I am having some trouble understanding the use of timers and their various modes. Specifically, there are a lot of options for "Output Compare" (OC) modes, and other options for "PWM" modes. The RM0091 Reference manual is written as if they are two distinct, alternate modes of operation. I am working with the new HAL drivers as well, and there are different APIs to use OC vs PWM.

However, it looks to me like PWM is basically a subset/use case of the Output Compare capability, where the timer channel is configured to directly drive a hardware output.

Is there a firm distinction between OC and PWM modes that I am missing? In what way is PWM waveform generation not an Output Compare function?

like image 513
mbmcavoy Avatar asked Sep 02 '14 19:09

mbmcavoy


People also ask

What is pulse width modulation output?

Pulse width modulation (PWM) is a modulation technique that generates variable-width pulses to represent the amplitude of an analog input signal. The output switching transistor is on more of the time for a high-amplitude signal and off more of the time for a low-amplitude signal.

What does an output compare do?

Output compare is the ability to trigger an output based on a timestamp in memory, without interrupting the execution of code by a processor or microcontroller. This is a functionality provided by many embedded systems.

What is input capture and output compare?

You are right partly, the input capture is used to capture a timer value (the timer runs continuously). So, in the interrupt service routine is not need a counter. The output compare is used to compare the selected time base value and it also can be used for generating pulses at a set time and a specified length.

What is pulse width modulation and how is it different from duty cycle?

A PWM signal is a method for creating digital pulses to control analog circuits. There are two primary components that define a PWM signal's behavior: Duty cycle: A duty cycle is the fraction of one period when a system or signal is active. We typically express a duty cycle as a ratio or percentage.


1 Answers

The question is old but I was wondering the same and started digging into the topic.

In an STM32 micro, not all timers implement every function. My post is based on the STM32F030 Timer 1, which is I believe the most feature packed 4 channel implementation (or at least it was a few years ago).

The timer basically acts as a counter, with various options to where the counter clock comes from, what resets it, what the period is and the counter direction. This is the base for all extra features it implements, the HAL driver refers to it as Time Base. By itself, the counter function is not related to the timer channels.

There are 1-6 channels implemented in STM32 timers. These channels can be configured either independently, or in some case, in pairs (for functions like quadrature encoder mode). A channel can be configured as either input capture, or output compare. The input capture "listens to" some event and saves the counter of the time base in the CCRx registers. The output compare compares the counter register to a set value, given in the CCRx registers.

All the simple IC/OC modes and the extra functions, like PWM input, output, Hall effect sensor interfacing, are built on top of these two modes, their respective option bits act on the various input/output multiplexers and, regarding to OC mode basically tell the hardware what action to take based on the comparator outputs (CNT = CCRx, CNT > CCRx). In this case, PWM mode lets the output mode controller return to the CNT <= CCRx state when the counter register resets (more specifically, the update event), where as in the other OC modes the output mode controller ignores this signal and can be reset by hand, or by an external signal. The output signal is the OCxREF signal which then passes through some more hardware before it gets to the output pins. If it gets output at all, because you are allowed to not connect the timer to the output pins.

The STM32 timers are complicated. They have many logical blocks and a ton of configuration registers/bits so I may have missed something or completely misread something. Please feel free to correct me.

like image 133
Buga Dániel Avatar answered Oct 19 '22 23:10

Buga Dániel