AVR Timers
AVR Timers
AVR Timers
com
D.BALAKRISHNA,
Research Assistant, IIIT-H
CHAPTER 1:
1:- Introduction.
m
co
Timers:-
Timers:-
n.
These timers are used to provide accurate delays. Secondly, it can be used to repeat
io
or initiate an action after/at a known period of time. This feature is very commonly
at
used in several applications. An example could be setting up an alarm which triggers
at a point of time or after a period of time.
uc
ed
Counters:-
Counters:-
hi
The counters are used to count number of events it has occurred. We connect the
external event source to the clock pin of the counter register. Then, when an event
ks
occurs externally, the content of the register counter increments. In this way the
a
One thing about ATMEGA16 timers is that they can run parallel to the micro
w
chips on ATMEGA 16 micro controller chip which can run independent of the
w
main program, interacting via the control and counter registers, and the timer
interrupts. Timers can be configured to produce outputs directly to pre-determined
pins, reducing the processing load on ATMEGA16 micro controller.
1
www.sakshieducation.com
www.sakshieducation.com
Timer Resolution:-
Resolution:-
The smallest amount of time that a timer can measure is determined by the
m
frequency of the clock source which the microcontroller uses. For example, if the
co
microcontroller uses a 16MHz crystal as the clock source, then the smallest time it
can measure is 1/16000000th of a second. Therefore if we measure 40 timer periods
n.
we would be covering a time of 1/400000 sec.
io
How the time delays can be generated using Timers?
First method:-
method:-
at
uc
When we want to generate time delays, we connect the oscillator to the clock pin of
ed
the counter. So, when the oscillator ticks the content of the counter is incremented.
As a result, the content of the counter register represents how many ticks have
hi
occurred from the time we have cleared the counter. Since the speed of the
ks
oscillator in a microcontroller is known, we can calculate the tick period, and from
the content of the counter register we will know how much time has elapsed.
a
.s
So, one way to generate a time delay is to clear the counter at the start time and wait
w
until
until the counter reaches a certain number.
w
For Example:-
Example:-
w
2
www.sakshieducation.com
www.sakshieducation.com
flag for each of the counters. The flag is set when the counter overflows, and it is
cleared by software.
Second method:-
method:-
The second method to generate a time delay is to load the counter register and wait
until the counter overflows and the flag is set.
m
For Example:-
Example:-
co
In a microcontroller with a frequency of 1 MHz, with an 8-bit counter register, if we
n.
want a time delay of 3 microseconds, we can load the counter register with 0xFD and
io
wait until the flag is set after 3 ticks. After the first tick, the content of the register
increments to 0xFE; after the second tick it becomes 0xFF and after the third tick it
at
overflows and flag is set. The content of register is 00.
uc
The ATMEGA16 micro controllers provide both 8bit and 16 bit timers and
ed
important. So in case of 8 bit counter, roll over occurs when the count reaches to
ks
255. In case of 16 bit counter, roll over occurs when the count reaches to 65535.
a
• The results in the timer register can be read for further use in the application.
w
w
3
www.sakshieducation.com
www.sakshieducation.com
Oscillator
Counter register
m
External source
co
n.
Flag
Timers / counters
io
at
uc
Figure 1.1:
1.1:-
.1:-General view of timers and counters
The clock source can be internal or external. If the clock source is from internal the
ed
frequency of the crystal oscillator is fed into the timer. So, the timer/counter is called
hi
timer. By choosing the external clock option the pulses can be fed into the timer so
that it can act as a counter002E
a ks
.s
w
w
w
4
www.sakshieducation.com
www.sakshieducation.com
m
M
co
E
G
n.
io
0C1B--PD4
A
at
0C1A—PD5 1
18
ICP1---PD6
uc
19 6 21 PD7—0C2
Figure 1.2:-
.2:-Pins Related To Timer Concept
ed
• ICP1 = Input Capture Pin: The PD6 pin can act as an Input Capture pin for
w
Timer/Counter1.
w
5
www.sakshieducation.com
www.sakshieducation.com
CHAPTER 2:
2: Timers/Counters.
Timers/Counters.
Lesson 1 Introduction
m
Timer0 8 Bit Timer. 0-255
Timer1 16Bit Timer. 0-65535
co
Timer2 8Bit Timer. 0-255
n.
Depending upon the processor type the capacity of the registers varies.
io
There are some basic timer registers, for the control of timers and to know the status
of timers.
Topic 2
at Timer Registers
uc
The basic timer registers for Timer 0 and Timer1 and Timer2 are,
ed
Timer(x)
OCR(x) registers. Output compare register Contents of OCR(x)
w
1. TCNT(x) registers:-
registers:-
6
www.sakshieducation.com
www.sakshieducation.com
m
register.
co
Example:-
Example:-
n.
io
TCNT0 Register (8 Bit):-
Bit):-
at
7bit 6bit 5 bit
uc 4 bit 3 bit 2 bit 1 bit 0 bit
Count value can be stored from 0 to 255 for TIMER 0 and TIMER 2
ed
(8 bit timers).
Count value can be stored from 0 to 65535 for TIMER 1(16 bit
hi
timers).
ks
3. TCCRN Regi
Register:-
ster:- (Timer/Counter Control Register)
w
• Normal mode.
• CTC mode.
• PWM mode.
i) This register is used to set the modes of operation of the timer.
7
www.sakshieducation.com
www.sakshieducation.com
ii) This register is used to select the clock source with selected prescalar
value.
iii) This register is used to control the waveform generated by timer.
m
4. OCRx Register:
Register:- Output Compare Register
co
The contents of output compare register (OCRx) are compared with the
n.
TCNTx. When they are equal the OCFx will be set.
io
Lesson 3 Timer Concepts
at
uc
We the know the basic relation,
ed
hi
ks
Example:-
Example:-
a
1/10ms = 100 Hz. Now let’s assume that we have an external crystal XTAL of 4
w
MHz Hence, the CPU clock frequency is 4 MHz now, as I said that the timer counts
w
from 0 to TOP. For an 8-bit timer, it counts from 0 to 255 whereas for a 16-bit timer
w
it counts from 0 to 65535. After that, they overflow. This value changes at every
clock pulse.
Let’s say the timer’s value is zero now. To go from 0 to 1, it takes one clock pulse.
To go from 1 to 2, it takes another clock pu
pulse.
lse. To go from 2 to 3, it takes one more
8
www.sakshieducation.com
www.sakshieducation.com
clock pulse. And so on. For F_CPU = 4 MHz, time period T = 1/4M = 0.00025
msec. Thus for every transition (0 to 1, 1 to 2, etc), it takes only 0.00025 ms!
m
following formula:
co
n.
io
Substitute Required Delay = 10 ms and Clock Time Period = 0.00025 ms, and you
at
get Timer Count = 39999. Can you imagine that? The clock has already ticked
uc
39999 times to give a delay of only 10 ms!
ed
Now, to achieve this, we definitely cannot use an 8-bit timer (as it has an upper limit
of 255, after which it overflows). Hence, we use a 16-bit timer (which is capable of
hi
The Prescalar:-
Prescalar:-
a
.s
Assuming F_CPU = 4 MHz and a 16-bit timer (MAX = 65535), and substituting in
w
the above formula, we can get a maximum delay of 16.384 ms. Now what if we need
w
Well hopefully, there lies a solution to this. Suppose if we decrease the F_CPU from
4 MHz to 0.5 MHz (i.e. 500 kHz), then the clock time period increases to 1/500k =
0.002 ms. Now if we substitute Required Delay = 20 ms and Clock Time Period =
0.002 ms, we get Timer Count = 9999. As we can see, this can easily be achieved
9
www.sakshieducation.com
www.sakshieducation.com
m
frequency and use it. There is a provision to do so in AVR by setting some bits
which we will discuss later.
co
But don’t think that you can use prescalar freely. It comes at a cost. There is a trade-
trade-
n.
off between resolution and duration.
duration As you must have seen above, the overall
io
duration of measurement has increased from a mere 16.384 ms to 131.072 ms. so
at
has the resolution. The resolution has also increased from 0.00025 ms to 0.002
ms.This means each tick will take 0.002 ms. So, what’s the problem with this? The
uc
problem is that the accuracy has decreased. Earlier, you were able to measure
ed
duration like 0.1125 ms accurately (0.1125/0.00025 = 450), but now you cannot
(0.1125/0.002 = 56.25). The new timer can measure 0.112 ms and then 0.114 ms.
hi
How to choos
choose
hoose type of Timer according to the delay required:-
required:-
w
Let’s take an example. We need a delay of 184 ms (I have chosen any random
w
number). We have F_CPU = 4 MHz the AVR offers us the following prescalar
w
values to choose from: 8, 64, 256 and 1024. A prescalar of 8 means the effective
clock frequency will be F_CPU/8. Now substituting each of these values into the
above formula, we get different values of timer value. The results are summarized as
below:
10
www.sakshieducation.com
www.sakshieducation.com
m
1024 3906.25hz 717.25
co
Now out of these four prescalar, 8 cannot be used as the timer value exceeds the
n.
limit of 65535. Also, since the timer always takes up integer values, we cannot
io
choose 1024 as the timer count is a decimal digit. Hence, we see that prescalar
at
values of 64 and 256 are feasible. But out of these two, we choose 64 as it provides
us with greater resolution. We can choose 256 if we need the timer for a greater
uc
duration elsewhere.
ed
Thus, we always choose prescalar which gives the counter value within the feasible
hi
limit (255 or 65535) and the counter value should always be an integer.
ks
• If we select the prescalar value 64 then the count value to be stored in the
a
timer register is 11499. So, here in this case 8 bit timer cannot be used and
.s
only the choice in ATmega 16 timer registers is 16 bit registers. So, therefore
we should consider timer register which has only 16 bit capacity that is Timer
w
• According to the above example considered the 8 bit register cannot be taken
w
11
www.sakshieducation.com
www.sakshieducation.com
Timer
Timer 0:-
0:-
Features:-
Features:-
m
Main features are:-
co
• Single Compare Unit Counter.
• Clear Timer on Compare Match (Auto Reload).
n.
• Glitch-free, Phase Correct Pulse Width Modulator (PWM).
io
• Frequency Generator.
at
• External Event Counter.
uc
• 10-bit Clock Prescalar.
• Overflow and Compare Match Interrupt Sources (TOV0 and OCF0).
ed
registers.
ks
TCNT0 Register:
Bit 7 6 5 4 3 2 1 0
w
w
Initial 0 0 0 0 0 0 0 0
value
Figure 2.4.1:
.4.1: TCNT0 Register
12
www.sakshieducation.com
www.sakshieducation.com
Now we know where the counter value lies. But this register won’t be activated
unless we activate the timer! Thus we need to set the timer up by using Timer
Counter Control Register.
m
TCCR0 Register:
Register:-
co
The Timer/Counter Control Register, shown in figure below
n.
Bit 7 6 5 4 3 2 1 0
FO W CO CO WG CS0 CS CS0
io
C0 GM M0 M00 M01 2 01 0
at
00 1
Read W R/W R/W R/W R/W R/W R/W R/W
uc
/write
Initial 0 0 0 0 0 0 0 0
ed
value
hi
Figure 4.4.2:
4.4.2: TCCR0 Register
ks
Clock
Clock Select Bits (CS 02: 00):-
00):-
a
Used to set the timer up by choosing proper prescalar. The possible combinations
.s
13
www.sakshieducation.com
www.sakshieducation.com
m
co
Figure4.4.3
Figure4.4.3:
4.4.3: TCCR0 Prescalar Definitions
Example code:
code:-
n.
TCCR0 |= (1 << CS00); // Initializing the counter in No precaling.
precaling.
io
TCCR0 |= (1 << CS02)|(1 << CS00); // set up timer with prescalar = 1024.
at
Bit 6, 3 – Wgm01:00 – Wave Generation Mode:-
Mode
uc
We choose the type of wave mode from here as follows
ed
correct
2 1 0 CTC OCR0 Immediate MAX
w
Figure2
Figure2.4.4:
.4.4: - Wave Generation Mode Bit Description
14
www.sakshieducation.com
www.sakshieducation.com
Non-PWM,
Phase Correct PWM mode and
Fast PWM mode.
m
0 1 Toggle OC0 on compare match
co
1 0 Clear OC0 on compare match
1 1 Set OC0 on compare match
n.
Figure
Figure 2.4.5:
.4.5: Compare Output Mode, non-
non-PWM
io
Bit 7 – FOC0 – Force Output Compare:-
Compare:-
When set to ‘1’
at
uc
Forces an immediate compare match and affects the
ed
be set to ‘0’.
a
OCR0 Register:-
Register:-
.s
Bit 7 6 5 4 3 2 1 0
w
w
15
www.sakshieducation.com
www.sakshieducation.com
TIMSK
TIMSK Register:-
Register:-
The Timer/Counter Interrupt Mask Register, shown in figure below:
Bi 7 6 5 4 3 2 1 0
m
t
co
OC T TI OCI OCI TO O T
IE0 OI CI E1A E1B IE1 CI OI
n.
E2 E1 E1 E0
Read R/W R/W R/W R/W R/W R/W R/W R/W
io
/write
Initial 0 0 0 0 0 0 0 0
at
value
uc
Figure
Figure2
ure2.4.7:
.4.7: - TIMSK Register
It is a common register for all the three timers.
ed
Bits (1:0):-
(1:0):
hi
• Correspond to TIMER0
ks
• Bit 0:
a
interrupt.
w
o
w
• Bit 1:
w
16
www.sakshieducation.com
www.sakshieducation.com
Bits (5:2):-
(5:2):-
• Correspond to TIMER1.
• Bit 2 – TOIE1 – Timer/Counter1 Overflow Interrupt Enable bit
o Enables the overflow interrupt of TIMER1.
• Other bits are related to CTC mode
m
o Bit 4:3 – OCIE1A:
OCIE1A: B – Timer/Counter1, Output Compare
co
A/B Match Interrupt Enable bits.
Enabling it ensures that an interrupt is fired whenever a
n.
match occurs.
io
Since there are two CTC channels, we have two
at
different bits OCIE1A and OCIE1B for them.
uc
Bit 5-
5- TICIE1 Timer 1 Input Capture Interrupt Enable
TICIE1= 0 Disables Timer l input capture interrupt
ed
Bits (7:6):
ks
• Correspond to TIMER2
• Setting the bit TOIE2 to ’1′ enables the TIMER0 overflow
a
.s
interrupt.
• OCIE0 – Timer/Counter0
w
occurs.
17
www.sakshieducation.com
www.sakshieducation.com
TIFR Register:-
Register:-
m
co
n.
Bi 7 6 5 4 3 2 1 0
t
io
OC TO IC OCF OCF TO O TO
F2 V2 F1 1A 1B V1 CF V0
at
0
uc
Read R/W R/W R/W R/W R/W R/W R/W R/W
/writ
ed
e
Initia 0 0 0 0 0 0 0 0
l
hi
value
ks
Figure 2.4.8:
.4.8: TIFR Register
a
.s
Bits (1:0):
w
• Correspond to TIMER0
w
• Bit 0:
o TOV0 (Timer/Counter1
Timer/Counter1 Overflow Flag
Flag)
lag bit is set (one)
whenever TIMER0 overflows.
This bit is reset (zero) whenever the Interrupt Service
Routine (ISR) is executed.
18
www.sakshieducation.com
www.sakshieducation.com
m
ISR is executed.
co
Alternatively it is cleared by writing ‘1’ to it.
n.
Bits (5:2):
io
• Correspond to TIMER1.
at
• Bit 2 – TOV1 – Timer/Counter1 Overflow Flag bit is set to 1
uc
whenever the timer overflows
o This bit is reset (zero) whenever the Interrupt Service Routine
ed
(ISR) is executed.
o If there is no ISR to execute, we can clear it manually by
hi
Bits (7:6):
• Correspond to TIMER2
• TOV2 bit is set (one) whenever TIMER2 overflows.
19
www.sakshieducation.com
www.sakshieducation.com
Example:
m
Flash an LED every 8 ms and we have an XTAL of 16 MHz
co
We can use a prescalar of 1024. Now refer to the descriptions of clock select
bits as shown in the TCCR0 register.
n.
#include <avr/io.h>
io
void timer0_init()
at
{
// set up timer with prescalar = 1024
uc
TCCR0 |= (1 << CS02)|(1 << CS00);
// initialize counter
ed
TCNT0 = 0;
}
hi
int main(void)
{
ks
// initialize timer
.s
timer0_init();
w
// loop forever
while(1)
w
{
w
20
www.sakshieducation.com
www.sakshieducation.com
}
}
}
TIMER1:-
TIMER1:-
The 16-bit Timer/Counter unit allows accurate program execution timing (event
m
management),
co
Wave generation, and signal timing measurement.
Features:-
Features:-
n.
io
The main features are:
• True 16-bit Design (i.e., allows 16-bit PWM).
at
• Two Independent Output Compare Units.
uc
• Double Buffered Output Compare Registers.
ed
• Frequency Generator.
w
In addition to the usual timer/counter, Timer 1 contains one 16 bit input capture
register and two 16-bit outputs compare registers.
21
www.sakshieducation.com
www.sakshieducation.com
The input capture register is used for measuring pulse widths or capturing
times. The output compare registers are used for producing frequencies or pulses
from the timer/counter to an output pin on the microcontroller.
TCNT1 Register:
m
Bit 7 6 5 4 3 2 1 0
co
TCNT
n.
1
[15:8]
io
TCNT
at
1
uc
[7:0]
Read R/W R/W R/W R/W R/W R/W R/W R/
ed
/write W
Initial 0 0 0 0 0 0 0 0
hi
value
ks
Figure2
Figure2.4.9:
.4.9: - TCNT1 Register.
It is 16 bits wide since the TIMER1 is a 16-bit register. TCNT1H represents the
a
HIGH byte whereas TCNT1L represents the LOW byte. The timer/counter value
.s
The ATMegal6 timer /control register for Timer 1 is actually composed of two
registers, TCCR1A and TCCR1B.
• TCCR1A controls the compare modes and the pulse width modulation
modes of Timer1.
22
www.sakshieducation.com
www.sakshieducation.com
TCCR1A Register:-
Register:-
m
The Timer/Counter1 Control Register A, shown in figure below.
co
Bit 7 6 5 4 3 2 1 0
n.
CO CO CO CO FOC FO W W
io
M1 M1 M1 M1B 1A C1 G GM
A1 A0 B1 0 B M1 10
at
uc 1
Read W R/W R/W R/W R/W R/W R/W R/W
/write
ed
Initial 0 0 0 0 0 0 0 0
value
hi
Figure
Figure2
ure2.4.10:
.4.10: TCCR1A Register
ks
23
www.sakshieducation.com
www.sakshieducation.com
m
low level)
1 1 Set OC1A/OC1B on compare match (Set output to
co
high level)
n.
io
Table
Table4.4.11:
4.4.11: Compare Output Mode, non-
non-PWM
at
• Bit 3:2 – FOC1A: B – Force Output Compare for Compare Unit A/B.
A/B
uc
o These bits are write only bits.
o They are active only in non-PWM mode.
ed
TCCR1B Register:-
Register:-
w
Bit 7 6 5 4 3 2 1 0
IC ICE - WG WG CS1 CS CS1
NC S1 M13 M12 2 11 0
1
Read R/W R/W R R/W R/W R/W R/W R/W
24
www.sakshieducation.com
www.sakshieducation.com
/write
Initial 0 0 0 0 0 0 0 0
value
Figure
Figure2
ure2.4.12:
.4.12: - TCCR1B Register
The bit 2:0 – CS12:10 are the Clock Select Bits of TIMER1. Their selection
m
is as follows.
co
CS12 CS11 CS10 Description
0 0 0 No clock source (Timer/Counter stopped)
n.
0 0 1 ClkI/O/1 (No prescaling)
io
0 1 0 ClkI/O/8 (From prescaler)
at
0 1 1 uc ClkI/O/64 (From prescaler)
1 0 0 ClkI/O/256 (From prescaler)
1 0 1 ClkI/O/1024 (From prescaler)
ed
edge.
a
Figure 4.4.13:
4.4.13: - TCCR1B Prescalar Definitions
.s
Bits 4:3 in TCCR1B (WGM13 & WGM12) are Wave Generation Mode Bits
which are used to select mode shown in figure below.
25
www.sakshieducation.com
www.sakshieducation.com
m
1 0 0 0 1 PWM, Phase Correct, 8- 0x00F TOP BO
bit F TT
co
OM
2 0 0 1 0 PWM, Phase Correct, 9- 0x01F TOP BO
n.
bit F TT
io
OM
3 0 0 1 1 PWM, Phase Correct, 10- 0x03F TOP BO
at
bit F TT
uc OM
4 0 1 0 0 CTC OCR1 Immedi MA
A ate X
ed
F P
7 0 1 1 1 Fast PWM, 10-bit 0x03F TOP TO
a
F P
.s
OM
w
OM
10 1 0 1 0 PWM, Phase Correct ICR1 TOP BO
TT
OM
11 1 0 1 1 PWM, Phase Correct OCR1 TOP BO
26
www.sakshieducation.com
www.sakshieducation.com
A TT
OM
12 1 1 0 0 CTC ICR1 Immedi MA
ate X
13 1 1 0 1 Reserved - - -
14 1 1 1 0 Fast PWM ICR1 TOP TO
P
m
15 1 1 1 1 Fast PWM OCR1 TOP TO
co
A P
n.
Figure2
Figure2.4.14:
.4.14: - Wave Generation Mode Bit Description
io
In pin configuration of ATMEGA16/32, we can see the pins PB3, PD4, PD5
at
and PD7. Their special functions are mentioned in the brackets (OC0, OC1A,
OC1B and OC2). These are the Output Compare pins of TIMER0, TIMER1 and
uc
TIMER2 respectively shown in figure below.
ed
hi
ks
We must tell the AVR to reset the timer as soon as its value reaches such and such
.s
value. So, the question is, how do we set such and such values? The Output
w
Bit 7 6 5 4 3 2 1 0
OCR 15:8 OCR1A
1A H
OCR 7:0 OCR1A
27
www.sakshieducation.com
www.sakshieducation.com
1A L
Rea R/W R/W R/W R/W R/W R/W R/W R/W
d
/wri
te
Initi 0 0 0 0 0 0 0 0
m
al
valu
co
e
n.
Figure
Figure2
ure2.4.15:
.4.15: OCR1A Register
io
Bit 7 6 5 4 3 2 1 0
at
OCR 15:8 OCR1B
1B
uc
H
OCR 7:0 OCR1B
ed
1B L
Rea R/W R/W R/W R/W R/W R/W R/W R/W
hi
d
ks
/wri
te
a
Initi 0 0 0 0 0 0 0 0
.s
al
valu
w
e
w
Figure2
Figure2.4.16:
.4.16: OCR1B Register
w
Since the compare value will be a 16-bit value (in between 0 and 65535), OCR1A
and OCR1B are 16-bit registers. In ATMEGA16/32, there are two CTC channels –
A and B. We can use any one of them or both. Let’s use OCR1A.
28
www.sakshieducation.com
www.sakshieducation.com
TIMER 2:
Features:-
Features:-
• Single Compare unit Counter
• Clear Timer on Compare Match (Auto Reload)
• Glitch-free, Phase Correct Pulse Width Modulator (PWM)
m
• Frequency Generator
• 10-bit Clock Prescaler
co
• Overflow and Compare Match Interrupt Sources (TOV2 and OCF2)
n.
• Allows clocking from External 32 kHz Watch Crystal Independent of
the I/O Clock
io
• TIMER2 is an 8-bit timer (like TIMER0).
at
• Most of the registers are similar to that of TIMER0 registers. Apart
uc
from that, TIMER2 offers a special feature which other timers don’t
that is Asynchronous Operation.
ed
Operation
• Timer 2 can be used as timer 1 or timer 0 but it can act as Real Time
hi
Counter, when the 32.768 kHz connected to Tosc1 and Tosc2 pins
ks
If AS2 = 1,
1 Timer2/counter2 is clocked from a crystal oscillator connected to
w
29
www.sakshieducation.com
www.sakshieducation.com
In timer0 when cs02 – cs00 have values 110 and 111, Timer 0 counts the
external events. But , in Timer 2 the values of cs are different compared to
Timer 0.
TCNT2 Register:
m
Bit 4
co
6 5 3
7 4 2 1 0
n.
TCNT2[7:0]
io
Read\
Read\Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial 0 0 0 0 0 0 0 0
at
Value uc
Figure2
Figure2.4.17:
.4.17: - TCNT2 Register
ed
TCCR2 Register:-
Register:-
hi
The Timer/Counter
Timer/Counter Control Register is shown in figure below.
ks
Bit 4
7 6 5 4 3
a
2 1 0
.s
Initial 0 0 0 0 0 0 0 0
w
Value
w
Figure 2.4.18:
.4.18: TCCR2 Register
In TIMER0/1 the prescalar available are 8, 64, 256 and 1024, whereas in
TIMER2, we have 8, 32, 64, 128, 256 and 1024.
30
www.sakshieducation.com
www.sakshieducation.com
The bit 2:0 – CS22:20 are the Clock Select Bits of TIMER2. Their selection
is as follows.
m
0 1 0 ClkT2S/8 (From prescalar)
co
0 1 1 ClkT2S/32 (From prescalar)
1 0 0 ClkT2S/64 (From prescalar)
n.
1 0 1 ClkT2S/128 (From prescalar)
io
1 1 0 ClkT2S/256 (From prescalar)
at
1 1 1 uc ClkT2S/1024 (From prescalar)
Figure 2.4.19:
.4.19: Clock Select Bit Description
The mode or counting sequence is determined by setting WGM21 and WGM20.
ed
0 1 CTC
1 0 PWM PHASE
ks
CORRECT
1 1 FAST PWM
a
.s
Figure 2.4.20:
2.4.20: Mode Selection Bit Description
w
w
w
31
www.sakshieducation.com
www.sakshieducation.com
CHAPTER 3
TIMER MODES
Lesson 1:
1: Introduction
Timers are basically used to,
o Produce the delay.
m
o Counts the events from external world.
o It can generate waveforms.
co
o It can measure the waveform period.
n.
The above applications can be done using
using all the timers present in the ATMEGA16
io
micro controller.
at
The above tasks listed are categorized under the section called modes. So , it can be
uc
said that the timers have four modes.
ed
1. Normal mode
2. CTC mode.
hi
32
www.sakshieducation.com
www.sakshieducation.com
Figure3
Figure3.2.1:
.2.1: - The timer or counter 0 in NORMAL MODE
m
Steps to Program Timer0 in Normal Mode
co
Step 1:-
1:- Load the TCNT0 register with initial value.
n.
Step 2:-
2:- Load the TCCR0 register,
io
at
• Indicating which mode (8 bit or 16 bit) is to be used.
uc
• Prescalar option.
ed
Step 3:-
3:- Keep monitoring the timer overflow flag (TOV0) to see if it is raised get
out of the loop.
hi
Step 4:-
4: Stop the timer by disconnecting the clock source TCCR0.
ks
Step 5:-
5: clear the TOV0 flag for the next round.
a
.s
Step 6:-
6: go back to step1 to load TCNT0 again.
w
Example 1:-
1:-
The led blinking program for delay of 3 sec using Timer 1 Normal
Mode using the XTAL = 8MHZ.
33
www.sakshieducation.com
www.sakshieducation.com
#include<avr/io.h>
#include<util/delay.h>
void delay3(void);
m
{
co
DDRB|=0XFF;//MAKE PROT B AS OUTPUT PORT
while(1)//loop
n.
{
io
PORTB|=0x01;//LED CONNECTED TO PORTB 0 PIN GETS THE
at
SUPPLY uc
delay3();
delay3();
hi
{
w
VALUE
w
TCNT1L|=0X71;
TCCR1B|=0X05;
34
www.sakshieducation.com
www.sakshieducation.com
m
To get the 3 sec delay using timer 1 in the above mentioned example, TCNTI =
co
0XA471 is loaded. How the value is calculated?
n.
The F_CPU frequency considered is 8MHZ.
io
Then according to the formula,
at
uc
ed
35
www.sakshieducation.com
www.sakshieducation.com
In the above table the last column has the Timer Count values.
m
The timer 0 and timer 2 have the capability to store the value up to
co
255.
n.
• When prescalar value is 8 then timer count value is 2999999. This
io
value cannot be stored in any timer register. So neglect it.
at
• When prescalar value is 64 then timer count value is 374999. This
value cannot be stored in any timer register. So neglect it.
uc
• When prescalar value is 256 then timer count value is 93750. This
ed
23436-----
23436-----0X5B8C
-----0X5B8C
FFFF-
FFFF-5B8C = A473.
w
w
36
www.sakshieducation.com
www.sakshieducation.com
Lesson 3:
3: CTC MODE
We consider timer 1 is considered for the explanation of CTC mode.
The timer 1 is a 16 bit timer. It has the timer counter register TCNT1 and
two OCR1 registers called OCR1A and OCR1B. Both are 16 bit registers. To
control these compare registers there must be required two TCCRn registers
m
of 16 bit size. They are TCCR1A and TCCR1B registers.
co
n.
io
at
uc
ed
The timer counts up until the content of the TCNT1 register becomes equal to the
content of OCR1A register. Then the timer will be cleared when the
a
next clock occurs. The OCF1A flag will be set as a result of compare
.s
match.
w
37
www.sakshieducation.com
www.sakshieducation.com
m
bit OM
co
2 0 0 1 0 PWM, Phase Correct, 9- 0x01FF TOP BOTT
bit OM
3 0 0 1 1 PWM, Phase Correct, 0x03FF TOP BOTT
n.
10-bit OM
io
4 0 1 0 0 CTC OCR1 Immedia MAX
A te
at
5 0 1 0 1 Fast PWM, 8-bit 0x00FF TOP TOP
uc
6 0 1 1 0 Fast PWM, 9-bit 0x01FF TOP TOP
7 0 1 1 1 Fast PWM, 10-bit 0x03FF TOP TOP
ed
Frequency correct A M OM
ks
1 A OM
1 1 1 0 0 CTC ICR1 Immedia MAX
w
2 te
w
1 1 1 0 1 Reserved - - -
3
w
38
www.sakshieducation.com
www.sakshieducation.com
We can see that there are two possible selections for CTC Mode. Practically,
both are the same, except the fact that we store the timer compare value in different
registers. Right now, let’s move on with the first option (0100). Thus, the
initialization of TCCR1A and TCCR1B is as follows.
m
TCCR1B |= (1 << WGM12)|(1 << CS11)|(1 << CS10); // Mode = CTC,
co
Prescaler = 64
n.
OCR1A and OCR1B Registers:
io
We must tell the AVR to reset the timer as soon as its value reaches to the
at
value stored in the OCR1A register. So, the question is, how do we set such values?
uc
The Output Compare Register 1A – OCR1A and the Output Compare Register 1B
– OCR1B are utilized for this purpose.
ed
Since the compare value will be a 16-bit value (in between 0 and 65535),
hi
OCR1A and OCR1B are 16-bit registers. In ATMEGA16/32, there are two CTC
channels – A and B. We can use any one of them or both. Let’s use OCR1A.
ks
Example:
a
.s
TIFR Register:
w
w
Figure 3.3.3 :-
:- TIFR register
39
www.sakshieducation.com
www.sakshieducation.com
m
Using Interrupts with CTC Mode:
co
In the previous methodology, we simply used the CTC Mode of operation.
We used to check every time for the flag bit (OCF1A). Now let’s shift this
n.
responsibility to the AVR itself! Yes, now we do not need to check for the flag bit at
io
all! The AVR will compare TCNT1 with OCR1A. Whenever a match occurs, it sets
at
the flag bit OCF1A, and also fires an interrupt! We just need to attend to that
uc
interrupt, that’s it.
• Overflow,
hi
• Compare
• Capture.
ks
TIMSK Register:
.s
w
w
w
Figure 3.3.4 :-
:- TIMSK register
40
www.sakshieducation.com
www.sakshieducation.com
m
interrupt is fired (as OCIE1A is set) and the OCF1A flag is set. Now since an
co
interrupt is fired, we need an Interrupt Service Routine (ISR) to attend to the
interrupt. Executing the ISR clears the OCF1A flag bit automatically and the timer
n.
value (TCNT1) is reset.
io
Example:-
Example:-Without
Without Interrupts
at
Program that shows how the led that is connected to the PORTB.1 pin blinks with
uc
3 seconds delay ctc mode, timer1, 1024 PRESCALAR.
ed
//F_CPU=8MHZ
hi
{
w
w
41
www.sakshieducation.com
www.sakshieducation.com
delay(); //DELAY
m
}
co
n.
void delay() //FUNCTION DEFINITION
io
{
at
TCNT1=0X0000; //TIMER 1A IS LOADED WITH 0 VALUE
uc
OCR1A=0X5B8C; // OCR1A IS LOADED WITH 999 IN DECIMAL
TCCR1B=0X0D;
ks
while((TIFR&(1<<OCF1A))==0);
{
a
.s
PORTB=0X01;
w
_delay_ms(500);
w
PORTB=0X00;
w
_delay_ms(500);
In the pin configuration of ATMEGA16/32, we can see the pins PB3, PD4,
PD5 and PD7. Their special functions are mentioned in the brackets (OC0, OC1A,
m
OC1B and OC2). These are the Output Compare pins of TIMER0, TIMER1 and
co
TIMER2 respectively.
n.
Here TCCR1A Register plays major role to operate this mode.
io
at
uc
ed
Figure 3.3.5 :-
:- TCCR1A register
hi
ks
Now time for us to concentrate on Bit 7:6 – COM1A1:0 and Bit 5:4
5:4 –
a
the behavior of the Output Compare (OC) pins. The behavior changes depending
w
43
www.sakshieducation.com
www.sakshieducation.com
m
level)
1 1 Set OC1A/OC1B on compare match (Set output to high
co
level)
n.
Figure 3.3.6 :-
:- compare match table
io
at
• We choose the second option (01). No need to check any flag bit, no need to
uc
attend to any interrupts, nothing. Just set the timer to this mode. Whenever a
ed
OC1B) can be controlled this way, which means that we should connect the LED to
ks
Example Code:
.s
#include <avr/io.h>
w
#include <avr/interrupt.h>
w
void timer1_init()
{
// set up timer with prescaler = 64 and CTC mode
TCCR1B |= (1 << WGM12)|(1 << CS11)|(1 << CS10); CS10);
// set up timer OC1A pin in toggle mode
TCCR1A |= (1 << COM1A0);
// initialize counter
44
www.sakshieducation.com
www.sakshieducation.com
TCNT1 = 0;
// initialize compare value
OCR1A = 24999;
}
int main(void)
{
DDRD |= (1 << 5); 5); // connect led to pin PD5
m
timer1_init();
timer1_init();//
); initialize timer
while(1) // loop forever
co
{
// do nothing
n.
// whenever a match occurs
// OC1A is toggled automatically!
io
// no need to keep track of any flag bits or ISR
}
at
} uc
Forcing Compare Match:
ed
Bit 3:2in TCCR1A – FOC1A: B – Force Output Compare for Compare Unit
hi
A/B.
A/B
ks
• For ensuring compatibility with future devices, these bits must be set to
w
45
www.sakshieducation.com
www.sakshieducation.com
In this section we will discuss about the registers only. CTC mode of TIMER
0/ 2 is exactly in the same way of TIMER 1. So we will discuss about TIMER 0 now.
TCCR0 Register:
m
The Timer/Counter0 Control Register–
Register TCCR10 Register is as follows:
co
• Bit 6:3 – WGM01:00 – Wave Generation Mode – Just like in
TIMER1, we choose the type of wave mode from here as follows.
n.
Choose 10 for CTC mode.
io
o
at
COM01 COM00 Description
0 0 Normal port operation, OC0 disconnected
uc
0 1 Toggle OC0 on compare match
ed
Figure 3.3.7:
.3.7: Compare Output Mode, non-
non-PWM
ks
46
www.sakshieducation.com
www.sakshieducation.com
OCR0 Register:
m
co
Figure
Figure 5.3.8:
5.3.8: OCR0 Register
n.
The value to be compared (max 255) is stored in this register.
io
TIMSK Register:
at
The Timer/Counter Interrupt Mask–
uc Mask TIMSK Register is as follows:
ed
hi
Figure
Figure 3.3.9:
.3.9: TIMSK Register
ks
Figure
Figure 3.3.10:
.3.10: TIFR Register
47
www.sakshieducation.com
www.sakshieducation.com
CHAPTER 4
m
co
Lesson 1: Introduction
This topic helps you to understand the concept like how to generate the square wave
n.
using the in built feature of AVR timers. The square wave can be generated from the
io
AVR port pins. By using some program we toggle 0 and 1from a pin of the micro
at
controller with some delay provided, so that we can obtain the square wave from
uc
that pin.
• There are four pins in ATMEGA16 micro controller used for generate the
ed
waveform.
hi
•
a
ATMEGA16
w
0C1A(PD5) 19 TIMER 1
OC1B(PD4) 18 TIMER 1
w
OC2(PD7) 21 TIMER2
Figure 4.1.1:-
.1.1:- pin description of OC0, OC1A, OC1B, OC2 pins.
48
www.sakshieducation.com
www.sakshieducation.com
m
Figure 4.1.2:-
.1.2:- timer modes
co
Here, the toggling the pin with some delay is enough to produce the square wave
form. So, delay can be produced by using the Normal mode and CTC mode. So we
n.
have seen earlier that the concept how to produce the delay using two modes. The
io
better option is to use the CTC mode than normal mode to have delay, to produce
at
the waveform from a pin.
When the Timer is triggered, register TCNTx counts the value constantly as timer
uc
started. Each timer has an OCRx (Output Compare Register), which is continuously
ed
compared with TCNTx register. In CTC mode whenever match occurs, OCFx
(Output Compare Flag) will set to 1. If continuous wave form generation is required,
hi
OCFx must be reset again. Alternatively, if OCIEx (Output Compare interrupt) and
ks
Global interrupt flags in SREG are set, OCFx will reset automatically after interrupt
execution.
a
.s
For example if we consider the Timer0 to produce the waveform then, OCR0
register is used as comparison register and OC0 pin is used to output the waveform
w
signal.
w
w
The bits WGM0 [1:0] are programmed to select waveform generation mode
49
www.sakshieducation.com
www.sakshieducation.com
3 10 CTC mode
4 11 Fast PWM mode
Figure 4.1.3:-
.1.3:- Waveform generation mode
m
co
COM[1:0] DESCRIPTION
DESCRIPTION
n.
2 Toggle OC0 on compare match
io
at
3 uc Clear OC0 on compare match
OCRx TCNTx
a
.s
8 bit comparator
w
w
TCNTx
Wave
TCNTx Bottom generator OCx
OCx
FOCx 50
www.sakshieducation.com
www.sakshieducation.com
Figure 4.1.4:
.1.4: - Wave Generator of 8 bit timer
• For each register there is at least an OCRx register (like OCR0 for timer 0).
m
The value of the register (OCRx) is constantly compared with the TCNTx
co
register, when match occurs, OCFx flag is set.
n.
• Each timer has waveform generator. This waveform generator can generate
io
the waves on OCx pin.
• The WGMx and COMx of the TCCRx register can determine the how the
waveform generator works.
at
uc
• When TCNTx register reaches top or bottom or compare match occurs, the
ed
waveform generator is informed. Then wave generator changes the state of the
OC0 pins according to the mode of timer (WGM00, WGM01) and compare
hi
Example: -
.s
The square wave generated from Pinb.4 (OC0) of 32 kHz, timer 0, ctc mode.
w
//XTAL=12MHZ
w
//concept:
//concept: waveform generation without using interrupts
w
51
www.sakshieducation.com
www.sakshieducation.com
TCCR0=0X19;
TCCR0=0X19; //setting the mode,prescalar ,compare modes of the timer
m
co
} //end of the program
n.
Example:-
Example:-
io
The square wave generated from Pinb.4 (OC0) of 32 kHz, timer 0, ctc mode.
//XTAL=12MHZ
at
uc
//concept: waveform generation using
using interrupts
ed
OCR0 = 187
187;
87;
52
www.sakshieducation.com
www.sakshieducation.com
} //end
m
co
n.
io
at
uc
Figure 4.1.5:- Waveform generation of frequency 32kHZ.
ed
hi
Lesson 2
53
www.sakshieducation.com
www.sakshieducation.com
Introduction:
Introduction:-
oduction:-
m
encode information for transmission, its main use is to allow the control of the
co
power supplied to electrical devices, especially to inertial loads such as motors.
n.
Note:-
Note:-
io
at
The average value of voltage (and current) fed to the load is controlled by turning the
switch between supply and load on and off at a fast pace.. The longer the switch is on
uc
compared to the off periods, the higher the power supplied to the load is.
ed
54
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
io
at
uc
Figure 4.2.2: - 50%
50% duty cycle - - - - -1/2 power
ed
hi
ks
3/2 power
a
.s
w
w
w
Figure 4.2.3
4.2.3:
2.3: - 75% duty cycle - - - - -3/2 power
55
www.sakshieducation.com
www.sakshieducation.com
So when constant supply is fed to the DC motor then it runs with some constant
speed. So, if in case we want to vary the speed of the motor then the supply voltage /
power is need to be changed. This can be achieved by using the PWM technique.
m
Here only the duty cycle is change to vary the speed of the motor.
co
The larger the ON time period in the wave form the more power fed into the motor.
n.
So the PWM signals can be provided by using micro controllers where a PWM
io
circuitry chip is embedded into the microcontroller.
at
We are very well aware that the AVR provides us with an option of 8 and 16 bit
uc
timers. 8bit timers count from 0 to 255, then back to zero and so on. 16bit timers
ed
count from 0 to 65535, then back to zero. Thus for a 8bit timer, MAX = 255 and for
a 16bit timer, MAX = 65535.
hi
a ks
.s
w
w
56
www.sakshieducation.com
www.sakshieducation.com
m
co
Figure 4.2.5:
.2.5: - fixed top and variable max value
n.
io
at
uc
ed
hi
Figure 4.2.6:
.2.6: -Fixed and Variable TOP in Timers
ks
Before going to PWM concepts in timers we have to aware about TOP, Bottom and
.s
timer whenever a match occurs. Due to this, the value of TOP can be reduced as
w
shown in 2nd figure. The thick line shows how the timer would have gone in normal
mode. Now, the CTC Mode can be extended to introduce variable TOP as shown
in 3rd figure.
57
www.sakshieducation.com
www.sakshieducation.com
Lesson 3
• Fast PWM.
• Phase Correct PWM.
m
Fast PWM Mode:
Mode:-
co
Let us consider the Timer 0 for the explanation
explanation of this mode.
n.
In this method the saw tooth wave is considered and this wave is compared with a
fixed voltage level as shown in figure. Now suppose we increase the compare voltage
io
level (to, say B). In this case, as we can see, the pulse width has reduced, and hence
at
the duty cycle.
uc
ed
hi
a ks
.s
w
w
w
Figure 4.3.1:
.3.1: - Example of Fast PWM Mode
But, as you can see, both the pulses (A and B) end at the same time
irrespective of their starting time.
58
www.sakshieducation.com
www.sakshieducation.com
In this mode, since saw tooth waveform is used, the timer counter TCNTx (n
= 0, 1, 2) counts from BOTTOM to TOP and then it is simply allowed to overflow
(or cleared at a compare match) to BOTTOM.
This method is not correct to use for controlling the DC motors of a robot because
the saw tooth wave considered is not symmetrical.
m
Phase of the output waveform cannot be maintained constant in this method.
co
In the Fast PWM method the counter counts like in normal mode. The TCNT
n.
value increments up to 0XFF and then then rolls from 0XFF to 00, it sets high
io
TOV0 flag bit.
at
When we consider the Timer 0 then OC0 pin of ATMEGA16 is used for output
uc
the waveform.
TIMER0.
hi
Here, we will learn how to set appropriate bits to run the timer in PWM
mode.
a
.s
Bit 7 6 5 4 3 2 1 0
FO W CO CO WG CS0 CS CS0
w
C0 GM M0 M00 M01 2 01 0
w
00 1
Read W R/W R/W R/W R/W R/W R/W R/W
w
/write
Initial 0 0 0 0 0 0 0 0
value
Figure
Figure 4.3.2:
.3.2: TCCR0 Register
59
www.sakshieducation.com
www.sakshieducation.com
m
0 0 0 Normal 0xFF Immediate MAX
co
1 0 1 PWM, Phase 0xFF TOP BOTTOM
correct
n.
2 1 0 CTC OCR0 Immediate MAX
3 1 1 Fast PWM 0xFF TOP MAX
io
at
Figure
Figure 4.3.3:
.3.3: Waveform Generation Mode Bit Description
uc
• Bit 5, 4 – COM01:0 – Compare Match Output
Output Mode - These bits are
set in order to control the behavior of Output Compare pin (OC0) in
ed
The following look up table to determine the operations of OC0 pin for Fast
ks
PWM mode.
(inverting mode)
Figure
Figure 4.3.4:
.3.4: Compare Output Mode, Fast PWM Mode
60
www.sakshieducation.com
www.sakshieducation.com
CS02
CS02 CS01 CS00 Timer 0 clock seletor
0 0 0 No clock source (Timer/Counter stopped)
0 0 1 ClkI/O/1 (No prescaling)
0 1 0 ClkI/O/8 (From prescaler)
0 1 1 ClkI/O/64 (From prescaler)
m
1 0 0 ClkI/O/256 (From prescaler)
1 0 1 ClkI/O/1024 (From prescaler)
co
1 1 0 External clock source on T0 pin. Clock on falling
n.
edge.
io
1 1 1 External clock source on T0 pin. Clock on rising
edge.
at
Figure 4.3.5: Timer 0 clock selector table
uc
Choosing Compare Output Modes to produce the PWM output :-
:-
ed
• inverted,
• non-inverted
ks
We see what the inverting and non inverting mode makes the difference in
a
.s
61
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
io
at
uc
ed
hi
When the COM01: 00 is 0:1 then when compare match occurs then the OC0 pin is
cleared. OC0 pin is low till the next comparison is matched. This example is shown
a
.s
62
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
io
at
uc
ed
hi
aks
.s
w
w
63
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
io
at
uc
ed
hi
a ks
.s
Figure 4.3.8:
.3.8: - PWM with non inverting mode with 100% duty cycle
w
w
w
Note:-
Note:-
We can observe in this mode that as the OCR0 value is increased the
duty cycle of the output wave is increased.
64
www.sakshieducation.com
www.sakshieducation.com
Inverting Mode:-
Mode:-
m
co
n.
io
at
uc
ed
hi
a ks
.s
w
w
Figure 4.3.9:
.3.9: - PWM with inverting mode.
w
65
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
io
at
uc
ed
hi
aks
.s
w
w
w
Figure 4.3.10:
.3.10:- PWM with inverting mode
66
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
io
at
uc
ed
hi
a ks
.s
w
Figure 4.3.11:
.11:- PWM with inverting mode with 100% duty cycle.
inverting
w
w
Note:-
Note:-
67
www.sakshieducation.com
www.sakshieducation.com
We can observe in this mode that as the OCR0 value is increased the
duty cycle of the output wave is decreased.
I ll make you understand how the wave form is generated with some considerations
like duty cycle with certain frequency using inverting or non inverting mode.
Example:-
Example:- for non inverting mode
m
co
Assuming XTAL = 8MHZ, using FAST PWM non-
non-inverting mode, write a program
that generates a wave with frequency of 31,250 Hz and duty cycle of 75%.
n.
Solution:-
Solution:-
io
Given,
XTAL = 8MHZ
at
uc
Frequency of the output wave should be 31,250 Hz.
ed
STEP 1:-
1:- Find the OCR0 value using formula,
a
.s
75 = 100
w
w
OCR0 = 191.
STEP 2:-
2:- set the timer in
68
www.sakshieducation.com
www.sakshieducation.com
• No prescalar.
• Non-
Non- inverted Fast PWM mode.
m
Frequency of generated wave = .
co
Where, N is prescalar value and fosc = frequency of
oscillator.
n.
io
So when we substitute the values in above formula then we
get N =1.
at
uc
Set CS 02:01:00 = 0 0 1 in TCCR0 register.
STEP 3:-
3:- To set the timer in Fast PWM mode, Non Inverting Mode.
ed
Set Com01:00 = 1 0.
ks
Set WGM01:00 = 1 1.
w
Bit 7 6 5 4 3 2 1 0
FO W CO CO WG CS0 CS CS0
C0 GM M0 M00 M01 2 01 0
00 1
Read W R/W R/W R/W R/W R/W R/W R/W
69
www.sakshieducation.com
www.sakshieducation.com
/write
Initial 0 0 0 0 0 0 0 0
value
m
0 1 1 0 1 0 0 1
co
TCCR0 = 0x69
n.
io
4:- The above process must repeated for infinite times to produce the wave
Step 4:-
form from OC0 pin continuously.
at
uc
Embedded C program:
program: - for non inverting mode
ed
that generates a wave with frequency of 31,250 Hz and duty cycle of 75%.
a ks
#include<avr/io
#include<avr/io.h>
avr/io.h> // header file includes the functions of input and output
w
statements
w
int main()
w
70
www.sakshieducation.com
www.sakshieducation.com
m
co
return 0;
n.
Output:-
Output:-
io
at
uc
ed
hi
a ks
.s
w
w
w
.3.12: - output waveform of 31250 HZ, fast PWM, non inverting mode
Figure 4.3.12:
71
www.sakshieducation.com
www.sakshieducation.com
Example:-
Example:- for inverting mode.
Assuming XTAL = 8MHZ, using FAST PWM inverting mode, write a program
that generates a wave with frequency of 31,250 Hz and duty cycle of 75%.
Solution:-
Solution:-
m
Given,
co
XTAL = 8MHZ
n.
Frequency of the output wave should be 31,250 Hz.
io
Duty cycle to be maintained is 75%.
at
Mode: - Fast PWM Mode, inverting mode.
uc
STEP 1:-
1:- Find the OCR0 value using formula,
ed
OCR0 = 63.
ks
STEP 2:-
2:- set the timer in
a
• No prescalar.
.s
• Non-
Non- inverted Fast PWM mode.
w
w
w
72
www.sakshieducation.com
www.sakshieducation.com
3:- To set the timer in Fast PWM mode, Non Inverting Mode.
STEP 3:-
m
To set in inverting mode then
co
Set Com01:00 = 1 1.
n.
To set in Fast PWM mode,
io
Set WGM01:00 = 1 1.
at
uc
Then TCCR0 register value is,
ed
Bit 7 6 5 4 3 2 1 0
FO W CO CO WG CS0 CS CS0
ks
C0 GM M0 M00 M01 2 01 0
00 1
a
/write
w
Initial 0 0 0 0 0 0 0 0
value
w
w
0 1 1 1 1 0 0 1
TCCR0 = 0x79
73
www.sakshieducation.com
www.sakshieducation.com
Step 4:-
4:- The above process must repeated for infinite times to produce the wave
form from OC0 pin continuously.
Embedded C program:-
program:- for inverting mode.
m
co
Note: - the OC0 pin is at PB3 of ATMEGA16.
n.
#include<avr/io.h> // header file includes the functions of input and output
io
statements
at
int main()
uc
{ //start of main function
ed
OCR0 = 63;
63; // top value stored in compare register.TCNT0 has 0
ks
values initially.
a
TCCR0 = 0X79
0X79;
79;// mode non inverting Fast PWM mode with no
.s
return 0;
w
Output:-
Output:-
74
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
io
at
uc
ed
hi
Figure 4.3.13:
.3.13:-output waveform of 31250 HZ, fast PWM, inverting mode
ks
The same concept can be applied to the Timer 1 but the formulas will be changed
like,
a
.s
F generated wave =
w
w
75
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
io
at
uc
ed
The TCNT0 value goes up and down as shown in above figure. First it counts up
ks
and then counts down. Whenever it reaches zero then TOV0 flag is set to 1.
a
These registers of Timer 0 are used to set the mode and to control the registers.
.s
w
Here, we will learn how to set appropriate bits to run the timer in PWM
mode.
w
76
www.sakshieducation.com
www.sakshieducation.com
Bit 7 6 5 4 3 2 1 0
FO W CO CO WG CS0 CS CS0
C0 GM M0 M00 M01 2 01 0
00 1
Read W R/W R/W R/W R/W R/W R/W R/W
/write
Initial 0 0 0 0 0 0 0 0
m
value
co
Figure
Figure 4.3.15:
.3.15: TCCR0 Register
n.
• Bit 6, 3 – WGM01, 00 – Waveform Generation Mode - These bits can
io
be set to either “00″ or “01″ depending upon the type of PWM you
at
want to generate.
Figure
Figure 4.3.16:
.3.16: Waveform Generation Mode Bit Description
.s
The following look up table to determine the operations of OC0 pin for
phase correc PWM mode.
77
www.sakshieducation.com
www.sakshieducation.com
m
Figure
Figure 4.3.17:
.3.17: Compare Output Mode, phase correct mode
co
CS02 CS01 CS00 Timer 0 clock seletor
0 0 0 No clock source (Timer/Counter stopped)
n.
0 0 1 ClkI/O/1 (No prescaling)
io
0 1 0 ClkI/O/8 (From prescaler)
at
0 1 1 uc ClkI/O/64 (From prescaler)
1 0 0 ClkI/O/256 (From prescaler)
1 0 1 ClkI/O/1024 (From prescaler)
ed
edge.
a
Figure 4.3.18:
.3.18: - Timer 0 clock selector
.s
• Inverting mode.
w
78
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
io
at
uc
ed
hi
a ks
Figure 4.3.19:- Phase Correct PWM Mode With Non Inverting Mode.
.s
w
w
w
79
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
io
at
uc
ed
hi
ks
Figure 4.3.20:
.3.20: - Phase correct non inverted PWM mode with 100% duty cycle.
a
.s
Note:-
Note:-
w
We can observe in this mode that as the OCR0 value is increased the
w
Note:-
Note:-
The longer the switch is on compared to the off periods, the higher the
power supplied to the load is.
80
www.sakshieducation.com
www.sakshieducation.com
Inverting mode:-
mode:-
When the COM01: 00 is 0:1 then when compare match occurs then the OC0 pin is
set to 1. OC0 pin is set to 1 till the next comparison is matched. This example is
shown in above figure mentioned. This process is called as INVERTING MODE.
m
co
n.
io
at
uc
ed
hi
a ks
.s
w
w
As we increase the OCR0 value then the duty cycle of the output wave form
decreases.
81
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
io
at
uc
ed
Figure 4.3.22:
.3.22: - Phase Correct Inverted mode wi
with
th 0 % duty cycle
I will make you understand how the wave form is generated with some
hi
considerations like duty cycle with certain frequency using inverting or non inverting
ks
mode.
a
Example:
Example: - Phase Correct PWM non-inverting mode
.s
program that generates a wave with frequency of 61 Hz and duty cycle of 87.5%.
w
w
Using timer 0.
Solution:-
Solution:-
Given,
XTAL = 8MHZ
82
www.sakshieducation.com
www.sakshieducation.com
STEP 1:-
1:- Find the OCR0 value using formula,
m
Duty cycle = *100.
co
OCR0 = 223.
223.
n.
STEP 2:-
2:- set the timer in
io
• Non-
at
Non- inverted Fast PWM mode.
uc
How to set prescalar value?
ed
oscillator.
a
get N =256
w
STEP 3:-
3:- To set the timer in Phase Correct PWM non-inverting mode
83
www.sakshieducation.com
www.sakshieducation.com
Set Com01:00 = 1 0.
Set WGM01:00
WGM01:00 = 0 1.
1.
m
Then TCCR0 register value is,
co
TCCR0 – Timer/Counter0 Control Register 0:
n.
Bit 7 6 5 4 3 2 1 0
FO W CO CO WG CS0 CS CS0
io
C0 GM M0 M00 M01 2 01 0
at
00 1
Read W R/W R/W R/W R/W R/W R/W R/W
uc
/write
Initial 0 0 0 0 0 0 0 0
ed
value
hi
1 1 0 0 1 0 0
ks
0
a
.s
TCCR0 = 0x6
0x64
w
w
4:- The above process must repeated for infinite times to produce the wave
Step 4:-
form from OC0 pin continuously.
w
84
www.sakshieducation.com
www.sakshieducation.com
m
#include<avr/io.h> // header file includes the functions of input and output
co
statements
n.
int main()
io
{ //start of main function
DDRB = 0b00001000;
at //PORT B 4 pin (OC0) as output pin.
uc
OCR0 = 223;
223; // top value stored in compare register.TCNT0 has
ed
0 values initially.
hi
TCCR0 = 0X6
0X64;// mode non inverting Fast PWM mode with no
ks
return 0;
w
85
www.sakshieducation.com
www.sakshieducation.com
Output:-
Output:-
m
co
n.
io
at
uc
Figure 4.3.23:-
.3.23:- the output wave of Phase Correct PWM non inverted mode
ed
hi
Example:-
Example:- Phase Correct PWM inverting mode
ks
Assuming XTAL = 8MHZ, using Phase Correct PWM inverting mode, write a
program that generates a wave with frequency of 61 Hz and duty cycle of 87.5%.
a
.s
Using timer 0.
w
Solution:-
Solution:-
w
Given,
w
XTAL = 8MHZ
86
www.sakshieducation.com
www.sakshieducation.com
STEP 1:-
1:- Find the OCR0 value using formula,
OCR0 = 32
m
STEP 2:-
2:- set the timer in
co
• Phase Corrected PWM inverted mode
n.
io
How to set prescalar value?
at
Frequency of generated wave = .
uc
Where, N is prescalar value and fosc = frequency of
ed
oscillator.
hi
get N =256
a
STEP 3:-
3:- To set the timer in Phase Correct PWM inverting mode.
w
Set Com01:00 = 1 1.
1.
Set WGM01:00
WGM01:00 = 0 1.
87
www.sakshieducation.com
www.sakshieducation.com
Bit 7 6 5 4 3 2 1 0
FO W CO CO WG CS0 CS CS0
C0 GM M0 M00 M01 2 01 0
m
00 1
Read W R/W R/W R/W R/W R/W R/W R/W
co
/write
Initial 0 0 0 0 0 0 0 0
n.
value
io
at
1 1 1 0 1 0 0
0
uc
ed
TCCR0 = 0x7
0x74
hi
4:- The above process must repeated for infinite times to produce the wave
Step 4:-
ks
Assuming XTAL = 8MHZ, using Phase Correct PWM inverting mode, write
write a
w
program that generates a wave with frequency of 61 Hz and duty cycle of 87.5%.
w
88
www.sakshieducation.com
www.sakshieducation.com
int main()
OCR0 = 32;
32; // top value stored in compare register.TCNT0 has 0
m
values initially.
co
TCCR0 = 0X74;// mode non inverting Fast PWM mode with no
prescalar option is set to timer 0.
n.
while (1); //never ending loop
io
at
return 0; uc
} // end of main funtion
ed
Output:-
Output:-
hi
a ks
.s
w
w
w
89
www.sakshieducation.com
www.sakshieducation.com
The same concept can be applied to the Timer 1 but the formulas will be changed
like,
m
% & '
co
as:--
Duty cycle for non inverted mode can be calculated as:
n.
(
F generated wave = )& *+
*100
io
Duty cycle for inverted mode can be calculated as:-
as:-
at
& ' (
F generated wave = )& *+
*100
uc
ed
hi
a ks
.s
w
w
w
90
www.sakshieducation.com