SlideShare a Scribd company logo
PULSE WIDTH MODULATION
(PWM)
K.saideep
Why use pwm?
• Generally all microcontrollers are provided
with analogue to digital converter to convert
analogue values to digital values. But there is
no such thing like DAC’s in AVR’s. In such case
pwm is closest solution.
• It is used generally for light dimmer controls
and motor speed controls by varying it’s
dutycycle
• Dutycycle: It is defined as duration for which
waveform is in active state to the total time
under consideration
• Dutycycle=(t/T)
• t=time for which waveform is in active state
• T=timeperiod of waveform
• Outputvoltage=(dutycycle)*(input voltage)
Simple pwm generator
• A simple wave generator(may be triangular ,
ramp etc.) can be combined with a
comparator to generate pwm.
• By varying the value of input given to negetive
terminal of comparator waveforms of different
timeperiod are obtained.
• They are used in applications like servos which
require a pulse of time period of 2.5ms
Output pwm waveform
• Comparator used to vary only duty cycle, then
how to change time period of the output
signal?
• This is possible using timers and based on
microcontroller number of timers and pwm
channels varies and generally the value which
we give for comparison is also given through
timers.
Timer
• Timer: There are two types of timers present
in ATMEGA16 and ATMEGA8:
• 1.8bit timer-(0-255)
• 2.16 bit timer-(0-65535)
• It’s value increases at a predetermined rate
known as timerclock (given by user) which is
free from cpu’s intervention
Basic concepts of timer
• Prescalar : It is the constant with which clock
frequency is to be divided to get desired timer
frequency
• Prescalar = clock frequency/desired timer
frequency.
• Generally prescalars are available in multiples
of 2 i.e, 1,8,32,64,128,1024
• If any timer has multiple pwm channels then
they share same value of prescalar.
• Interrupt:When the timer counter reaches a
specific value then CPU stops it’s normal
functioning and executes the action which is
given in terms of interrupt
• Syntax for interrupt service routine(ISR)
• ISR(TIMER1_COMP_VECT)
• {
• }
Basic registers in timer
• In the previous waveforms the compare value
is given by OCRX(output compare register)
and top value is given by ICRX(Input capture
register) and each timer is provided separate
counters and they are designated by TCNTX
and X indicates the timer which is being used.
Inputs and outputs of timer in pwm
operation
Clock:
• This is either the speed of any external crystal you have used
or the internal clock speed of your microcontroller. When you
compile your program you normally (in AVRlib) have to set the
F_CPU variable to this value. Obviously there is only one clock
speed per microcontroller.
• Compare:
• This value is generally given as register
variable to the timer
• PWM out: The number of pwm channels are
nothing but pwm output pins , whose number
depend on microcontroller.
• For example ATMEGA8 has 3 channels
Things to be considered before pwm
operation
• Bits of accuracy
• Modes of pwm
• Compare output mode
• Modes of pwm
• 1.fast pwm
• 2.phase correct pwm
• 3.phase and frequency correct pwm
FAST PWM
Phase correct and phase and
frequency correct pwm
• There is no general difference between this
two types of pwm
• But there is difference compared to first type
as in this type a triangular waveform
generator is used which increases time period
by two times when compared to previous
method
Difference between Input waveforms
Input and Output waveform
Bits of accuracy
• This refers to possible range of values for
comparator are possible based on TOP value
• If the TOP value is 7 then it is possible to get 8
different values from 0-7 so the bits of
accuracy is 3.
Compare output modes
• Pwm turned off
• Inverted pwm
• Non inverted pwm
• Toggle pwm
• The first mode is self explanaratory
• Inverted pwm: It will set output pins high
when counter value is greater than
comparator value
• Non inverted pwm: It will set output pin low
when counter value is greater than
comparator value
• Toggle pwm: It will toggle the output pin when
counter value reaches comparator value
Timers in ATMEGA8
• TIMER0
• TIMER1:
• TCCR1A,TCCR1B
• TIMER2
• Timer0 is not used for pwm application as it
executes an interrupt when timercounter
exceeds the value of TOP value which is not
desirable in pwm motor control
Registers in TIMER1
COM1A1:0 compare output mode for channel A
COM1B1:0 compare output mode for channel B
This four cells controls the functionality output compare
pins (OC1A and OC1B) respectively.
Before using this register corresponding compare pins
should be assigned as output pins using DDR register
The operation being performed according to the value
given to bits varies with the mode of pwm we are using
• FOC1A:force output compare for channel A
• FOC1B:force output compare for channel B
• When this cells are assigned as logic one the
CPU is forced to do operations based on the
setting given according to the last four bits of
TCCR1A register. It is generally preferred to
assign them as logic zero for satisfactory
operation.
TCCR1B register
CS10:2 :-These three bits are clock select bits which are used
to select the timer frequency for the timer(i.e, prescalar)
Prescalar selection
• WGM13:0 :-This is generally used to select the
mode of operation of pwm based on which
waveform is generated by waveform
generator.
• ICNC1(Input capture noise canceller): when
this is activated the filtering of input at ICP1
takes place and due to this there is delay in
output for four cycles
• ICES1(Input capture edge select):when the cell
is assigned as one then ICR1 is made equal to
counter value at the rising edge on the pin
ICP1.
• If it is assigned as zero then the ICR1 is made
equal to counter value at the falling edge on
pin ICP1
TIMSK(Timer interrupt mask register)
OCIE1A:when this value of bit is written as one , then
only an interrupt is executed when the counter value
reaches the OCR1A value
OCIE1B(output compare interrupt enable):similar to
functioning of cell OCIE1A for TCCR1B
TIFR(Timer interrupt flag register)
OCF1A:The cell is automatically set to one when OCR1A
matches with TCNT1 and it is automatically erased when the
interrupt is executed
OCF1B:The functionality is similar to OCF1A which is done in
TCNT1 matches with OCR1B
Output pins of timers in ATMEGA8
Calculation of OCR1A
• Value of OCR1A=(timer frequency)/(required
frequency at which interrupts are to be
executed)

