You are on page 1of 28

AVR Course

Ashraf Darwish Bassam Tarek

Session 6 Timers revision & PWM


2 6-Aug-11

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

6-Aug-11

Each can work as


Counter Counts externally by user or events occurrence EX: Garage gate External button connected to T0,T1 or T2 pin of the IC

Timer Counts automatically EX: clock, delay

6-Aug-11

Modes
Normal mode From 0x00 (or initial value set by user) to 0xFF Overflow flag (TOV0,TOV1,TOV2) is set when timer/counter reach to maximum value 0xFF (timer0 & timer2) or 0xFFFF (timer1). 5 6-Aug-11 CTC mode From 0x00 (or initial value set by user) to certain value also set by user.

Output compare match flag (OCF0, OCF1,OCF2) is set when timer reach the compare match value

Modes
Normal mode Timer overflow interrupt can be enabled when TOIE0 bit=1 (TIMSK register) CTC mode Timer compare match interrupt can be enabled when OCIE0 bit =1 (TIMSK register) We can choose the output of the (OC0,OC1,OC2) PINs to be (logic1, logic0, toggle ) at compare match
6 6-Aug-11

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 it either in normal mode or CTC mode.
7 6-Aug-11

Registers
2- OCR0: output compare register 0

Used only in CTC mode.


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

6-Aug-11

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

6-Aug-11

Registers
4- TIFR: Timer Interrupt Flag Register 0

Holds: Timer overflow interrupt flag ( TOV0, TOV1, TOV2 ) in case of normal mode flag is set when timer reach 0xff Timer Output compare match Flag ( OCF0, OCF1, OCF2) in case of CTC mode flag is set when timer reach the compare match value
10 6-Aug-11

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
11 6-Aug-11

Code wizard

Over flow interrupt enable Compare match interrupt enable

Timer initial value

Compare match value

12

6-Aug-11

Code wizard

Clock value of the timer after prescaling

13

6-Aug-11

Code wizard

Mode of operation

14

6-Aug-11

Pulse Width Modulation (PWM)

15

6-Aug-11

PWM
What is PWM ? PWM represents Pulse Width Modulation we can also call it DAC (Digital To Analog Converter).

PWM is a very efficient way to provide intermediate amounts of electrical power between fully and fully off.

16

6-Aug-11

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

17

6-Aug-11

Square wave and duty cycle


Any square wave has its duty cycle which can be constant along the time or variable. Duty cycle is the percentage between the period that the wave is high and the periodic time.

18

6-Aug-11

To generate PWM signal we need: 1- Timer 2- Comparator

19

6-Aug-11

As mentioned before, the AVR Timers has 4 modes: 1- Normal mode. 2- CTC mode. 3- Fast PWM mode. 4- Phase correction mode. We will talk about third and fourth modes ( PWM modes )

20

6-Aug-11

PWM
Fast PWM mode: Timer counts from bottom to the maximum then restarts to bottom (Single slope operation) Phase correction mode: The timer counts from bottom to maximum and then counts from maximum to bottom (Dual slope operation).

21

6-Aug-11

22

6-Aug-11

In CTC mode the value of OCR is constant

The basic idea of PWM is if we change the value of OCR (output compare register).

23

6-Aug-11

24

6-Aug-11

Applications: 1-power control we can use PWM to control the delivered power to a load. 2-Voltage regulation Switching voltage to load with appropriate duty cycle. 3-Audio effects PWM sometimes used in audio effects.

25

6-Aug-11

Code Wizard
Choose clock value

Fast PWM mode


Select the output shape

26

6-Aug-11

27

6-Aug-11

Thank you

28

6-Aug-11

You might also like