Lecture 5
Lecture 5
FACULTY OF ENGINEERING
ELECTRICAL & ELECTRONICS ENGINEERING DEPT.
Instructor:
Assist. Prof. Dr. Mohammed ALKRUNZ
Electrical & Electronics Engineering Dept.
PIC16F84A- TMR0
Introduction:
Programmer should think how to get advantage of this timer and its features.
Number of bits determines the maximum counting of the timer. (i.e., 8 bits
timer can count until 255).
After each count up to 255, timer resets its value to zero, and starts with a
new cycle of counting to 255. This transition process from 255 to zero is called
“Overflow”
Dr. M. Alkrunz
November 4, 2022 2
EEE471: Embedded System Design
PIC16F84A- TMR0
Introduction:
During each transition from 255 to zero (overflow), T0IF bit in “INTCON”
register is set to “1”.
Dr. M. Alkrunz
November 4, 2022 3
EEE471: Embedded System Design
PIC16F84A- TMR0
TMR0 As a Counter Mode:
In this case, “OPTION_REG” register play an important role. Let us discuss that:
Dr. M. Alkrunz
November 4, 2022 4
EEE471: Embedded System Design
PIC16F84A- TMR0
TMR0 As a Counter Mode:
In this case, “OPTION_REG” register play an important role. Let us discuss that:
Dr. M. Alkrunz
November 4, 2022 5
EEE471: Embedded System Design
PIC16F84A- TMR0
TMR0 As a Counter Mode:
In this case, “OPTION_REG” register play an important role. Let us discuss that:
Dr. M. Alkrunz
November 4, 2022 6
EEE471: Embedded System Design
PIC16F84A- TMR0
TMR0 As a Counter Mode:
In this case, “OPTION_REG” register play an important role. Let us discuss that:
Write a program to perform a counter (00 to FF) using TMR0 via RA4/T0CK.
The counter increases every 2 external clocks.
Dr. M. Alkrunz
November 4, 2022 8
EEE471: Embedded System Design
PIC16F84A- TMR0
Example 1:
Write a program to perform a counter (00 to FF) using TMR0 via RA4/T0CK.
The counter increases every 2 external clocks.
Dr. M. Alkrunz
November 4, 2022 9
EEE471: Embedded System Design
PIC16F84A- TMR0
Delay UsingTMR0 (Delay Mode):
The idea here to get advantage from TMR0 and T0IF without interrupt.
When TMR0 reach 255, this mean that overflow is occurred and T0IF =1.
In this case, decrease one from a constant and reset T0IF to allow TMR0 to
count again and so on.
So, the number of TMR0 overflows will be achieved as the number of the
constant.
Dr. M. Alkrunz
November 4, 2022 10
EEE471: Embedded System Design
PIC16F84A- TMR0
Delay UsingTMR0 (Delay Mode):
𝟒
𝟐𝒑𝒓𝒆𝒔.+𝟏 × 𝟐𝟓𝟓 − 𝑻𝑴𝑹𝟎 𝒗𝒂𝒍𝒖𝒆 × 𝒌 ×
𝑭𝒐𝒔𝒄
Where:
Dr. M. Alkrunz
November 4, 2022 11
EEE471: Embedded System Design
PIC16F84A- TMR0
Delay UsingTMR0 (Delay Mode):
Example:
Assume that we need 5 sec delay using TMR0. Do the required calculations.
𝟒
𝟐𝒑𝒓𝒆𝒔.+𝟏 × 𝟐𝟓𝟓 − 𝑻𝑴𝑹𝟎 𝒗𝒂𝒍𝒖𝒆 × 𝒌 ×
𝑭𝒐𝒔𝒄
4
2𝟕+1 × 255 − 𝟎 × 𝑘 × = 5 sec 𝒌 = 𝟕𝟔
4 𝑀𝐻𝑧
Write a program to perform a counter (00 to FF) using TMR0 delay. The
required delay is 2 sec.
Assume:
Prescaler = 7 ,
TMR0 value = 0,
4
2𝟕+1 × 255 − 𝟎 × 𝑘 × = 2 sec 𝒌 = 𝟑𝟎
4 𝑀𝐻𝑧
Dr. M. Alkrunz
November 4, 2022 13
EEE471: Embedded System Design
PIC16F84A- TMR0
Example 2:
Dr. M. Alkrunz
November 4, 2022 14
EEE471: Embedded System Design
PIC16F84A- TMR0
Interrupt UsingTMR0 (Interrupt Mode):
Do you remember from the last lecture when I talked about the sources of
interrupt. TMR0 was one source type of them.
Sources of Interrupt:
Dr. M. Alkrunz
November 4, 2022 15
EEE471: Embedded System Design
PIC16F84A- TMR0
Example 3:
Write a program to perform a counter (00 to FF) using TMR0 Interrupt. In the
same time, a flasher is working on RA0.
We will use a delay for the counter equals to 1 sec. (Prescaler = 7, TMR0 = 0, k = 15)
Go to file
Dr. M. Alkrunz
November 4, 2022 16
EEE471: Embedded System Design
PIC16F84A- TMR0
Example 3:
Dr. M. Alkrunz
November 4, 2022 17
EEE471: Embedded System Design
PIC16F84A- TMR0
As a Summery:
8-bit timer/counter,
Dr. M. Alkrunz
November 4, 2022 18
EEE471: Embedded System Design
*Questions
Dr. M. Alkrunz
November 4, 2022 19
EEE471: Embedded System Design
*Thanks
Dr. M. Alkrunz
November 4, 2022 20
EEE471: Embedded System Design