More Related Content

PPTX
Timer counter in arm7(lpc2148)
PPTX
Pic 18 microcontroller
DOCX
Altera flex
PPTX
PIC Microcontrollers
PPT
SPI Bus Protocol
PPT
PPTX
Interfacing technique with 8085- ADC[0808]
PPT
PIC Microcontrollers.ppt
Timer counter in arm7(lpc2148)
Pic 18 microcontroller
Altera flex
PIC Microcontrollers
SPI Bus Protocol
Interfacing technique with 8085- ADC[0808]
PIC Microcontrollers.ppt

What's hot (20)

PPT
PPTX
8051 Microcontroller ppt
PPTX
LCD Interacing with 8051
PPT
Design of cmos based ring oscillator
PDF
Introduction to Avr Microcontrollers
PPTX
Arm modes
PPSX
Lect 3 ARM PROCESSOR ARCHITECTURE
PPTX
Watch-dog Timer in LPC1768
PPTX
Module 2 ARM CORTEX M3 Instruction Set and Programming
PDF
Master slave jk flip flop.pdf
PPT
8051 microcontroller
PPTX
PIC-18 Microcontroller
PPTX
8051 MICROCONTROLLER ARCHITECTURE.pptx
PDF
Arm instruction set
PDF
Chapter 5 introduction to VHDL
PPT
Architecture of 8086 Microprocessor
PPT
8051 MICROCONTROLLER
PPT
Serial Peripheral Interface(SPI)
PPTX
PPT
Interfacing LCD with 8051 Microcontroller
8051 Microcontroller ppt
LCD Interacing with 8051
Design of cmos based ring oscillator
Introduction to Avr Microcontrollers
Arm modes
Lect 3 ARM PROCESSOR ARCHITECTURE
Watch-dog Timer in LPC1768
Module 2 ARM CORTEX M3 Instruction Set and Programming
Master slave jk flip flop.pdf
8051 microcontroller
PIC-18 Microcontroller
8051 MICROCONTROLLER ARCHITECTURE.pptx
Arm instruction set
Chapter 5 introduction to VHDL
Architecture of 8086 Microprocessor
8051 MICROCONTROLLER
Serial Peripheral Interface(SPI)
Interfacing LCD with 8051 Microcontroller
Ad

Viewers also liked (20)

PDF
Lecture 2 timers, pwm, state machine IN PIC
PPT
Introduction to Atmel's 8-bit AVR Microcontrollers
PDF
Arm assembly language by Bournemouth Unversity
PDF
Pulse width-modulation
PPTX
Elvira I. Baygildina, "Matrix frequency converter based on full-controlled IG...
PPTX
pulse width modulation pwm | Communication Systems
PDF
Speed Control of DC Motor under Varying Load Using PID Controller
PPTX
Temperature Controller with Atmega16
PPTX
Data logger
PPT
Chapter 6 edit
PDF
pulse width modulated inverter techniques
PPTX
Advanced techniques of PULSE WIDTH MODULATION.
PPTX
Sine Pulse Width Modulation Inverter
PPT
AT mega8 basics
PDF
L15 timers-counters-in-atmega328 p
PPTX
Advance Microcontroller AVR
DOC
Ccna complete notes
PPTX
555 timer ppt by vishnu
PPTX
MPPT Based Optimal Charge Controller in PV system
Lecture 2 timers, pwm, state machine IN PIC
Introduction to Atmel's 8-bit AVR Microcontrollers
Arm assembly language by Bournemouth Unversity
Pulse width-modulation
Elvira I. Baygildina, "Matrix frequency converter based on full-controlled IG...
pulse width modulation pwm | Communication Systems
Speed Control of DC Motor under Varying Load Using PID Controller
Temperature Controller with Atmega16
Data logger
Chapter 6 edit
pulse width modulated inverter techniques
Advanced techniques of PULSE WIDTH MODULATION.
Sine Pulse Width Modulation Inverter
AT mega8 basics
L15 timers-counters-in-atmega328 p
Advance Microcontroller AVR
Ccna complete notes
555 timer ppt by vishnu
MPPT Based Optimal Charge Controller in PV system
Ad

