Docsity

Timer-0 Configuration and Operation in PIC16F877A Microcontroller, Summaries of Microcontrollers

Partial preview of the text

Download Timer-0 Configuration and Operation in PIC16F877A Microcontroller and more Summaries Microcontrollers in PDF only on Docsity!

Introduction to

Timers/Counters

Characteristics of Timers

An n-bit Timer/Counter divides its input clock frequency by , Where n is no. of bits of timer

OR

An n-bit Timer/Counter counts from 0 to

When a Timer/Counter reaches its maximum value it simply overflows to zero on the next clock

pulse

The overflow of the timer is indicated by an overflow flag

The overflow flag is usually automatically SET by the hardware on overflow and its has to

cleared in software

PIC16F877A has three independent timers

๏ƒ˜ Timer-0 (8-bit Timer)

๏ƒ˜ Timer-1 (16-bit Timer)

๏ƒ˜ Timer-2 (8-bit Timer)

๏ƒ˜ Suppose the microcontroller is clocked by a 20 MHz clock, thus for Timer-

The can also be called as overflows/sec

๏ƒ˜ if we apply an input clock of 5 MHz then the output clock will be approximately 19.53 KHz

๏ƒ˜ In other words the Counter will overflow 19531.25 times in one second

๏ƒ˜ Since Timer-0 overflow is indicated by an overflow flag therefore we can count the number of

overflows and thus generate a precise one second.

๏ƒ˜ This can be accomplished by constantly checking the Timer-0 Overflow flag, if the Overflow Flag

becomes HIGH then we clear the overflow flag and then increment a variable.

๏ƒ˜ When the incremented variable reaches a value equal to the calculated overflows/sec then this means

that exactly one second has passed

Mathematical Calculations

Flow

Diagram for

one Sec

Generation

Has Timer

Overflowed

Initialize Timer-

Increment Overflow Variable

Is Variable

=

Overflows/s

ec

Clear Overflow Flag

Clear Overflow Variable

Execute any desired

function

(Remember this

function will be

executed every second)

Prescaler

If we use a prescaler of 1:16 with the same oscillator frequency, then

Overflows have reduced because of the prescaler

Other division ratios can be selected to further divide these overflows

Timer-0 Configuration

OPTION Register

Clock Source

Timer-0 can be clocked by

Clock derived from the main oscillator () also known
as the internal instruction clock cycle
Clock Signal Applied on an external I/O pin
(RA4/T0CKI)

The external clock signal be programmed for

Rising OR Falling Edges

The T0CS (Timer-0 Clock Source) bit decides the
clock source of Timer-

If we SET this bit then the clock source is External

If we CLEAR this bit, then the clock source is Internal

Prescaler Configuration

The prescaler provides a pre-divided clock to Timer-0 or the Watch Dog Timer (WDT)

The prescaler is either connected to the WDT timer or to Timer-

The PSA (Prescaler Assignment) bit decides whether the prescaler is connected to Timer-0 or

the WDT

Prescaler

8-bit Timer/Counter

Watch Dog Timer

(WDT)

PSA = 1

PSA = 0

๐ถ๐‘™๐‘˜ ๐‘œ๐‘ข๐‘ก

=

๐ถ๐‘™๐‘˜ ๐‘–๐‘›

256

๐‘“๐‘œ๐‘Ÿ ๐‘ƒ๐‘†๐ด = 1

๐ถ๐‘™๐‘˜ ๐‘œ๐‘ข๐‘ก

=

๐ถ๐‘™๐‘˜ ๐‘–๐‘›

( 256 ) (^) ร— ( ๐‘ƒ๐‘Ÿ๐‘’๐‘ ๐‘๐‘Ž๐‘™๐‘’๐‘Ÿ ๐‘…๐‘Ž๐‘ก๐‘–๐‘œ )

๐‘“๐‘œ๐‘Ÿ ๐‘ƒ๐‘†๐ด = 1

Prescaler Configuration โ€“ Division Ratio

If the prescaler is assigned to Timer-0 then we have to configure the prescaler division ratio as

well

The first three bits in the OPTION register decide the prescaler division ratio

Timer-0 Internal

Architecture

๏ƒ˜ Clock Source Internal

(T0CS = 0)

๏ƒ˜ Precaler connected with

Timer-0 (PSA = 0)

Timer-0 Internal

Architecture

๏ƒ˜ Clock Source External

(T0CS = 1)

๏ƒ˜ Precaler connected with

WDT (PSA = 1)

Timer-0 Configuration

Assume Fosc = 20 MHz
Clock source = Internal
Prescaler Connected to WDT
0 x 1 x x x

Timer-0 Configuration

Assume Fosc = 20 MHz
Clock source = Internal
Prescaler Connected to Timer-
Prescaler Ratio 1: