Вы находитесь на странице: 1из 4

MENU

Embedded Lab
An online teaching laboratory for Microcontrollers and Embedded Systems
TIMERS AND COUNTERS
Posted on November 11, 2010
by R-B
One comment
|
Introduction
Most microcontrollers are equipped with one or more precision timing systems that can be used to perform a variety of precision timer functions including
generating events at specific times, determining the duration between two events, or counting events. Example applications that require generating
events include generating an accurate 1 Hz 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.
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 2 -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. 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 clocks frequency is 1 MHz (period 1 s), and
we have counted 3000 pulses on the clock signal, then the elapsed time is 3000 microseconds.
In microcontrollers, the input clock to the internal timing system is software selectable. The microcontrollers 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. 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.
A functional structure of a simple timer module is shown below. 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.
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 250001 s = 25 milliseconds.
Since this timers counter can count from 0-65535 (2 -1), this particular arrangement can measure time ranging from 0 to 65535 x 1 s =
65.535 milliseconds, with a resolution of 1 s. The maximum time interval a timer can measure is known as the timers range, whereas the resolution of a
timer defines the minimum interval it can measure.
The timer module has an additional output, overflow flag, that indicates the counter reaches its maximum output, in which case the timer rolls over to 0. In
microcontrollers, the built-in timers generate timer overflow interrupts when they roll over back to 0 after reaching their maximum counting range. We can
count the number of times the timer overflows, and thus extend the range of time we can measure. In such a case,
elapsed time = (stop count start count) + number of overflows x (2 -1),
where, n = 16 for a 16-bit counter.
Embedded System Embedded Projects Embedded Displays Embedded Control
8 Like Like
Software Defined Radio
analog.com/Software-Defined-Radio
Get Design Friendly RF Transceiver Solution in a Single Chip Here Now.
n
16
n

Share
converted by Web2PDFConvert.com
Embedded Lessons
where, n = 16 for a 16-bit counter.
A more advanced timer has a prescaler (shown below). 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. Thus, a prescaler can extend a timers range by reducing its resolution. For example, if we use a prescaler to our previous 16-bit timer to
divide the clock frequency by 8, then the new range of the timer will be 65535 x 8 s = 524.280 milliseconds. But the new resolution is reduced to 8 s.
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.
Related posts:
1. Lab 11: Multiplexing seven segment LED displays
2. Netduino Day 3 Multiplexed Seven-Segment LED displays
3. Lab 17: Sleep and Wake PIC microcontrollers
4. Netduino Day 4 : Eight Digit Seven Segment LED Display with MAX7219
tagged with counter, prescaler, timer
ONE COMMENT
Pingback: pic timers and prescal of timers in it
LEAVE A REPLY
Your email address will not be published. Required fields are marked *
Name *
Email *
Website
Comment

8 Like Like
Embedded
Instrumentation
innovative-dsp.com
Kintex-7 FPGA, Intel CPU, FMC Sites
Download datasheets Now!
26 4 4 1387

converted by Web2PDFConvert.com
Post Comment
2
Follow Follow @EmbeddedLab @EmbeddedLab
Read Our Privacy Policy!
SUBSCRIBE
Subscribe through email Sign Up
EASY PULSE SENSOR
Easy Pulse Sensor is designed for hobby and educational applications to illustrate the principle of finger photoplethysmography (PPG) as a non-invasive
technique for detecting cardio-vascular pulse wave. Read More ...
Search...
Software Defined
Radio
analog.com/Software-Defined-Radio
Get Design Friendly RF
Transceiver Solution in a Single
Chip Here Now.
Embedded Lab
6,179 people like Embedded Lab.
Facebook social plugin
Like Like
Embedded Microcontrollers
Embedded Based Projects
Applications in Embedded
converted by Web2PDFConvert.com
2014 Embedded-Lab. All Rights
Reserved.
zeeDynamic
Theme
US customers buy here
International customers buy here
SERIAL 7-SEGMENT LED DISPLAYS
7-segment LED displays are fun way of displaying numeric sensor readings in Arduino or any other microcontroller-based projects. The downside is they
are resource hungry and requires lots of I/O pins and CPU time for continuously displaying the readings. We have designed varieties of 7-segment displays
that support SPI interface and allows you an easy control of every LED segments using only 3 I/O pins of your MCU.
Visit our Tindie store
Get 10% OFF with this code: 164E1CA for limited time only.
CATEGORIES
555 Timer (9)
Analog (3)
Arduino (57)
AVR Projects (37)
AVR Tutorials (5)
chipKIT (15)
dsPIC (1)
Embedded Lab Projects (60)
Embedded Labs (25)
Embedded Lessons (39)
MCU develeopment tools (11)
Microcontroller Programmers (6)
MSP430 Launchpad (2)
Netduino (9)
PIC Projects (80)
PIC Tutorials (44)
PIC18F (10)
Power Supply (8)
Processing (3)
Product Review (16)
Products (32)
Raspberry Pi (4)
Robotics (7)
Tech News (94)
Tips and Tricks (67)
Uncategorized (2)
Go to Top| Contact Us | Privacy Policy | Log In
Embedded CPU
Embedded SW
Embedded Work
converted by Web2PDFConvert.com

Вам также может понравиться