Similar to Timers and pwm (20)

PPTX
PWM in ARM LPC2148.pptx
PPTX
KTU_Microprocessor and Microcontrollers_Module2
PPTX
timer counter (1).pptx
PPTX
What isn’t told about timers in stm32 application
PDF
PWM_skgdfshkfdjkfgjsjienvdkncjdhrkshd.pdf
PPTX
AVRTIMER.pptx
PPT
8051e
PPTX
Enhanced phase width modulation in TMS320F28335D
PDF
AVR_Course_Day7 timers counters and interrupt programming
PPT
PLC PPT.ppt
PDF
Microcontroller part 3
PPTX
Lpc 1768 timers
PPTX
SOC Interconnect modified version 2019 course
PDF
PLC.pdf
PPT
Microcontroller Timer Counter Modules and applications
PPT
Evolution of Programmable Logic Controllers
PPTX
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
PPTX
U III embedded system MOTION CONTROL QEI.pptx
PPTX
digital Counter
PPTX
PWM in ARM LPC2148.pptx
KTU_Microprocessor and Microcontrollers_Module2
timer counter (1).pptx
What isn’t told about timers in stm32 application
PWM_skgdfshkfdjkfgjsjienvdkncjdhrkshd.pdf
AVRTIMER.pptx
8051e
Enhanced phase width modulation in TMS320F28335D
AVR_Course_Day7 timers counters and interrupt programming
PLC PPT.ppt
Microcontroller part 3
Lpc 1768 timers
SOC Interconnect modified version 2019 course
PLC.pdf
Microcontroller Timer Counter Modules and applications
Evolution of Programmable Logic Controllers
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
U III embedded system MOTION CONTROL QEI.pptx
digital Counter

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Spectroscopy.pptx food analysis technology
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PPTX
A Presentation on Artificial Intelligence
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPT
Teaching material agriculture food technology
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
1. Introduction to Computer Programming.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
cuic standard and advanced reporting.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Getting Started with Data Integration: FME Form 101
PDF
Empathic Computing: Creating Shared Understanding
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
MIND Revenue Release Quarter 2 2025 Press Release
Spectroscopy.pptx food analysis technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Group 1 Presentation -Planning and Decision Making .pptx
A Presentation on Artificial Intelligence
Advanced methodologies resolving dimensionality complications for autism neur...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Teaching material agriculture food technology
NewMind AI Weekly Chronicles - August'25-Week II
MYSQL Presentation for SQL database connectivity
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
1. Introduction to Computer Programming.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
cuic standard and advanced reporting.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Getting Started with Data Integration: FME Form 101
Empathic Computing: Creating Shared Understanding
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Programs and apps: productivity, graphics, security and other tools

