You are on page 1of 13

EXPERIMENT 4: Programs on Counters and Time Delays

Objectives
To familiarize with counters and time delays using C language.

Components
Software: MPLAB X IDE (latest version)
Software: XC8 C Compiler (latest version)
IC type: PIC16F877A microcontroller
LEDs: Red
Crystal: 4MHz
Capacitors: 22pF
Resistors: 330Ω ±5%

Introduction
A timer module is typically a digital circuit (peripheral) within the microcontroller chip that
could be controlled via few dedicated registers, Special Function Registers. An 8-Bit timer
will count from (0 to 255). A 16-Bit timer will count from (0 to 65535). In timer mode, the timer
module is being incremented by the local clock of the system (oscillator).

Figure 4-1 Block diagram of the Timer 2 module.

In counter mode, the timer module is being incremented by an external clock source (signal)
on the RC0 Pin. A push button can be used to connect to the counter’s external clock input,
then pressing the button will increment the counter’s value (the TMRx register itself), it could
be TMR0, TMR1, etc. The counter must first have a falling edge before increments occur on
every rising edge. Timer 0, Timer 1 and Timer 2 module’s general equations are:

4 × 𝑃𝑟𝑒𝑠𝑐𝑎𝑙𝑒𝑟 × (256 − 𝑇𝑀𝑅0) × 𝑋


𝑇𝑜𝑢𝑡 =
𝐹𝑜𝑠𝑐

4 × 𝑃𝑟𝑒𝑠𝑐𝑎𝑙𝑒𝑟 × (65536 − 𝑇𝑀𝑅1) × 𝑋


𝑇𝑜𝑢𝑡 =
𝐹𝑜𝑠𝑐

4 × 𝑃𝑟𝑒𝑠𝑐𝑎𝑙𝑒𝑟 × (𝑃𝑅2 − 𝑇𝑀𝑅2) × 𝑃𝑜𝑠𝑡𝑠𝑐𝑎𝑙𝑒𝑟 × 𝑋


𝑇𝑜𝑢𝑡 =
𝐹𝑜𝑠𝑐

Prepared by Steven Khoo. Page 1 of 2


Procedure
1. Construct the schematic diagram as shown in Figure 4-2. Assume the crystal used in
this experiment is 4 MHz.

Figure 4-2 Timer operation using PIC16F877A Microcontroller.

2. Write a C program to generate a 1-second time interval at Port B, RB0 pin using
Timer 2 module.
3. Refer to the Guideline for more details. Ensure that the program can be compiled
without errors. Timer Modules in PIC Microcontrollers:
https://deepbluembedded.com/timer-modules/

Questions
1. Timer modules can be configured to operate in two different modes such as Timer
Mode and Counter Mode. Differentiate between these two modes using appropriate
example and diagram.
2. PIC16F877A microcontroller has 3 different timer modules such as Timer0, Timer1
and Timer2. Differentiate between these three timers using appropriate example
and diagram.
3. Explain in details why it is better to use the Timer module to blink an LED instead of
generic Delays as shown by the four lines of code below.
Line Code
1 RB0 = 1;
2 delay_ms(1000);
3 RB0 = 0;
4 delay_ms(1000);

Prepared by Steven Khoo. Page 2 of 2


Introduction
The purpose of this experiment is to familiarize with timer module and its
function. In actual fact, there are many timer modules depend on the PIC family
that we used. For example, PIC18F4550 has 4 timer modules, which are Timer0,
Timer1, Timer2 and Timer3. But in this experiment, PIC16F877A is used. It has
3 timer modules, which are Timer0, Timer1 and Timer2. (Note: as the number
increases from 16F to 18F, there is more functionality.)
Timer module is part of the microcontroller, as shown in Figure 1. The main
purpose of it is to perform time delays, as shown in Figure 2.

Figure 1: Timers in PIC16F877A Figure 2: 1sec time delay

Results and discussion


For this experiment, we are required to write a C program to produce a 1-second
time interval at Port B using Timer2 module in MPLAB X IDE and the generated
hex file is used as a program file for PIC16F877A. Before coding, golden
combination number should be calculated. The general equation for Timer2
module is:
𝑓𝑐𝑙𝑘 = 4𝑀 , 𝑃𝑅2 = 255

Suppose that 𝑓𝑐𝑙𝑘 = 4𝑀𝐻𝑧 and 𝑃𝑅2 = 255, the prescaler and postscaler values
can be calculated by selecting the bits ratio.

It can be 1, 4 or 16. It can be 1-16.


With that, full calculation can be made:
Let 𝑇𝑀𝑅2 = 5, 𝑃𝑟𝑒𝑠𝑐𝑎𝑙𝑒𝑟 = 4, 𝑃𝑜𝑠𝑡𝑠𝑐𝑎𝑙𝑒𝑟 = 10, therefore
4𝑀𝐻𝑧
Count = = 100 (Note: The count must be in integer)
4×4×(255−5)×10

Timer2 module block diagram and its associated registers should have known as
well. They will be used for coding later on.

Figure 3: Block diagram of the Timer2 module

Table 1: Registers associated with Timer2 module

When coding, initialize a value to register is important. In this case, registers


