0% found this document useful (0 votes)
10 views26 pages

Understanding Pulse Width Modulation

Uploaded by

tekiyagaming
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views26 pages

Understanding Pulse Width Modulation

Uploaded by

tekiyagaming
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Pulse Width

Modulation

Thareendhra Keerthi Wijayasiriwardhane


Department of Industrial Management
Faculty of Science
University of Kelaniya
thareen@[Link]
Learning Outcomes
 On completion of this session, you should be able to
 Explain the need of PWM
 Compute duty cycle and frequency for PWM outputs
 Describe how Timers generate PWM outputs
 Explain the functionality of Timer/Counter registers
used for PWM
 Construct a microcontroller-based system and program it
to generate a PWM output with a varying duty cycle

2
Pulse Width Modulation
 A technique for getting an analog like output by means
of digital
 Toggles a digital output fast enough, making any
attached device to react not to the individual pulses, but
to the % of time that the output is high in one cycle

 How fast?

3
Duty Cycle
 Percentage of time that output is high in one cycle

4
Average Voltage

5
PWM
 Using 8-bit Timer

6
Waveform Generation

7
PWM Frequency
 In Non-Inverted mode, while
Timer counts, if a compare match
occurs, Waveform Generator
clears OCnX pin and sets it when
Timer comes to Bottom
 Waveform Generator generates a
one PWM cycle

8
Timer / Counter Registers
 TCCRnB (Timer/Counter Control Register n B)

 CSn2 (Clock Select), CSn1 and CSn0 bits select prescale


CSn2 CSn1 CSn0 Clock Source
0 0 0 Disconnect clock source
0 0 1 F_CPU
0 1 0 F_CPU/8
0 1 1 F_CPU/64
1 0 0 F_CPU/256
1 0 1 F_CPU/1024

9
Timer / Counter Registers
 TCCRnA (Timer/Counter Control Register n A)

 WGMn1 (Waveform Generation Mode) and WGMn0


bits with WGMn2 bit of TCCRnB specify waveform
generation mode
WGMn2 WGMn1 WGMn0 Mode
0 0 0 Normal
0 0 1 Phase Correct PWM
0 1 0 Clear Timer on Compare (CTC)
0 1 1 Fast PWM

10
Timer / Counter Registers
 TCCRnA (Timer/Counter Control Register n A)

 COMnA1 (Compare Output Mode), COMnA0,


COMnB1 and COMnB0 bits specify compare output
mode
COMnX1 COMnX0 Mode Description
Clears OCnX on compare
1 0 Non-Inverted
match & sets it at Bottom
Sets OCnX on compare
1 1 Inverted
match & clears it at Bottom

11
Timer / Counter Registers
 OCRnA (Output Compare Register n A) and OCRnB
 hold values to be compared against TCNTn register
 If 8-bit, can hold a value between 0 and 28 – 1
 If 16-bit, can hold a value between 0 and 216 – 1

12
Timer / Counter Registers
 TIFRn (Timer Interrupt Flag Register n)

 When a compare match occurs between OCRnX and


TCNTn registers, OCFnX (Output Compare match Flag
n X) flag bit is set
 Set OCFnX flag bit to clear it

13
Timer / Counter Registers
 TIMSKn (Timer Interrupt MaSK Register n)

 OCIEnA (Output Compare match Interrupt Enable n A)


and OCIEnB bits enable (or disables) Timer/Counter
compare match A and B interrupts, respectively

14
System 15
fader - Gradually fades the light of an
LED again and again

15
ATmega328P PWM Pinout

16
Hardware
 Schematic
Diagram

17
Hardware
 Wiring Diagram

18
Firmware
 Algorithm

19
C Program

20
Using Hexadecimal Constants

21
Using Labeled Constants

22
Firmware
 Algorithm for Arduino

23
Firmware
 Source Code (using Arduino Wrapper Functions)

24
True Analog Output
 Three options
 Switch to a microcontroller
with built-in DAC
 Add a DAC breakout
 Use a RC filter

25
Questions

26

Common questions

Powered by AI

To implement a fading LED effect using PWM on microcontrollers like the ATmega328P, a common strategy involves gradually changing the PWM duty cycle from 0 to maximum and back to 0 in a loop. This can be achieved programmatically by incrementing and decrementing the value in the Output Compare Register (OCRnA/OCRnB) over time, often with delays introduced between changes to control fade speed. Utilizing timer interrupts may enhance performance by allowing other processes to run concurrently, making the system more efficient and responsive .

