Working with time: interrupts,
counters and timers
Chapter 3
Dr. Gheith Abandah 1
Outline
•Interrupts
–Interrupt Structures
–Interrupt Demonstration Program
•Counters and Timers
•Watchdog Timer
•Sleep Mode
Dr. Gheith Abandah 2
Interrupt
Dr. Gheith Abandah 3
Interrupts are one of the most powerful features
of PIC microcontrollers interrupts make it
possible to create applications that can respond
to external stimulus in real time. An interrupt is
basically an event that requires the
microcontroller to stop normal program
execution and then to jump to execute a
program code related to the event causing the
interrupt.
The 16F84A Interrupt Sources
Dr. Gheith Abandah 4
1.External interrupt. It shares a pin with Port B,
bit 0. It is edge triggered.
2.Timer 0 overflow. It occurs when the timer’s
8-bit counter overflows.
3.Port B interrupt on change. This interrupts
when a change is detected on any of the
higher 4 bits of Port B.
4.EEPROM write complete.
The 16F84A Interrupt Structure
Dr. Gheith Abandah 5
INTCON:
SFR 0Bh
Dr. Gheith Abandah 6
Dr. Gheith Abandah 7
External interrupt
Dr. Gheith Abandah 8
Timer0interrupt
Dr. Gheith Abandah 9
Dr. Gheith Abandah 10
Counters and Timers
•A digital counter is
usually made of flip
flops and counts up or
down.
•When the triggering
event is a constant
frequency clock, we get
a timer.
Dr. Gheith Abandah 11
Timer Applications
(a)Measure the time
between two events
(b)Measure the time
between two pulses
(c)Measure a pulse
duration
Use interrupts
Dr. Gheith Abandah 12
Hardware-Generated Delays
•In software-generated delays, the CPU is tied
up during the delay time.
•Can use Timer 0 for delays.
•The timer overflow interrupt is used to inform
the CPU when the delay time is up.
•Example:
–Clock = 800 KHz → Fosc/4 = 200 KHz → T = 5 μs
–Preselect = 8 for 125 count → 125 x 8 x 5 μs = 5.00 ms
–Need to set T0 at 256-125 = 131 to get overflow after 5 ms
Dr. Gheith Abandah 16
The Watchdog Timer
•A big danger with any computer-based system
is that the software fails in some way and that
the system locks up or becomes unresponsive.
•The WDT continually counts up. If it ever
overflows, it forces the microcontroller into
reset.
•If the instruction clrwdtis not executed every
(18 ms * prescaler rate), overflow occurs.
Dr. Gheith Abandah 17
Sleep Mode
•When the sleepinstruction is executed, the
PIC halts execution and enters power saving
mode.
•It awakes at:
–External reset (MCLR)
–WDT wake-up
–Occurrence of an enabled interrupt
Dr. Gheith Abandah 18
Summary
•Interrupts and counter/timers are important
hardware features of almost all microcontrollers.
•They both carry a number of important hardware
and software concepts, which must be understood.
•The basic techniques of using interrupts and
counter/timers have been introduced in this
chapter. There is considerably increased
sophistication in their use in more advanced
applications.
Dr. Gheith Abandah 19