Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Timers and pwm


Published on

Published in: Technology, Business

Timers and pwm

  2. 2. Why use pwm?• Generally all microcontrollers are providedwith analogue to digital converter to convertanalogue values to digital values. But there isno such thing like DAC’s in AVR’s. In such casepwm is closest solution.• It is used generally for light dimmer controlsand motor speed controls by varying it’sdutycycle
  3. 3. • Dutycycle: It is defined as duration for whichwaveform is in active state to the total timeunder consideration• Dutycycle=(t/T)• t=time for which waveform is in active state• T=timeperiod of waveform• Outputvoltage=(dutycycle)*(input voltage)
  4. 4. Simple pwm generator• A simple wave generator(may be triangular ,ramp etc.) can be combined with acomparator to generate pwm.• By varying the value of input given to negetiveterminal of comparator waveforms of differenttimeperiod are obtained.• They are used in applications like servos whichrequire a pulse of time period of 2.5ms
  5. 5. Output pwm waveform
  6. 6. • Comparator used to vary only duty cycle, thenhow to change time period of the outputsignal?• This is possible using timers and based onmicrocontroller number of timers and pwmchannels varies and generally the value whichwe give for comparison is also given throughtimers.
  7. 7. Timer• Timer: There are two types of timers presentin ATMEGA16 and ATMEGA8:• 1.8bit timer-(0-255)• 2.16 bit timer-(0-65535)• It’s value increases at a predetermined rateknown as timerclock (given by user) which isfree from cpu’s intervention
  8. 8. Basic concepts of timer• Prescalar : It is the constant with which clockfrequency is to be divided to get desired timerfrequency• Prescalar = clock frequency/desired timerfrequency.• Generally prescalars are available in multiplesof 2 i.e, 1,8,32,64,128,1024• If any timer has multiple pwm channels thenthey share same value of prescalar.
  9. 9. • Interrupt:When the timer counter reaches aspecific value then CPU stops it’s normalfunctioning and executes the action which isgiven in terms of interrupt• Syntax for interrupt service routine(ISR)• ISR(TIMER1_COMP_VECT)• {• }
  10. 10. Basic registers in timer• In the previous waveforms the compare valueis given by OCRX(output compare register)and top value is given by ICRX(Input captureregister) and each timer is provided separatecounters and they are designated by TCNTXand X indicates the timer which is being used.
  11. 11. Inputs and outputs of timer in pwmoperation
  12. 12. Clock:• This is either the speed of any external crystal you have usedor the internal clock speed of your microcontroller. When youcompile your program you normally (in AVRlib) have to set theF_CPU variable to this value. Obviously there is only one clockspeed per microcontroller.• Compare:• This value is generally given as registervariable to the timer
  13. 13. • PWM out: The number of pwm channels arenothing but pwm output pins , whose numberdepend on microcontroller.• For example ATMEGA8 has 3 channels
  14. 14. Things to be considered before pwmoperation• Bits of accuracy• Modes of pwm• Compare output mode• Modes of pwm• pwm• 2.phase correct pwm• 3.phase and frequency correct pwm
  15. 15. FAST PWM
  16. 16. Phase correct and phase andfrequency correct pwm• There is no general difference between thistwo types of pwm• But there is difference compared to first typeas in this type a triangular waveformgenerator is used which increases time periodby two times when compared to previousmethod
  17. 17. Difference between Input waveforms
  18. 18. Input and Output waveform
  19. 19. Bits of accuracy• This refers to possible range of values forcomparator are possible based on TOP value• If the TOP value is 7 then it is possible to get 8different values from 0-7 so the bits ofaccuracy is 3.
  20. 20. Compare output modes• Pwm turned off• Inverted pwm• Non inverted pwm• Toggle pwm• The first mode is self explanaratory
  21. 21. • Inverted pwm: It will set output pins highwhen counter value is greater thancomparator value• Non inverted pwm: It will set output pin lowwhen counter value is greater thancomparator value• Toggle pwm: It will toggle the output pin whencounter value reaches comparator value
  22. 22. Timers in ATMEGA8• TIMER0• TIMER1:• TCCR1A,TCCR1B• TIMER2• Timer0 is not used for pwm application as itexecutes an interrupt when timercounterexceeds the value of TOP value which is notdesirable in pwm motor control
  23. 23. Registers in TIMER1COM1A1:0 compare output mode for channel ACOM1B1:0 compare output mode for channel BThis four cells controls the functionality output comparepins (OC1A and OC1B) respectively.Before using this register corresponding compare pinsshould be assigned as output pins using DDR registerThe operation being performed according to the valuegiven to bits varies with the mode of pwm we are using
  24. 24. • FOC1A:force output compare for channel A• FOC1B:force output compare for channel B• When this cells are assigned as logic one theCPU is forced to do operations based on thesetting given according to the last four bits ofTCCR1A register. It is generally preferred toassign them as logic zero for satisfactoryoperation.
  25. 25. TCCR1B registerCS10:2 :-These three bits are clock select bits which are usedto select the timer frequency for the timer(i.e, prescalar)
  26. 26. Prescalar selection
  27. 27. • WGM13:0 :-This is generally used to select themode of operation of pwm based on whichwaveform is generated by waveformgenerator.• ICNC1(Input capture noise canceller): whenthis is activated the filtering of input at ICP1takes place and due to this there is delay inoutput for four cycles
  28. 28. • ICES1(Input capture edge select):when the cellis assigned as one then ICR1 is made equal tocounter value at the rising edge on the pinICP1.• If it is assigned as zero then the ICR1 is madeequal to counter value at the falling edge onpin ICP1
  29. 29. TIMSK(Timer interrupt mask register)OCIE1A:when this value of bit is written as one , thenonly an interrupt is executed when the counter valuereaches the OCR1A valueOCIE1B(output compare interrupt enable):similar tofunctioning of cell OCIE1A for TCCR1B
  30. 30. TIFR(Timer interrupt flag register)OCF1A:The cell is automatically set to one when OCR1Amatches with TCNT1 and it is automatically erased when theinterrupt is executedOCF1B:The functionality is similar to OCF1A which is done inTCNT1 matches with OCR1B
  31. 31. Output pins of timers in ATMEGA8
  32. 32. Calculation of OCR1A• Value of OCR1A=(timer frequency)/(requiredfrequency at which interrupts are to beexecuted)