Prescale settings are crucial in determining the PWM signal frequency as they divide the clock frequency, thereby affecting the timer count speed and ultimately the PWM frequency. The Clock Select (CSn2, CSn1, CSn0) bits in the Timer/Counter Control Register specify these prescale values, for example, dividing the clock by 1, 8, 64, 256, or 1024. Selecting a prescale determines how often the timer increments, influencing the PWM period. Lower prescale values result in a higher frequency, making them suitable for applications requiring rapid control, while higher values lower the frequency, potentially reducing power usage and smoothing out the response for applications like LED dimming .

The main challenge in using PWM signals to generate true analog outputs is their digital nature, which requires filtering to smooth out high-frequency components. A common solution involves using an RC (Resistor-Capacitor) filter to average the PWM signals into a continuous voltage. However, this method introduces limitations, such as a slower transient response and potential losses in precision due to the filter's frequency response and component tolerances. Alternatively, switching to a microcontroller with a built-in DAC or adding an external DAC provides a more straightforward and reliable generation of true analog signals, though at the expense of increased complexity and cost .

To program a microcontroller-based system to vary the duty cycle of a PWM output, developers can adjust the value in the Output Compare Register (OCRnA/OCRnB). By changing this value in a periodic manner within the main program loop or via interrupts, the PWM signal’s duty cycle can be modulated over time. This method is often used in applications requiring dynamic control, like motor speed variation or LED brightness adjustment. Implementing feedback using sensors can enable automated duty cycle adjustment according to the desired outcome in real-time applications .

The availability of a built-in DAC simplifies PWM-based system design by providing true analog output without the need for additional components, reducing complexity and potential points of failure. If a microcontroller lacks a built-in DAC, adding an external DAC can achieve similar results, allowing for a smoother and more precise control over analog devices. Both options potentially enhance signal quality compared to PWM outputs, which approximate analog signals through digital means. Additionally, a DAC output is not band-limited by filtering, allowing for broader frequency response applications compared to PWM with an RC filter .

Pulse Width Modulation (PWM) offers several advantages, including the ability to simulate an analog signal through digital means, which is crucial in systems that do not have a Digital-to-Analog Converter (DAC). It achieves efficiencies by toggling a digital output at a frequency high enough so that the output device responds to the average power delivered over time rather than each individual pulse. This approach can lead to reduced power loss, improved control over power delivery, and the ability to control devices like motors and LEDs with precision .

In non-inverted PWM mode, when a compare match occurs, the output pin (OCnX) is cleared, and it is set again when the timer count reaches the bottom. This means that the output is high until a match occurs and then goes low, creating a waveform where the proportion of the high time to the total period determines the duty cycle. Conversely, in inverted PWM mode, the output pin is set at a compare match and cleared when the timer resets, resulting in a waveform where the output is low until the match occurs and then goes high. These modes affect how power is delivered to a load, impacting its performance .

Timer/Counter registers, such as TCCRnA and TCCRnB, are utilized in microcontrollers to generate PWM signals. These registers configure the mode of operation, such as Normal, Phase Correct PWM, Clear Timer on Compare (CTC), and Fast PWM, through the bits WGMn0, WGMn1, and WGMn2. The compare output modes, defined by the COMnA1, COMnA0, COMnB1, and COMnB0 bits, determine how the output compare register value controls the output waveform's behavior during a compare match. The OCRnA and OCRnB registers hold the values compared against the TCNTn register to determine when an action like clearing or setting the output pin should occur, effectively controlling the PWM signal duty cycle .

The duty cycle of a PWM signal is calculated as the percentage of one cycle in which a signal is active, or high. It's given by the equation Duty Cycle (%) = (Time High / Total Cycle Time) * 100. The duty cycle directly influences the average voltage supplied to a load and, therefore, controls the power delivery in a PWM system. A higher duty cycle increases the average voltage and power delivered, while a lower duty cycle decreases it. This can affect performance, such as the speed of a motor or the brightness of an LED .

The Timer Interrupt Flag Register (TIFRn) plays a significant role in PWM signal generation by indicating when a compare match has occurred between the Output Compare Register (OCRnX) and the Timer/Counter Register (TCNTn). When such a match occurs, the Output Compare match Flag (OCFnX) bit is set, which can trigger an interrupt if enabled by the Timer Interrupt MaSK Register (TIMSKn). This mechanism allows for precise control over PWM signal timing, facilitating tasks like updating the duty cycle, synchronizing signals, or implementing non-blocking delays within a system .

You might also like