0% found this document useful (0 votes)
10 views55 pages

ES Lec7

Uploaded by

Habiba Ghazi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views55 pages

ES Lec7

Uploaded by

Habiba Ghazi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Tanta Higher Institute of Engineering

and Technology, THIET

‫االنظمة المدمجة‬
Embedded Systems
Course Code: ELE 413

Communication & Computer Engineering Department


Level: 400
Lec 7
PIC16F877A Microcontrollers
(TIMERS AND COUNTERS)
TIMERS AND COUNTERS

 Most microcontrollers are equipped with one or more


timing systems
 that can be used to perform a variety of timer functions
including generating events at specific times, determining
the duration between two events, or counting events.
TIMERS AND COUNTERS

 Example applications that require generating events


include generating an accurate signal in a digital watch,
keeping a traffic light green for a specific duration, or
communicating bits serially between devices at a specific
rate, etc.
TIMERS AND COUNTERS

Theory of operation
 The main component of such a timing system is a free
running binary counter.
 The counter increments for each incoming timing pulse.
 The counter counts continuously from 0 to 2n-1, where n
is the number of bits in the counter.
 Since it runs independently, it can count inputs or clock
pulses concurrently while the microcontroller is executing
the main program.
TIMERS AND COUNTERS

Theory of operation
 If the input clock to the binary counter has a fixed known
frequency, we can make an accurate measurement of
time interval by counting the pulses from the clock.
 For example, if a particular clock’s frequency is 1 MHz
(period 1 µs), and we have counted 3000 pulses on the
clock signal, then the elapsed time is 3000
microseconds.
TIMERS AND COUNTERS

Theory of operation
 In microcontrollers, the input clock to the internal timing
system is software selectable.
 The microcontroller’s own clock source (which is fixed)
can be selected as the clock input to the free running
counter module of the timing system. In such a case, the
timing system is known to be working as a timer because
it is driven by a fixed known frequency clock.
TIMERS AND COUNTERS

Theory of operation
 However, the input clock to the timing system can also be
provided through an external I/O pin of the
microcontroller in which case it is known to be working as
a counter, and will count the external pulses which may
appear at random intervals.
 These pulses could be manual inputs from a push button,
or more likely, would be produced by some other signal
source, like a road sensor that generates a pulse every
time a car is passed over it.
TIMERS AND COUNTERS

Theory of operation
 A functional structure of a simple timer module is shown
 This has a free running 16-bit up counter that increments its
value on each clock pulse. Thus, the 16-bit output count
represents the number of pulses arrived since the counter
was last reset to zero.
 We can convert this value into time interval by knowing the
frequency (or period) of the input clock signal.
TIMERS AND COUNTERS

Theory of operation
 Suppose, we want to measure the time elapsed between
any two successive events.
 Lets assume that when the first event occurs, the timer is
reset to zero, and when the second event occurs, the timer
output is 25000.
 If we know that the input clock has period of 1 µs, then the
time elapsed between the two events is 25000×1 µs = 25
milliseconds.
TIMERS AND COUNTERS

Theory of operation
 A more advanced timer has a prescaler.
 A prescaler is essentially a configurable clock-divider circuit.
Depending on the selected configuration bits, the prescaler
output could be the same as the input signal, or it may have
half the frequency, one-fourth the frequency, one-eighth
the frequency, etc.
TIMERS AND COUNTERS

Timers in microcontrollers have additional


configurable features such as:
 Enabling or stopping the counter
 Enabling or disabling interrupt generation when
the counter overflows
 Selecting the clock edge for incrementing the
counter, etc.
PIC Family Control Registers
• Uses a series of “Special Function Registers” for
controlling peripherals and PIC behaviors.
 STATUS  Bank select bits, ALU bits (zero, borrow, carry)
 INTCON  Interrupt control: interrupt enables, flags, etc.
 OPTION_REG  contains various control bits to
configure the TMR0 prescaler/WDT postscaler,
the External INT Interrupt
Special Function Register
”STATUS Register“
Special Function Register
”INTCON Register“
Special Function Register
OPTION_REG Register
TIMERS AND COUNTERS

• The PIC16F877A includes three timers:


Timer0, Timer1, and Timer2. Each timer has
unique features and applications.
1. Timer0
• Timer0 is an 8-bit timer/counter available in the
PIC16F877A microcontroller. It can function as a
timer or a counter, depending on the configuration of
the TMR0 module.
• Key Features:
 8-bit timer/counter
 Selectable internal or external clock source
 Prescaler available for dividing the clock frequency
 Overflow interrupt capability
1. Timer0
• Applications:
• Generating time delays
• Event counting
• Registers Associated with Timer0:
 TMR0: Holds the current value of the timer/counter.
 OPTION_REG: Configures the prescaler and clock
source.
1. Timer1
• Timer1 is a 16-bit timer/counter, which makes it
suitable for applications requiring higher precision.
Key Features:
 16-bit timer/counter
 Operates as a timer or counter
 Can use an external crystal oscillator for increased
accuracy
 Supports prescaler for frequency division
 Can trigger interrupts on overflow
