+ All Categories
Home > Technology > Timers and pwm

Timers and pwm

Date post: 23-Jun-2015
Category:
Upload: saideep-kamishetty
View: 1,024 times
Download: 4 times
Share this document with a friend
Popular Tags:
32
PULSE WIDTH MODULATION (PWM) K.saideep
Transcript
Page 1: Timers and pwm

PULSE WIDTH MODULATION(PWM)

K.saideep

Page 2: Timers and pwm

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

Page 3: Timers and pwm

• 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)

Page 4: Timers and pwm

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

Page 5: Timers and pwm

Output pwm waveform

Page 6: Timers and pwm

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

Page 7: Timers and pwm

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

Page 8: Timers and pwm

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.

Page 9: Timers and pwm

• 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)• {• }

Page 10: Timers and pwm

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.

Page 11: Timers and pwm

Inputs and outputs of timer in pwm operation

Page 12: Timers and pwm

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

Page 13: Timers and pwm

• PWM out: The number of pwm channels are nothing but pwm output pins , whose number depend on microcontroller.

• For example ATMEGA8 has 3 channels

Page 14: Timers and pwm

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

Page 15: Timers and pwm

FAST PWM

Page 16: Timers and 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

Page 17: Timers and pwm

Difference between Input waveforms

Page 18: Timers and pwm

Input and Output waveform

Page 19: Timers and pwm

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.

Page 20: Timers and pwm

Compare output modes

• Pwm turned off• Inverted pwm• Non inverted pwm• Toggle pwm• The first mode is self explanaratory

Page 21: Timers and pwm

• 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

Page 22: Timers and pwm

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

Page 23: Timers and pwm

Registers in TIMER1

COM1A1:0 compare output mode for channel ACOM1B1:0 compare output mode for channel BThis 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 registerThe operation being performed according to the value given to bits varies with the mode of pwm we are using

Page 24: Timers and pwm

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

Page 25: Timers and pwm

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)

Page 26: Timers and pwm

Prescalar selection

Page 27: Timers and pwm

• 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

Page 28: Timers and pwm

• 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

Page 29: Timers and pwm

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 valueOCIE1B(output compare interrupt enable):similar to functioning of cell OCIE1A for TCCR1B

Page 30: Timers and pwm

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 executedOCF1B:The functionality is similar to OCF1A which is done in TCNT1 matches with OCR1B

Page 31: Timers and pwm

Output pins of timers in ATMEGA8

Page 32: Timers and pwm

Calculation of OCR1A

• Value of OCR1A=(timer frequency)/(required frequency at which interrupts are to be executed)


Recommended