0
0
Embedded Cprogramming~20 mins

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

Choose your learning style9 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))