HCMC University of Technology and Education
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Faculty of Electrical & Electronic Engineering
MICROPROCESSOR
Chapter 5:
TIMER - COUNTER
Ngo Ba Viet, PhD
1
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Introduction
▪ Timers are used to measure time or generate precise time
delays.
▪ While the microcontroller can create or measure the
required time delays by executing loops, using a
timer/counter frees the CPU from this redundant and
repetitive task, enabling it to dedicate more processing
time to other activities.
2
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Introduction
▪ A timer is essentially a simple binary counter that can be
configured to count clock pulses (either internal or
external). When it reaches its maximum value, it resets to
zero, sets an overflow flag, and generates an interrupt if it
is enabled.
3
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Introduction
▪ Pic16f887 has three independent timers which can be
used as timer, Counters, or PWM generation.
▪ Below table provides the details of the three Timers.
4
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Timer Calculation
▪ PIC Oscillator frequency is divided by 4 and then fed to the
controller. Now this frequency can be further divided by
prescalar to generate the range of delays.
▪ Time to increment the Timer count by one (timer tick) can
be determined as below.
tick = (Prescaler/(Fosc/4) or tick = (Prescaler * 4)/Fosc
5
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Timer Calculation
▪ Now the Timer value for the required delay can be
calculated as below.
- Delay = TimerCount * tick
- Count = (Delay/tick)
- RegValue = TimerMax- Count
- RegValue = TimerMax-(Delay/tick) = TimerMax -
(Delay/((Prescalar *4)/Fosc))
→ RegValue = TimerMax-((Delay * Fosc)/(Prescalar*4))
6
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Timer Calculation
▪ Below table provides the formula for all the three Timers.
7
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Timer0 Registers
The timer0/counter0 has the following features:
• It is an 8-bit timer/counter.
• The counting value of the timer/counter can be read and
written.
• It includes an 8-bit prescaler that allows software-selectable
division factors.
• It allows selection of internal or external clock sources.
• Generates an interrupt when it overflows from FFH to 00H.
• Allows selection of clock pulse triggering on the rising or
falling edge. 8
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Timer0 Registers
The timer control register is OPTION_REG. The register
configuration and the functions of the bits are as follows:
Bit 7: Port B pull-up resistor control bit.
Bit 6: Interrupt edge selection bit.
Bit 5: TMR0 Clock Source Select bit.
1 = External clock pulse input at T0CKI pin.
9
0 = Internal clock pulse input.
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Timer0 Registers
Bit 4: TMR0 Source Edge Select bit.
0 = Active on the rising edge at the T0CKI pin.
1 = Active on the falling edge at the T0CKI pin.
Bit 3: Prescaler Assignment bit.
1 = Prescaler is assigned to the Watchdog Timer (WDT).
0 = Prescaler is assigned to Timer0.
Bits 2-0: Prescaler rate select bits.
10
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Timer0 Block Diagram
Example: Delay Calculations for 1ms @20Mhz with Prescaler as 32:
RegValue = 256-(Delay * Fosc)/(Prescalar*4)) = 256-((1ms * 20Mhz)/(32*4))
= 256-156=100
11
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Timer0 Registers
- When the count value in the TMR0 register overflows from FFh to 00h,
an interrupt is generated, and the TMR0IF interrupt flag is set to 1. The
interrupt can be disabled using the TMR0IE interrupt enable bit.
- In the Timer0 interrupt service routine, the TMR0IF interrupt flag must
be cleared.
- According to the datasheet, the interrupt enable bit is named T0IE, and
the interrupt flag is named T0IF.
- The INTCON register is located in the RAM memory area at address
0xB0.
12
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Timer1 Registers
- Timer1 is a 16-bit timer/counter consisting of two 8-bit
registers (TMR1H and TMR1L), which can be read from and
written to.
- When an overflow occurs, Timer1 will generate an interrupt,
and the TMR1IF interrupt flag (PIR1<0>) will be set to 1.
- Timer1 has an interrupt enable/disable bit named TMR1IE
(PIE1<0>).
13
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Timer1 Registers
14
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Timer1 Registers
Bit 7: Timer1 Gate Invert bit
1 = Timer1 gate is active-high (Timer1 counts when the
gate is at a high level).
0 = Timer1 gate is active-low (Timer1 counts when the
gate is at a low level).
Bit 6: Timer1 Gate Enable bit
- If TMR1ON = 0: This bit has no effect.
- If TMR1ON = 1:
1 = Timer1 is enabled when the gate is inactive.
15
0 = Timer1 is enabled.
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Timer1 Registers
Bits 5-4: Timer1 Input Clock Prescale Select bits
- 11 = 1:8. - 01 = 1:2.
- 10 = 1:4. - 00 = 1:1.
Bit 3: Timer1 Oscillator Enable Control bit
1 = Oscillator is enabled.
0 = Oscillator is disabled.
Bit 2: Timer1 External Clock Input Synchronization Control bit
- When TMR1CS = 1:
1 = Do not synchronize the external clock input.
0 = Synchronize the external clock input.
- When TMR1CS = 0: This bit is ignored. Timer1 uses the internal clock when
TMR1CS = 0.
Bit 1: Timer1 Clock Source Select bit
1 = External clock source selected from pin RC0/T1OSO/T1CKI
(rising edge).
0 = Internal clock source selected (FOSC/4).
Bit 0: Timer1 Control bit
1 = Timer1 is enabled (counting).
16
0 = Timer1 is disabled (stopped).
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Timer2 Registers
- Timer2 is an 8-bit timer with both a prescaler and a postscaler.
- Timer2 is used for pulse-width modulation (PWM) when the CCP module
operates in PWM mode.
- The TMR2 register can be read from and written to, and is cleared when
reset.
- The internal clock (FOSC/4) passes through a prescaler with division
factors of "1:1," "1:4," or "1:16," selected by the control bits
T2CKPS1:T2CKPS0 (T2CON<1:0>).
- PR2 is an 8-bit period register used for comparison. Timer2 increments
from 00h until it matches the value stored in PR2, then resets to 00h and
repeats the process.
- PR2 is a readable and writable register, and is initialized to FFH when the
system is reset.
- The output of TMR2 goes through a 4-bit postscaler before triggering an
interrupt request, setting the TMR2IF flag (PIR1<1>) to 1.
- When the TMR2ON bit is 0, Timer2 is turned off to reduce power
consumption.
17
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Timer2 Registers
18
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
Timer2 Registers
Bit 7: Unused; if read, it will have a value of '0'.
Bits 6-3: Timer2 Postscaler Output Select bits
- 0000 = 1:1 - 0001 = 1:2 - 0010 = 1:3 - 0011 = 1:4
- 0100 = 1:5 - 0101 = 1:6 - 0110 = 1:7 - 0111 = 1:8
- 1000 = 1:9 - 1001 = 1:10 - 1010 = 1:11 - 1011 = 1:12
- 1100 = 1:13 - 1101 = 1:14 - 1110 = 1:15 - 1111 = 1:16
Bit 2: Timer2 Enable/Disable Control bit
1 = Timer2 is enabled (counting).
0 = Timer2 is disabled (stopped).
Bits 1-0: Timer2 Prescaler Select bits for the clock source
00 = Prescale factor is 1.
01 = Prescale factor is 4. 19
1x = Prescale factor is 16.
Timer Commands
setup_timer_X( mode )
X: 0-2
Mode:
- Timer or counter mode or disable timer
- Prescaler
Constants from different groups may be or'ed together with |.
Ex: setup_timer_1 (T1_INTERNAL| T1_DIV_BY_8);
Setup timer 1 as timer mode and use prescaler 8
* Open file 16f887.h and find timer used to see options you
can use with this command
Timer Commands
Sets the count value
SET_TIMERX( value)
X: 0-2
Value:
Timers 1 get a 16 bit int.
Timer 0 and 2 gets an 8 bit int.
Timer Commands
Gets the count value
value=get_timerX();
X: 0-2
Value:
If use timer 1: value is a 16 bit variable
If use timer 0 or 2 : value is an 8 bit variable
EXAMPLE
Use Timer 1 to create a delay of 100ms. Given that Fosc =
20MHz and the prescaler is 8, please write some commands
to set up the timer to operate according to the above
requirements.
setup_timer_1 (T1_INTERNAL|T1_DIV_BY_8);
SET_TIMER1(3036);
Value = 65536-((100ms *20MHz)/(8*4))
= 65536-((0.1s *20000000Hz)/(8*4))=3036
EXAMPLE
Count externals pulse and display the counted value
on the second line of LCD using timer 0
CODE
CODE
CODE
EXAMPLE
Measure one second using timer 1
and display the result of counting
second from 0 to 59 on the second
line of LCD
EXAMPLE
5M 625K
20M
❑ Consider that the crystal used is 20MHz.
❑ After dividing by 4, the clock frequency is 5MHz, which means
that the counter must count 5 million pulses to measure one
second.
❑ Since the counter is limited to 65535, we need to use a
prescaler. By choosing a prescaler of 8, the counter must now
count 625,000 pulses to measure one second.
❑ To achieve this, we will count ten times every 62,500 pulses.
Therefore, we need to set the counter to start counting from
(65536 - 62500 = 3036).
EXAMPLE
CODE
CODE
CODE
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
END
34