session6_timerrevision&pwm

Upload: sayan-aich

Post on 04-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Session6_TimerRevision&PWM

    1/28

    AVR Course

    Ashraf Darwish

    Bassam Tarek

  • 7/30/2019 Session6_TimerRevision&PWM

    2/28

    6-Aug-112

    Session 6

    Timers revision& PWM

  • 7/30/2019 Session6_TimerRevision&PWM

    3/28

    6-Aug-113

    Timers revision

    We have 3 timers/counters in Atmega 16

    2 8-Bit timer/counter Timer/counter0 &Timer/counter2

    can count from 0x00 to 0xFF

    1 16-Bit timer/counter Timer/counter1

  • 7/30/2019 Session6_TimerRevision&PWM

    4/28

  • 7/30/2019 Session6_TimerRevision&PWM

    5/28

    6-Aug-115

    Modes

    Normal mode CTC mode

    From 0x00 (or initialvalue set by user) to

    0xFF

    Overflow flag(TOV0,TOV1,TOV2) is

    set when timer/counterreach to maximumvalue 0xFF (timer0 &timer2) or 0xFFFF(timer1).

    From 0x00 (or initial valueset by user) to certain value

    also set by user.

    Output compare matchflag (OCF0, OCF1,OCF2) isset when timer reach the

    compare match value

  • 7/30/2019 Session6_TimerRevision&PWM

    6/28

    6-Aug-116

    Modes

    Normal mode CTC mode

    Timer overflow interruptcan be enabled when

    TOIE0 bit=1 (TIMSKregister)

    Timer compare matchinterrupt can be enabled

    when OCIE0 bit =1(TIMSK register)

    We can choose theoutput of the(OC0,OC1,OC2) PINs to be(logic1, logic0, toggle ) atcompare match

  • 7/30/2019 Session6_TimerRevision&PWM

    7/28

    6-Aug-117

    Registers

    1- TCNT0: Timer/Counter0

    This register holds the value of timer/counter.We can use this register to:

    read the value of the timer at any time.

    Put an initial value for the timer to start from iteither in normal mode or CTC mode.

  • 7/30/2019 Session6_TimerRevision&PWM

    8/28

    6-Aug-118

    Registers

    2- OCR0: output compare register 0

    Used only in CTC mode.

    Holds the value of compare match ( the endvalue of the timer/counter in CTC mode)

  • 7/30/2019 Session6_TimerRevision&PWM

    9/28

    6-Aug-119

    Registers

    3- TCCR0: Timer/counter control register 0

    To adjust:The mode (Normal,CTC,PWM,Phase correction PWM)

    The prescaler value (1, 8, 64, 256,1024)

    Timer output (OC0,OC1,OC2) in case of CTC mode

  • 7/30/2019 Session6_TimerRevision&PWM

    10/28

    6-Aug-1110

    Registers

    4- TIFR: Timer Interrupt Flag Register 0

    Holds:Timer overflow interrupt flag ( TOV0, TOV1, TOV2 ) in case ofnormal mode flag is set when timer reach0xff

    Timer Output compare match Flag ( OCF0, OCF1,OCF2)in case ofCTC mode flag is set when timer reach thecompare match value

  • 7/30/2019 Session6_TimerRevision&PWM

    11/28

    6-Aug-1111

    Registers

    5- TIMSK: Timer Interrupt mask register

    To: enable or disable timer interrupts.

    if we want to enable timer overflow interrupt,

    TOIE0=1

    If we want to enable compare match interrupt,OCIE0=1

  • 7/30/2019 Session6_TimerRevision&PWM

    12/28

    6-Aug-1112

    Code wizard

    Over flow interrupt enable

    Compare match interrupt

    enable

    Timer initial value

    Compare match value

  • 7/30/2019 Session6_TimerRevision&PWM

    13/28

    6-Aug-1113

    Code wizard

    Clock value of the timer after

    prescaling

  • 7/30/2019 Session6_TimerRevision&PWM

    14/28

    6-Aug-1114

    Code wizard

    Mode of operation

  • 7/30/2019 Session6_TimerRevision&PWM

    15/28

    6-Aug-1115

    Pulse Width Modulation(PWM)

  • 7/30/2019 Session6_TimerRevision&PWM

    16/28

    6-Aug-1116

    PWMWhat is PWM ?

    PWM represents Pulse Width Modulation we can

    also call it DAC (Digital To Analog Converter).

    PWM is a very efficient way to provideintermediate amounts of electrical powerbetween fully and fully off.

  • 7/30/2019 Session6_TimerRevision&PWM

    17/28

    6-Aug-1117

    When we have a motor (DC motor) and we want to make

    control on it.

    we can use a variable resistance connected in series

    with the motor to control the current . But it is inefficient

    way because of losses.

    Another way is to use a variable power supply. But it is

    expensive way.

    PWM is one of the most efficient way used to switch

    between fully and fully off to make a good control on DCmotor.

  • 7/30/2019 Session6_TimerRevision&PWM

    18/28

    6-Aug-1118

    Square wave and duty cycle

    Any square wave has its duty cycle which can beconstant along the time or variable.

    Duty cycle is the percentage between the period

    that the wave is high and the periodic time.

  • 7/30/2019 Session6_TimerRevision&PWM

    19/28

    6-Aug-1119

    To generate PWM signal we need:1- Timer

    2- Comparator

  • 7/30/2019 Session6_TimerRevision&PWM

    20/28

    6-Aug-1120

    As mentioned before, the AVR Timers has 4modes:

    1- Normal mode.2- CTC mode.3- Fast PWM mode.4- Phase correction mode.

    We will talk about third and fourth modes ( PWMmodes )

  • 7/30/2019 Session6_TimerRevision&PWM

    21/28

    6-Aug-1121

    Fast PWM mode:

    Timer counts from

    bottom to themaximum then restartsto bottom(Single slope operation)

    Phase correction mode:

    The timer counts frombottom to maximum andthen counts from maximum tobottom (Dual slopeoperation).

    PWM

  • 7/30/2019 Session6_TimerRevision&PWM

    22/28

    6-Aug-1122

  • 7/30/2019 Session6_TimerRevision&PWM

    23/28

    6-Aug-1123

    The basic idea of PWM is if we change the value of

    OCR (output compare register).

    In CTC mode the value of OCR is constant

  • 7/30/2019 Session6_TimerRevision&PWM

    24/28

    6-Aug-1124

  • 7/30/2019 Session6_TimerRevision&PWM

    25/28

    6-Aug-1125

    Applications:

    1-power controlwe can use PWM to control the delivered power to aload.

    2-Voltage regulationSwitching voltage to load with appropriate dutycycle.

    3-Audio effectsPWM sometimes used in audio effects.

  • 7/30/2019 Session6_TimerRevision&PWM

    26/28

    6-Aug-1126

    Code Wizard

    Choose clock value

    Fast PWM mode

    Select the output shape

  • 7/30/2019 Session6_TimerRevision&PWM

    27/28

    6-Aug-1127

  • 7/30/2019 Session6_TimerRevision&PWM

    28/28

    6-Aug-1128

    Thank you