Professional Documents
Culture Documents
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
6-Aug-11
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
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)
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
Code wizard
12
6-Aug-11
Code wizard
13
6-Aug-11
Code wizard
Mode of operation
14
6-Aug-11
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
18
6-Aug-11
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
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
26
6-Aug-11
27
6-Aug-11
Thank you
28
6-Aug-11