Timers and pwm

  • 2. Why use pwm? • Generally all microcontrollers are provided with analogue to digital converter to convert analogue values to digital values. But there is no such thing like DAC’s in AVR’s. In such case pwm is closest solution. • It is used generally for light dimmer controls and motor speed controls by varying it’s dutycycle
  • 3. • Dutycycle: It is defined as duration for which waveform is in active state to the total time under consideration • Dutycycle=(t/T) • t=time for which waveform is in active state • T=timeperiod of waveform • Outputvoltage=(dutycycle)*(input voltage)
  • 4. Simple pwm generator • A simple wave generator(may be triangular , ramp etc.) can be combined with a comparator to generate pwm. • By varying the value of input given to negetive terminal of comparator waveforms of different timeperiod are obtained. • They are used in applications like servos which require a pulse of time period of 2.5ms
  • 6. • Comparator used to vary only duty cycle, then how to change time period of the output signal? • This is possible using timers and based on microcontroller number of timers and pwm channels varies and generally the value which we give for comparison is also given through timers.
  • 7. Timer • Timer: There are two types of timers present in ATMEGA16 and ATMEGA8: • 1.8bit timer-(0-255) • 2.16 bit timer-(0-65535) • It’s value increases at a predetermined rate known as timerclock (given by user) which is free from cpu’s intervention
  • 8. Basic concepts of timer • Prescalar : It is the constant with which clock frequency is to be divided to get desired timer frequency • Prescalar = clock frequency/desired timer frequency. • Generally prescalars are available in multiples of 2 i.e, 1,8,32,64,128,1024 • If any timer has multiple pwm channels then they share same value of prescalar.
  • 9. • Interrupt:When the timer counter reaches a specific value then CPU stops it’s normal functioning and executes the action which is given in terms of interrupt • Syntax for interrupt service routine(ISR) • ISR(TIMER1_COMP_VECT) • { • }
  • 10. Basic registers in timer • In the previous waveforms the compare value is given by OCRX(output compare register) and top value is given by ICRX(Input capture register) and each timer is provided separate counters and they are designated by TCNTX and X indicates the timer which is being used.
  • 11. Inputs and outputs of timer in pwm operation
  • 12. Clock: • This is either the speed of any external crystal you have used or the internal clock speed of your microcontroller. When you compile your program you normally (in AVRlib) have to set the F_CPU variable to this value. Obviously there is only one clock speed per microcontroller. • Compare: • This value is generally given as register variable to the timer
  • 13. • PWM out: The number of pwm channels are nothing but pwm output pins , whose number depend on microcontroller. • For example ATMEGA8 has 3 channels
  • 14. Things to be considered before pwm operation • Bits of accuracy • Modes of pwm • Compare output mode • Modes of pwm • 1.fast pwm • 2.phase correct pwm • 3.phase and frequency correct pwm
  • 16. Phase correct and phase and frequency correct pwm • There is no general difference between this two types of pwm • But there is difference compared to first type as in this type a triangular waveform generator is used which increases time period by two times when compared to previous method
  • 18. Input and Output waveform
  • 19. Bits of accuracy • This refers to possible range of values for comparator are possible based on TOP value • If the TOP value is 7 then it is possible to get 8 different values from 0-7 so the bits of accuracy is 3.
  • 20. Compare output modes • Pwm turned off • Inverted pwm • Non inverted pwm • Toggle pwm • The first mode is self explanaratory
  • 21. • Inverted pwm: It will set output pins high when counter value is greater than comparator value • Non inverted pwm: It will set output pin low when counter value is greater than comparator value • Toggle pwm: It will toggle the output pin when counter value reaches comparator value
  • 22. Timers in ATMEGA8 • TIMER0 • TIMER1: • TCCR1A,TCCR1B • TIMER2 • Timer0 is not used for pwm application as it executes an interrupt when timercounter exceeds the value of TOP value which is not desirable in pwm motor control
  • 23. Registers in TIMER1 COM1A1:0 compare output mode for channel A COM1B1:0 compare output mode for channel B This four cells controls the functionality output compare pins (OC1A and OC1B) respectively. Before using this register corresponding compare pins should be assigned as output pins using DDR register The operation being performed according to the value given to bits varies with the mode of pwm we are using
  • 24. • FOC1A:force output compare for channel A • FOC1B:force output compare for channel B • When this cells are assigned as logic one the CPU is forced to do operations based on the setting given according to the last four bits of TCCR1A register. It is generally preferred to assign them as logic zero for satisfactory operation.
  • 25. TCCR1B register CS10:2 :-These three bits are clock select bits which are used to select the timer frequency for the timer(i.e, prescalar)
  • 27. • WGM13:0 :-This is generally used to select the mode of operation of pwm based on which waveform is generated by waveform generator. • ICNC1(Input capture noise canceller): when this is activated the filtering of input at ICP1 takes place and due to this there is delay in output for four cycles
  • 28. • ICES1(Input capture edge select):when the cell is assigned as one then ICR1 is made equal to counter value at the rising edge on the pin ICP1. • If it is assigned as zero then the ICR1 is made equal to counter value at the falling edge on pin ICP1
  • 29. TIMSK(Timer interrupt mask register) OCIE1A:when this value of bit is written as one , then only an interrupt is executed when the counter value reaches the OCR1A value OCIE1B(output compare interrupt enable):similar to functioning of cell OCIE1A for TCCR1B
  • 30. TIFR(Timer interrupt flag register) OCF1A:The cell is automatically set to one when OCR1A matches with TCNT1 and it is automatically erased when the interrupt is executed OCF1B:The functionality is similar to OCF1A which is done in TCNT1 matches with OCR1B
  • 31. Output pins of timers in ATMEGA8
  • 32. Calculation of OCR1A • Value of OCR1A=(timer frequency)/(required frequency at which interrupts are to be executed)