Bird
Raised Fist0
Embedded Cprogramming~20 mins

PWM generation using timers in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PWM Timer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
PWM Duty Cycle Calculation Output

What is the output of the following code that calculates the PWM duty cycle percentage?

Embedded C
unsigned int timer_period = 1000;
unsigned int pulse_width = 250;
float duty_cycle = (pulse_width / (float)timer_period) * 100;
printf("Duty Cycle: %.1f%%\n", duty_cycle);
ADuty Cycle: 2.5%
BDuty Cycle: 0.0%
CDuty Cycle: 25.0%
DDuty Cycle: 250.0%
Attempts:
2 left
💡 Hint

Remember to cast integers to float before division to get a decimal result.

🧠 Conceptual
intermediate
2:00remaining
Timer Configuration for PWM Frequency

Which timer configuration will produce a PWM signal with a frequency of 1 kHz if the timer clock is 1 MHz?

ASet timer period to 1000 and prescaler to 1
BSet timer period to 100 and prescaler to 11
CSet timer period to 2000 and prescaler to 1
DSet timer period to 500 and prescaler to 3
Attempts:
2 left
💡 Hint

Frequency = timer_clock / (prescaler * period)

🔧 Debug
advanced
2:00remaining
Identify the Error in PWM Initialization Code

What error will occur when running this PWM initialization code?

Embedded C
TIM_HandleTypeDef htim;
htim.Init.Prescaler = 0;
htim.Init.Period = 0;
HAL_TIM_PWM_Init(&htim);
HAL_TIM_PWM_Start(&htim, TIM_CHANNEL_1);
ANo error, PWM starts with 0 frequency
BRuntime error: Division by zero in timer period calculation
CCompilation error: Missing semicolon
DRuntime error: Null pointer dereference
Attempts:
2 left
💡 Hint

Check the timer period value before initialization.

📝 Syntax
advanced
2:00remaining
Correct Syntax for PWM Duty Cycle Update

Which option correctly updates the PWM duty cycle to 50% for a timer with period 1000?

Embedded C
/* Assume htim and channel are properly initialized */
uint32_t new_pulse = 500; // 50% of 1000
// Update PWM pulse here
Ahtim.Instance->CCR1 = new_pulse;
Bhtim->Instance->CCR1 = new_pulse;
Chtim.Instance.CCR1 = new_pulse;
Dhtim.Instance->CCR1 = &new_pulse;
Attempts:
2 left
💡 Hint

Check the structure pointer and member access syntax.

🚀 Application
expert
2:00remaining
Calculate PWM Signal Frequency from Timer Settings

Given a timer clock of 8 MHz, prescaler set to 7, and period set to 999, what is the PWM signal frequency?

A100 Hz
B8000 Hz
C10000 Hz
D1000 Hz
Attempts:
2 left
💡 Hint

Frequency = timer_clock / ((prescaler + 1) * (period + 1))

Practice

(1/5)
1. What does PWM stand for in embedded C programming when using timers?
easy
A. Pulse Width Modulation
B. Power Wave Management
C. Programmed Write Mode
D. Peripheral Watchdog Monitor

Solution

  1. Step 1: Understand the acronym PWM

    PWM is a common term in embedded systems related to signal control.
  2. Step 2: Match PWM with its full form

    PWM stands for Pulse Width Modulation, which controls signal timing using timers.
  3. Final Answer:

    Pulse Width Modulation -> Option A
  4. Quick Check:

    PWM = Pulse Width Modulation [OK]
Hint: Remember PWM controls signal timing by pulse width [OK]
Common Mistakes:
  • Confusing PWM with power management terms
  • Thinking PWM is a mode for writing data
  • Mixing PWM with watchdog timer functions
2. Which of the following is the correct way to start a timer for PWM in embedded C?
easy
A. TIM_StartPWM();
B. HAL_TIM_PWM_Start(&htim);
C. StartTimerPWM();
D. timer_pwm_start();