1. Timer1
• Applications:
 Real-time clock implementation
 Pulse width measurement
• Registers Associated with Timer1:
 TMR1H and TMR1L: Hold the high and low bytes
of the timer value.
 T1CON: Configures the timer mode, clock source,
and prescaler.
1. Timer2
• Timer2 is an 8-bit timer/counter with additional
features. It is primarily used in Pulse Width
Modulation (PWM) and other periodic signal
generation.
• Key Features:
 8-bit timer/counter
 Built-in prescaler
1. Timer2
• Applications:
 PWM signal generation
 Servo motor control
• Registers Associated with Timer2:
 TMR2: Holds the current value of the timer.
 T2CON: Configures the prescaler, and timer mode.
Timer0
TIMER0 (TMR0)

 Timer0 is an 8-bit Timer/Counter module with the


following features:
1. 8-bit prescaler (shared with WDT).
2. Selectable internal or external clock source.
3. Interrupt on overflow.
4. Source edge selection when external clock source is
used (positive or negative going edge).
 To configure the Timer0 module the OPTION_REG Special
Function Register (SFR) is used.
Timer 0 Block Diagram

Fosc is the frequency of the instruction cycle clock, which is the CPU clock divided by 4
(each instruction takes four clock cycles). It is used in many places in the chip such as the
input to a timer.
Timer0 Operation Modes
• Timer Mode:
o In timer mode, Timer0 increments on every instruction cycle
(Fosc/4).
o A prescaler can be applied to divide the clock frequency for
longer delays.
• Counter Mode:
o In counter mode, Timer0 increments based on an external
signal applied to the T0CKI pin.
o The edge of the signal (rising or falling) is selectable via the
T0SE bit.
Interrupts in Timer0

• Timer0 generates an interrupt when it overflows from 0xFF to


0x00. The interrupt flag is:
 T0IF (Timer0 Overflow Interrupt Flag): Set when an
overflow occurs.
 The interrupt must be enabled using the T0IE (Timer0
Interrupt Enable) bit in the INTCON register.
Special Function Register
OPTION_REG Register
Using OPTION_REG Register to Configure TMR0

 The OPTION_REG register contains various control bits to


configure TMR0/WDT prescaler, timer TMR0, external
interrupt and pull-ups on PORTB.
 PSA (Prescaler Assignment Bit):
assigns prescaler (only one exists) to the timer or watchdog timer.
 1 - Prescaler is assigned to the WDT.
 0 - Prescaler is assigned to the TMR0.
 IF PSA is set (1), prescaler is assigned to
watchdog timer and PS2:PS0 have no effect
(TMR0 rate = 1:1).
 PS2, PS1, PS0 (Prescaler Rate Select bits):

 Prescaler rate is selected by combining these three bits. As


shown in the following table, prescaler rate depends on whether
prescaler is assigned to the timer (TMR0) or watch-dog timer
(WDT).
IF PSA =1 (prescaler is assigned to watchdog timer), TMR0 rate = 1:1
 T0SE (Timer0 Source Edge Select Bit ):
 This bit has an effect only when an external clock source only is used
(T0CKI = clock from RA4 pin)

 1 = TMR0 register increments on high-to-low transition on T0CKI pin

 0 = TMR0 register increments on low-to-high transition on T0CKI pin


Effect of Timer0 Source Edge Select Bit

T0SE=0

T0SE=1
 T0CS (Timer0 Clock Source Select bit):
 1 = Signal present on T0CKI (Timer0 Clock Input) pin (RA4 pin) used as
Timer0 clock source.

 0 = Internal instruction cycle clock used as source.

 Internal instruction cycle = (Microcontroller oscillator Frequency)/4

 These timers run at a speed of 1/4 of the clock speed. So, if we use 4
MHz crystal, the internal timer will run at 1 MHz.
TMR0

1 TMR0 As A Timer 2 TMR0 As A Counter

0 0 0 0 0 0 0 1 0 1 0 0 0

1/0
according to
prescaler rate
(1) TMR0 As A Timer
TMR0 Over Flow Time Out

 1 over flow TMR0 Time Out = (256 – T0) x prescale x 4 x clock_period

 T0 is the initial value assigned to TMR0


 clock_period = 1/f Sec

 IF T0 =0 , 1 over flow TMR0 Time Out is the maximum possible


value and is equal to:

1 over flow Max. TMR0 Time Out= 256 x prescale x 4 x


clock_period

 When over flow occurs (TMR0 value change from 255→0), the
timer0 interrupt flag bit T0IF (INTCON.T0IF) is set to 1.
 Example1:

If (1:256) Prescaler is assigned to TMR0 with f = 4MHz find:

1- The time of 1 over flow for TMR0

2- No. of required 1s over flows to get time equal to 1


sec.

Assume two cases:


 Case1: The initial value of TMR0 =0 (TMR0 =0;)

 Case2:The initial value of TMR0 =39 (TMR0 =39;)


Case1: The initial value of TMR0 =0 (T0 =0)
 1 over flow TMR0 Time Out = 256 x prescale x 4 x clock_period
-6
= 256 x 256 x 4 x 0.25*10 sec = 65536 μSec