like INTCON, PIR1, PIE1, TMR2, T2CON and PR2 must be mentioned and
initialized in the source code. (Note: Shaded cells are not used by Timer2
module)
The structure of registers are shown in the figures below:

Not in the
block diagram

Figure 4: Structure of INTCON register

TMR2IF = 0
Figure 5: Structure of PIR1 register

Not in the
block diagram
Figure 6: Structure of PIE1 register

Note: TMR2 and PR2 registers are initialized to 5 and 255 respectively. (Based
on the calculation above)
T2CON =
0b01001101
1:10 postscale = 1001

TMR2ON = 1

Prescaler is 4 = 01

Figure 7: Structure of T2CON register

Since GIE, PEIE and TMR2IE (Interrupt enable bits) are not used in the block
diagram, therefore they can be ignored in the source code. With that, the source
code will result as below:

Figure 8: Source code for Timer2 Module (Timer mode)


Sequence = 0000 0000 → 1111 1111

Figure 9: Timer2 module operation using PIC16F877A

As shown in Figure 9, Timer2 module will generate a 1-second time interval at


Port B. 8 LEDs are connected to Port B and they will light up in sequence.

Timer modules can be configured to operate in two different modes such as


Timer Mode and Counter Mode. Differentiate between these two modes using
appropriate example and diagram. (Question 1)

Solution - Table 2: Comparison between Timer Mode and Counter Mode

Timer Mode Counter Mode

Figure 10 Figure 11
Use frequency of internal clock Use external signal (eg. push button)
(Fosc/4) and generates time delays to count pulses

Incremented by the local clock of Incremented by external source on


system (oscillator) the RC0 Pin

Configuration bit for TMR1CS Configuration bit for TMR1CS


must be 0 (If use TMR1) must be 1 (If use TMR1)

Generates automatically Generates manually


Eg. Sand timer (Figure 12) Eg. Flip-flops (Figure 13)

Counter Mode

Figure 14
Timer Mode

Figure 15

PIC16F877A microcontroller has 3 different timer modules such as Timer0,


Timer1 and Timer2. Differentiate between these three timers using
appropriate example and diagram. (Question 2)

Solution – Table 3: Comparison between TMR0, TMR1 and TMR2

Timer0 Timer1 Timer2

SIZE OF 16-bits
8-bits (TMR0) 8-bits (TMR2)
REGISTER (TMR1H:TMR1L)

PRESCALER 1:2 to 1:256 1:1 to 1:8 1:1,1:4,1:16

POSTSCALER - - 1:1 to 1:16

INTERRUPT On overflow On overflow TMR2 matches


EVENT and FLAG FFh to 00h FFFFh to 0000h PR2
LOCATION (T0IF in INTCON) (TMR1IF in PIR1) (TMR2IF in PIR1)
CLOCK
SOURCE T0CKI pin T1CKI pin -
(External)
CAN WAKE PIC NO
NO YES
FROM SLEEP?

Note: T0IF is the same as TMR0IF.


Timer0
BLOCK
DIAGRAM

Figure 16

FORMULA

Timer1
BLOCK
DIAGRAM

Figure 17

FORMULA

Timer2
BLOCK
DIAGRAM

Figure 18
FORMULA

Of course, to differentiate between these 3 timers (TMR0, TMR1 & TMR2),


their block diagrams and formulas are also different as well. With that, their
source codes will be totally different.

Timer0 Timer1 Timer2

Figure 19 Figure 20 Figure 21

Figure 22: PIC16F877A


Explain in details why it is better to use the Timer module to blink an LED
instead of generic Delays as shown by the four lines of code below.

(Question 3)
Table 4: Using Generic delays

Solution:

Timer Module Delays

The system will have a very The system will have a very
high responsiveness low responsiveness
(Reason: Doing one action in (Reason: Doing several actions
the main loop) in the main loop)

Time Delay: Accurate Time Delay: Not so accurate

Cannot handle longer time delays


Can handle longer time delays (Maximum delay – Based on
Crystal Oscillator used)

Time delay can be easily changed Cannot change time delay during
by input clock frequency program execution

Can do multitasking Cannot do multitasking

Table 5: Comparison between Timer Module and Generic Delays

Summary:
When comes to Embedded system, efficiency is the key aspect. As shown in
Table 5, timer module is way more efficient as it is better than generic Delays in
terms of responsiveness, accuracy and time delay management.
Conclusion
The maximum number of counts for a timer module depends on the size. The
more the bits, the longer the timer will last before overflowing. In this example,
Timer1 module has higher bits than Timer0 and Timer2 modules. Therefore, it
can increment up to a value of 65535 (216 − 1) before it overflows back to 0.

Timer0 8 bits (FFh to 00h)

Timer1 16 bits (FFFFh to 0000h)

Timer2 8 bits (FFh to 00h)

Table 6: Timer modules’ bit lengths and their max value

Timer module is also used in small and big applications. Just like in the airport,
to count the exact number of passengers boarding, the sensor can be placed at
the door. In this way, the sensor will detect the presence of passengers and the
output will be delivered to the input pin of the microcontroller. The timer
register inside the microcontroller then increments each time a passenger
crosses the door. (In this case, timer module acts as counter)
Figure 23 below shows PIR sensor connected to microcontroller to detect the
human presence:

Figure 23: PIR sensor as external input (RC0)

You might also like