What does PWM stand for and what is its main purpose in embedded systems?
PWM stands for Pulse Width Modulation. It is used to control the amount of power delivered to a device by switching the signal on and off rapidly, adjusting the width of the 'on' time.
Click to reveal answer
beginner
How do timers help in generating PWM signals?
Timers count clock pulses and can be configured to toggle output pins at specific counts, creating a PWM signal by controlling the duty cycle and frequency.
Click to reveal answer
beginner
What is 'duty cycle' in PWM?
Duty cycle is the percentage of one period in which the signal is 'on'. For example, a 50% duty cycle means the signal is on half the time and off half the time.
Click to reveal answer
intermediate
In embedded C, which timer register is typically used to set the PWM duty cycle?
The Output Compare Register (OCR) is used to set the duty cycle by defining when the timer toggles the output pin within the timer period.
Click to reveal answer
beginner
What happens if you set the duty cycle to 0% or 100% in PWM?
At 0% duty cycle, the output is always off (no signal). At 100% duty cycle, the output is always on (constant signal).
Click to reveal answer
What does the timer's Output Compare Register (OCR) control in PWM generation?
AThe duty cycle of the PWM signal
BThe frequency of the clock
CThe power supply voltage
DThe input signal level
✗ Incorrect
The OCR sets the point in the timer count when the output pin changes state, controlling the PWM duty cycle.
If you want a PWM signal with a 25% duty cycle, how long is the signal ON compared to the total period?
A25% of the period
B50% of the period
C75% of the period
D100% of the period
✗ Incorrect
Duty cycle is the fraction of the period the signal is ON, so 25% means ON for 25% of the total period.
Which timer mode is commonly used to generate PWM signals?
ASleep mode
BNormal counting mode
CInput capture mode
DClear Timer on Compare Match (CTC) mode
✗ Incorrect
CTC mode resets the timer on a compare match, allowing precise control of PWM frequency and duty cycle.
What is the effect of increasing the PWM frequency while keeping the duty cycle constant?
AThe output voltage increases
BThe duty cycle changes
CThe signal switches ON and OFF faster
DThe timer stops
✗ Incorrect
Increasing frequency means the signal cycles faster, switching ON and OFF more times per second.
In embedded C, which hardware component is essential for PWM generation?
AUART module
BTimer/Counter peripheral
CADC (Analog to Digital Converter)
DEEPROM memory
✗ Incorrect
Timers or counters are used to generate PWM by counting clock pulses and toggling output pins.
Explain how a timer is configured to generate a PWM signal in embedded C.
Think about how the timer counts and toggles the output pin to create the PWM waveform.
You got /5 concepts.
Describe the relationship between PWM frequency, duty cycle, and the timer registers involved.
Consider how the timer counts up to a value and when it changes the output pin state.
You got /4 concepts.
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
Step 1: Understand the acronym PWM
PWM is a common term in embedded systems related to signal control.
Step 2: Match PWM with its full form
PWM stands for Pulse Width Modulation, which controls signal timing using timers.
Final Answer:
Pulse Width Modulation -> Option A
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
Step 1: Identify standard embedded C HAL function
HAL libraries use the prefix HAL_TIM_PWM_Start for starting PWM timers.
Step 2: Check syntax correctness
HAL_TIM_PWM_Start(&htim); uses correct syntax with a timer handle pointer, matching HAL style.
Final Answer:
HAL_TIM_PWM_Start(&htim); -> Option B
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
Step 1: Understand duty cycle formula
Duty cycle = (pulse / period) * 100%
Step 2: Calculate duty cycle with given values
Duty cycle = (250 / 1000) * 100% = 25%
Final Answer:
25% -> Option D
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:
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
Step 1: Check PWM initialization sequence
Before starting PWM, the channel must be configured with pulse value using HAL_TIM_PWM_ConfigChannel.
Step 2: Identify missing configuration call
The code sets pulse in Init (which is incorrect) and skips HAL_TIM_PWM_ConfigChannel call.
Final Answer:
Missing call to HAL_TIM_PWM_ConfigChannel before starting PWM -> Option A
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).