Solution

  1. Step 1: Identify standard embedded C HAL function

    HAL libraries use the prefix HAL_TIM_PWM_Start for starting PWM timers.
  2. Step 2: Check syntax correctness

    HAL_TIM_PWM_Start(&htim); uses correct syntax with a timer handle pointer, matching HAL style.
  3. Final Answer:

    HAL_TIM_PWM_Start(&htim); -> Option B
  4. Quick Check:

    HAL_TIM_PWM_Start(&htim) is correct syntax [OK]
Hint: HAL functions start with HAL_ prefix and use handles [OK]
Common Mistakes:
  • Using incorrect function names without HAL prefix
  • Missing the timer handle argument
  • Using lowercase or camelCase incorrectly
3. Given the following code snippet, what is the duty cycle percentage of the PWM signal?
uint32_t period = 1000;
uint32_t pulse = 250;
// Timer configured with period and pulse
// PWM output uses pulse width = pulse
medium
A. 100%
B. 75%
C. 50%
D. 25%

Solution

  1. Step 1: Understand duty cycle formula

    Duty cycle = (pulse / period) * 100%
  2. Step 2: Calculate duty cycle with given values

    Duty cycle = (250 / 1000) * 100% = 25%
  3. Final Answer:

    25% -> Option D
  4. Quick Check:

    Duty cycle = 25% [OK]
Hint: Duty cycle = pulse divided by period times 100% [OK]
Common Mistakes:
  • Confusing pulse with period
  • Calculating 1000/250 instead of 250/1000
  • Forgetting to multiply by 100 for percentage
4. Identify the error in this PWM timer initialization code snippet:
TIM_HandleTypeDef htim;
htim.Init.Period = 1000;
htim.Init.Pulse = 500;
HAL_TIM_PWM_Start(&htim);
medium
A. Missing call to HAL_TIM_PWM_ConfigChannel before starting PWM
B. The 'Pulse' field requires pointer syntax like htim->Init.Pulse
C. Period value should be set after starting PWM
D. Timer handle 'htim' must be a pointer, not a variable

Solution

  1. Step 1: Check PWM initialization sequence

    Before starting PWM, the channel must be configured with pulse value using HAL_TIM_PWM_ConfigChannel.
  2. Step 2: Identify missing configuration call

    The code sets pulse in Init (which is incorrect) and skips HAL_TIM_PWM_ConfigChannel call.
  3. Final Answer:

    Missing call to HAL_TIM_PWM_ConfigChannel before starting PWM -> Option A
  4. Quick Check:

    ConfigChannel call needed before HAL_TIM_PWM_Start [OK]
Hint: Always configure PWM channel before starting timer [OK]
Common Mistakes:
  • Setting pulse in Init structure instead of channel config
  • Starting PWM without configuring channel
  • Assuming timer handle must be pointer variable
5. You want to generate a PWM signal with a 60% duty cycle and a frequency of 1 kHz using a timer with a 72 MHz clock. Which timer period and pulse values should you set? Assume timer prescaler is set to 71 (divides clock by 72).
hard
A. Period = 1000, Pulse = 600
B. Period = 72000, Pulse = 43200
C. Period = 999, Pulse = 600
D. Period = 71, Pulse = 43

Solution

  1. Step 1: Calculate timer frequency after prescaler

    Timer clock = 72 MHz / (71 + 1) = 1 MHz (1 microsecond per tick)
  2. Step 2: Calculate period for 1 kHz PWM frequency

    Period = (Timer frequency / PWM frequency) - 1 = (1,000,000 / 1,000) - 1 = 999
  3. Step 3: Calculate pulse for 60% duty cycle

    Pulse = Period * 0.6 = 999 * 0.6 = 599.4 ≈ 600
  4. Final Answer:

    Period = 999, Pulse = 600 -> Option C
  5. Quick Check:

    Period 999 and pulse 600 give 60% duty cycle at 1 kHz [OK]
Hint: Divide clock by prescaler, then calculate period and pulse [OK]
Common Mistakes:
  • Not adding 1 to prescaler or period calculations
  • Using full clock without prescaler division
  • Rounding pulse incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard