0
0
Embedded Cprogramming~10 mins

PWM generation using timers in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - PWM generation using timers
Configure Timer Prescaler and Period
Set Timer Mode to PWM
Set PWM Duty Cycle
Start Timer
Timer Counts Up
Compare Counter with Duty Cycle
Counter < Duty
Output HIGH
Repeat Counting
PWM Signal
The timer is set up with a prescaler and period, configured for PWM mode, then the duty cycle is set. The timer counts up, and the output pin is set HIGH or LOW depending on the counter compared to the duty cycle, creating a PWM signal.
Execution Sample
Embedded C
TIM_HandleTypeDef htim2;

void PWM_Init() {
  htim2.Init.Prescaler = 79;
  htim2.Init.Period = 999;
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
}

void Set_Duty(uint16_t duty) {
  __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, duty);
}
This code initializes a timer for PWM with a prescaler and period, starts PWM on channel 1, and sets the duty cycle by changing the compare value.
Execution Table
StepTimer Counter (CNT)Duty Cycle (CCR)Condition (CNT < CCR)Output PinAction
10500TrueHIGHTimer starts counting from 0, output HIGH
2100500TrueHIGHCounter less than duty, output stays HIGH
3499500TrueHIGHCounter still less than duty, output HIGH
4500500FalseLOWCounter equals duty, output switches LOW
5700500FalseLOWCounter greater than duty, output LOW
6999500FalseLOWCounter reaches period, output LOW
70500TrueHIGHCounter resets to 0, output HIGH again
ExitCounter resets after reaching PeriodPWM cycle repeats continuously
💡 Timer counter resets after reaching the period, PWM signal repeats
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 7Final
CNT (Timer Counter)005000Repeats 0 to 999
CCR (Duty Cycle)500500500500Constant at 500
Output PinLOWHIGHLOWHIGHToggles based on CNT vs CCR
Key Moments - 3 Insights
Why does the output pin go LOW when the counter equals the duty cycle?
Because in the execution_table at step 4, when CNT equals CCR, the condition CNT < CCR becomes false, so the output switches from HIGH to LOW to create the PWM off time.
What happens when the timer counter reaches the period value?
At step 6 and 7 in the execution_table, the counter resets to 0 after reaching the period, restarting the PWM cycle and setting the output HIGH again.
Why is the duty cycle value constant during the PWM cycle?
The duty cycle (CCR) is set once before the timer starts and remains constant during counting, as shown in variable_tracker, controlling how long the output stays HIGH each cycle.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output pin state at step 3 when CNT is 499 and CCR is 500?
AHIGH
BLOW
CUndefined
DToggling
💡 Hint
Check the 'Output Pin' column at step 3 in the execution_table
At which step does the timer counter reset to 0?
AStep 4
BStep 7
CStep 6
DStep 5
💡 Hint
Look at the 'Timer Counter (CNT)' column and the 'Action' description in the execution_table
If the duty cycle (CCR) is increased to 800, how would the output pin behave between CNT 500 and 700?
AOutput switches LOW at CNT 800
BOutput switches LOW at CNT 500
COutput stays HIGH
DOutput stays LOW
💡 Hint
Refer to the condition CNT < CCR in the execution_table and how output depends on it
Concept Snapshot
PWM generation uses a timer counting from 0 to a set period.
Output pin is HIGH while counter < duty cycle (compare value).
When counter >= duty cycle, output goes LOW.
Timer resets at period, repeating PWM cycle.
Duty cycle controls pulse width (HIGH time).
Prescaler and period set PWM frequency.
Full Transcript
PWM generation using timers works by configuring a timer with a prescaler and period to set the PWM frequency. The timer counts up from zero to the period value repeatedly. A compare register holds the duty cycle value. While the timer counter is less than the duty cycle, the output pin is set HIGH, creating the ON part of the pulse. When the counter reaches or exceeds the duty cycle, the output pin goes LOW, creating the OFF part. This cycle repeats continuously, producing a PWM signal with a pulse width proportional to the duty cycle. The example code initializes the timer, starts PWM, and sets the duty cycle by updating the compare register. The execution table shows the timer counter increasing, output pin switching states based on the counter compared to the duty cycle, and the counter resetting after reaching the period. Key moments clarify why the output changes state and how the timer resets. The visual quiz tests understanding of output states and timer behavior. This method is common in embedded systems to control motors, LEDs, and other devices by varying power with PWM.