Professional Documents
Culture Documents
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).
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:
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);
Suppose that 𝑓𝑐𝑙𝑘 = 4𝑀𝐻𝑧 and 𝑃𝑅2 = 255, the prescaler and postscaler values
can be calculated by selecting the bits ratio.
Timer2 module block diagram and its associated registers should have known as
well. They will be used for coding later on.
Not in the
block diagram
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
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 10 Figure 11
Use frequency of internal clock Use external signal (eg. push button)
(Fosc/4) and generates time delays to count pulses
Counter Mode
Figure 14
Timer Mode
Figure 15
SIZE OF 16-bits
8-bits (TMR0) 8-bits (TMR2)
REGISTER (TMR1H:TMR1L)
Figure 16
FORMULA
Timer1
BLOCK
DIAGRAM
Figure 17
FORMULA
Timer2
BLOCK
DIAGRAM
Figure 18
FORMULA
(Question 3)
Table 4: Using Generic delays
Solution:
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 can be easily changed Cannot change time delay during
by input clock frequency program execution
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.
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: