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.
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
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