You are on page 1of 20

ISTANBUL AYDIN UNIVERSITY

FACULTY OF ENGINEERING
ELECTRICAL & ELECTRONICS ENGINEERING DEPT.

EEE471: EMBEDDED SYSTEM DESIGN

5. PIC16F84A TMR0 Concepts

Instructor:
Assist. Prof. Dr. Mohammed ALKRUNZ
Electrical & Electronics Engineering Dept.
PIC16F84A- TMR0
Introduction:

 Timers are usually the most complicated parts of a microcontroller.

 Physically, timer is a register whose value is continually increasing from 0 to


255, and then it starts all over again: 0, 1, 2, 3, ….. , 255, 0, 1, 2, 3 …… etc.

 This increment is done in the background of any process.

 Programmer should think how to get advantage of this timer and its features.

 PIC16F84A has one 8-bit timer.

 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”.

 If interrupts are allowed to occur, this can be taken as an advantage reason of


generating interrupts.

 It is up to programmer to reset “T0IF” bit in the interrupt routine, so the new


interrupt or the new overflow could be detected.

 Timer counting could be increased internally or externally:

 Internally (by oscillator),

 Externally (by RA4/T0CK1) pin.

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:

It is just an internal resistors to


provide extra protection for port B
when it is used as input port

We talked about this pin in the


topic of interrupt.

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:

By this pin you can select if TMR0


will count by external or internal
pulses.

Setting the edge


if external pulses
is selected by
RA4/T0CK1 pin

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:

Prescaler Rate Select Bits

PIC16F84A has 2 timers; TMR0 and


WDT. By this pin you can select
which timer you need.

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:

Prescaler Rate Select Bits

 If you select PSA = 0. TMR0 is enabled.


 If PS2:PS1:PS0 = 001. TMR0 will increase
its value by 1 every 4 clocks.
 User will decide this clocks will be
internally or externally by setting “T0CS”
pin.
𝟐𝒑𝒓𝒆𝒔.+𝟏 𝟐𝒑𝒓𝒆𝒔.
Dr. M. Alkrunz
November 4, 2022 7
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 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 T0IF = 1: This means that an overflow is occurred to TMR0.

 The idea of making delay using TMR0 is as following:

 TMR0 will start to count internally (depends on prescaler).

 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):

 How to calculate the value of Delay?!!!

𝟒
𝟐𝒑𝒓𝒆𝒔.+𝟏 × 𝟐𝟓𝟓 − 𝑻𝑴𝑹𝟎 𝒗𝒂𝒍𝒖𝒆 × 𝒌 ×
𝑭𝒐𝒔𝒄

Where:

 2𝑝𝑟𝑒𝑠.+1 ≡ 𝑃𝑟𝑒𝑠𝑐𝑎𝑙𝑒𝑟 𝐴𝑠𝑠𝑖𝑔𝑛𝑚𝑒𝑛𝑡

 255 − 𝑇𝑀𝑅0 𝑣𝑎𝑙𝑢𝑒 ≡


𝑟𝑒𝑝𝑙𝑎𝑐𝑒 𝑇𝑀𝑅0 𝑣𝑎𝑙𝑢𝑒 𝑤𝑖𝑡ℎ 𝑡ℎ𝑒 𝑠𝑡𝑎𝑟𝑡𝑖𝑛𝑔 𝑣𝑎𝑙𝑢𝑒 𝑔𝑖𝑣𝑒𝑛 𝑖𝑛 𝑡ℎ𝑒 𝑝𝑟𝑜𝑔𝑟𝑎𝑚.

 𝑘 ≡ 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡 𝑖𝑛𝑡𝑒𝑔𝑒𝑟 𝑛𝑢𝑚𝑏𝑒𝑟. 𝑌𝑜𝑢 𝑐𝑎𝑛 𝑠𝑒𝑙𝑒𝑐𝑡 𝑎 𝑛𝑢𝑚𝑏𝑒𝑟 𝑓𝑟𝑜𝑚 1 𝑡𝑜 255.

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.

If we select (prescaler = 7 “Max value”), and (TMR0 value = 0). Then:

𝟒
𝟐𝒑𝒓𝒆𝒔.+𝟏 × 𝟐𝟓𝟓 − 𝑻𝑴𝑹𝟎 𝒗𝒂𝒍𝒖𝒆 × 𝒌 ×
𝑭𝒐𝒔𝒄

4
2𝟕+1 × 255 − 𝟎 × 𝑘 × = 5 sec 𝒌 = 𝟕𝟔
4 𝑀𝐻𝑧

 If you need 1 sec delay, you can select (k = 15).


Dr. M. Alkrunz
November 4, 2022 12
EEE471: Embedded System Design
PIC16F84A- TMR0
Example 2:

Write a program to perform a counter (00 to FF) using TMR0 delay. The
required delay is 2 sec.

To make a 2 sec delay:

Assume:

 Prescaler = 7 ,

 TMR0 value = 0,

 Then, by calculating: k = ?!!.

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.

AGAIN REMEMBER !!!!

 Sources of Interrupt:

1. Finishing writing data process to EEPROM. (EEIE, EEIF)

2. TMR0 interrupt caused by timer overflow. (T0IE, T0IF)

3. Alteration on RB4 to RB7 pins of port B. (RBIE, RBIF)

4. External interrupt from RB0/INT pin. (INTE, INTF)

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:

The TMR0 module of PIC16F84A has the following features:

 8-bit timer/counter,

 Readable and writable,

 Internal or external clock select,

 Edge select for external clock,

 8-bit software programmable prescaler,

 Interrupt on overflow from (FFh to 00h).

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

You might also like