0% found this document useful (0 votes)
35 views17 pages

Lab8 Timer (Con)

The document provides an overview of Pulse Width Modulation (PWM) and its application in embedded programming, specifically using timers in the STM32F401RE microcontroller. It explains key concepts such as duty cycle, PWM frequency, and the configuration of timers to generate PWM signals. Additionally, it includes an exercise to create a PWM signal to control an LED's brightness using specific timer settings and GPIO configurations.

Uploaded by

Phạm Nhất
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views17 pages

Lab8 Timer (Con)

The document provides an overview of Pulse Width Modulation (PWM) and its application in embedded programming, specifically using timers in the STM32F401RE microcontroller. It explains key concepts such as duty cycle, PWM frequency, and the configuration of timers to generate PWM signals. Additionally, it includes an exercise to create a PWM signal to control an LED's brightness using specific timer settings and GPIO configurations.

Uploaded by

Phạm Nhất
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Lab 8: Pulse width modulation

Introduction to embedded programming course


Introduction to PWM
Pulse Width Modulation (PWM) is a technique used to
create a varying analog signal level using a digital
output. It achieves this by switching the output on and off
at a high frequency, effectively creating a square wave,
where the duty cycle controls the average voltage level.

Duty cycle: The duty cycle is the percentage of time the


signal is in the "high" or "on" state during one cycle of
the PWM waveform.

PWM frequency: the rate at which the signal completes


one cycle of "on" and "off.“

Average output voltage or power: proportional to the duty cycle.


Basic structure of timer

From TIM2 -> TIM5,


each timer has 4 channel

How these registers work in How to configure output to


PWM mode? generate PWM signal?
PWM signal
Example of edge-aligned PWM waveform (upcounting)

TIMx_PSC: prescaler value (to devive frequency)


TIMx_ARR: auto – reload value (counter will reloaded with this
value
TIMx_CNT: current value of counter
TIMx_CCRy: stores value to be compared with current value of
counter (duty cycle of PWM signal)
OCxREF:
- OCxREF is the output compare reference signal. It is
generated by the timer and used to drive the actual PWM
output.
- OCxREF is high as long as TIMx_CNT <TIMx_CCRx else it
becomes low.
- If TIMx_CCRx > TIMx_ARR) then OCxREF is held at ‘1.
- If the TIMx_CCRx = 0 then OCxREF is held at ‘0.
When OCxREF is high, the output signal is at a high level.
When OCxREF is low, the output signal is at a low level.

CCxIF is the compare capture interrupt flag. This flag is set by


the timer when the counter value equals the compare register
value (CCR), can be used to trigger interrupt signal.
PWM
PWM signal
Example of edge-aligned PWM waveform (upcounting)

TIMx_PSC: prescaler value (to devive frequency)


TIMx_RCC: auto – reload value (counter will reloaded with this
value
TIMx_CNT: current value of counter
TIMx_CCRy: stores value to be compared with current value of
counter (duty cycle of PWM signal)
OCxREF:
How to configure Edge-aligned or center-aligned PWM?
How to configure output signal is high or low when TIMx_CNT
<TIMx_CCRx?
Enable output for TIMx channel y?
Enable counter?

Configure for a port pin to be used as TIMx channel y?


Basic registers
Basic registers
How to configure Edge-aligned or center-aligned PWM? Enable counter?
Basic registers
How to configure output signal is high or low when TIMx_CNT <TIMx_CCRx?
Basic registers
Enable output for TIMx channel y?
Basic registers
Configure for a port pin to be used as TIMx channel y?
Step 1: Configure operation mode of a port in is alternate function
Basic registers
Configure for a port pin to be used as TIMx channel y?
Alternate function of a port pin supports multiple peripherals -> choose timer
Step 2: Choose TIMx channel y in alternate function of a specific port pin

Look at Table 9, page 45 in


STM32F401RE datasheet for
more information.
Basic registers
Configure for a port pin to be used as TIMx channel y?
Alternate function of a port pin supports multiple peripherals -> choose timer
Step 2: Choose TIMx channel y in alternate function of a specific port pin
Exercise
Exercise: Create PWM signal on PA5 pin (connected to green led) of STM32F401RE to turn on and
turn off led gradually.
- Using TIM2 to create PWM signal on PA5.
- Using TIM3 to create a delay function void delay_ms(int ms) to create a short delay between different
levels of PWM signal (correspond to different duty cycles) in order to see more clearly the effect of
LED gradually turn on and turning off.
Structure of program:
Exercise
#include "stm32f401xe.h"

void GPIO_Init(void);
void TIM2_PWM_Init(void);
void delay_ms(uint32_t ms);

int main(void) {

// Initialize GPIO and Timer 2 for PWM


GPIO_Init();
TIM2_PWM_Init();

// Infinite loop to gradually fade LED


while (1) {
// Gradually increase the duty cycle
for (int dutyCycle = 0; dutyCycle <= 1000; dutyCycle += 10) {
TIM2->CCR1 = ; // Set duty cycle (compare value)
delay_ms(10); // Delay 10 ms for smooth fading
}

// Gradually decrease the duty cycle


for (int dutyCycle = 1000; dutyCycle >= 0; dutyCycle -= 10) {
; // Set duty cycle (compare value)
delay_ms(10); // Delay 10 ms for smooth fading
}
}
}
Exercise
void GPIO_Init(void) {

; // Enable GPIOA clock (using RCC->AHB1ENR register)

; // Set mode for PA5 to alternate function (using GPIOA->MODER register)

; // Set alternate function for PA5 to AF1 (TIM2_CH1) (using GPIOA->AFR[0] register)
}

void TIM2_PWM_Init(void) {

; //Enable Timer 2 clock (using RCC->APB1ENR register)

TIM2->PSC = 16 - 1; // Set prescaler (using TIM2->PSC register)

; // Set auto-reload value for 1 kHz PWM frequency (1ms period) (using TIM2->ARR register)

; // Set output compare register for initial duty cycle (0%) (using TIM2->CCR1 register)

; // Configure PWM mode 1 on TIM2 Channel 1 (using TIM2->CCMR1 register)

; // Enable output for TIM2 Channel 1 (using TIM2->CCER register)

; // Enable the counter (using TIM2->CR1 register)


}
Exercise
void delay_ms(uint32_t ms) {

; // Enable Timer 3 clock (using RCC->APB1ENR register)


; // Set prescaler to get 1 ms tick (16 MHz / 16000 = 1 kHz) (using TIM3->PSC register)
; // Set auto-reload value for desired ms delay (using TIM3->ARR register)
; // Enable update event generation and counter (using TIM3->CR1 register)

// Poll the update event flag


while (!(TIM3->SR & 0x1)); // Wait until the event update flag (UIF) is set
; // Clear the event update flag (using TIM3->SR register)
}

You might also like