1 over flow 65536 μSec

X over flow 1 Sec

1
X = = 15.2587 = 16
-6
65536 * 10

The required 1s over flows to get time equal to 1 sec = 16 over flows.
Case2: The initial value of TMR0 =39 (T0 =39)
 1 over flow TMR0 Time Out = (256-T0) x prescale x 4 x clock_period =
-6
(256-39) x 256 x 4 x 0.25*10 sec = 55552 μSec

1 over flow 55552 μSec

X over flow 1 Sec

X = 1
= 18
-6
55552* 10

The required 1s over flows to get time equal to 1 sec = 18 over flows.
 Example 2:

If (1:256) Prescaler is assigned to TMR0 with f = 4MHz find:

1- The time of 1 over flow for TMR0

2- No. of required 1s over flows to get time equal to


1.5 sec.

Assume two cases:


 Case1: The initial value of TMR0 =0 (TMR0 =0;)

 Case2:The initial value of TMR0 =39 (TMR0 =39;)


Case1: The initial value of TMR0 =0 (T0 =0)
 1 over flow TMR0 Time Out = 256 x prescale x 4 x clock_period
-6
= 256 x 256 x 4 x 0.25*10 sec = 65536 μSec

1 over flow 65536 μSec

X over flow 1.5 Sec

1.5
X = = 22.89 = 23
-6
65536 * 10

The required 1s over flows to get time equal to 1.5 sec = 23 over flows.
Case2: The initial value of TMR0 =39 (T0 =39)
 1 over flow TMR0 Time Out = (256-T0) x prescale x 4 x clock_period =
-6
(256-39) x 256 x 4 x 0.25*10 sec = 55552 μSec

1 over flow 55552 μSec

X over flow 1.5 Sec

X = 1.5
= 27
-6
55552* 10

The required 1s over flows to get time equal to 1.5 sec = 27 over flows.
 Example 3:

Write a PIC16F877A C program to toggle all bits of PORTB

every 1 sec using timer0. Assign (1:256) to TMR0 with

f = 4MHz.
 OPTION_REG Configuration:

0 0 0 0 0 1 1 1

OPTION_REG = 0x07;
 The required 1s over flows to get time equal to 1 sec
• Assume The initial value of TMR0 =0 (T0 =0)

• 1 over flow TMR0 Time Out = 256 x prescale x 4 x clock_period


-6
= 256 x 256 x 4 x 0.25*10 sec = 65536 μSec

1 over flow 65536 μSec

X over flow 1 Sec

1
X = = 15.2587 = 16
-6
65536 * 10

The required 1s over flows to get time equal to 1 sec = 16 over flows.
void main() {
int Num=0;
TRISB = 0x00; // PORTB is configured to be output
PORTB = 0X55; // Initialize PORTB
OPTION_REG = 0x07; // Prescaler (1:256) is assigned to the timer TMR0
TMR0 = 0; // Timer0 counts from 0 to 255
while(1)
{
if (T0IF == 1) // check if 1 over flow occurs // (INTCON.T0IF == 1)
{
Num ++; // 1 over flow occurs causes Num to be incremented by 1
T0IF =0;
TMR0 = 0;
}
if (Num == 16)
{
PORTB = ~PORTB; // Toggle PORTB every 1 Sec
Num = 0;
}
}
}
void main() {
int Num=0;
TRISB = 0x00; // PORTB is configured to be output
PORTB = 0X55; // Initialize PORTB
OPTION_REG = 0x07; // Prescaler (1:256) is assigned to the timer TMR0
TMR0 = 0; // Timer0 counts from 0 to 255
while(1)
{
while(Num < 16)
{
if (T0IF == 1) // check if 1 over flow occurs // (INTCON.T0IF == 1)
{
Num ++; // 1 over flow occurs causes Num to be incremented by 1
T0IF =0;
TMR0 = 0;
}
}
PORTB = ~PORTB; // Toggle PORTB every 1 Sec
Num = 0;
}
}
EX2: TMR0 As A Counter

 Write a microcontroller PIC16F877A C program to use


TMR0 as a counter such that:

 The counter input is connected to a push button so that


any button press causes timer TMR0 to count one pulse.

 When the number of pulses matches the number stored in


the TEST register, a logic one (5v) appears on the RD3 pin.

 Assume the value that stored in the TEST register is 7.


T0CKI
void main() {
Int TEST = 7; // Constant TEST = 5
PORTA = 0; // Reset port A
TRISA = 0xFF; // All portA pins are configured as inputs
PORTD = 0; // Reset port D
TRISD = 0b11110111; // Pin RD3 is configured as an output, while the rest are configured as inputs

OPTION_REG.F5 = 1; // Counter TMR0 receives pulses through the RA4 pin (T0CKI)
OPTION_REG.F3 = 1; // Prescaler rate is 1:1
TMR0 = 0; // Reset timer/counter TMR0
do {
if (TMR0 == TEST) // Does the number in timer match the value stored in TEST?
PORTD.B3 = 1; // (Set RD3 to 1 if the number in timer match the value stored in TEST
}
while (1); // Remain in endless loop
}

You might also like