6 Working With Time Interrupts Counters and Timers
6 Working With Time Interrupts Counters and Timers
6 Working With Time Interrupts Counters and Timers
Chapter Six
Outline
Interrupts
Interrupt Structures Interrupt Demonstration Program Dealing with Multiple Interrupt Sources Context Saving
; ;Port A all output ;Port B: Bit 0 = Interrupt input ; org 00 ;Reset start goto start
org 04 ;Int Service Routine start goto Int_Routine
Dr. Gheith Abandah 7
bcf status,rp0 ;select bank 0 bsf intcon,inte ;enable external interrupt bsf intcon,gie ;enable global int ;Remove semi-colon from following instruction to change ;interrupt edge ; bsf option_reg,intedg
Dr. Gheith Abandah 8
10
Int_Routine bcf status,0 ;clear the Carry flag movlw 0ff ;change W reg value bcf intcon,intf retfie end
Dr. Gheith Abandah 11
...
Critical Regions
In critical regions, we cannot allow the intrusion of an interrupt. Critical regions generally include all timesensitive activity and any calculation where the ISR makes use of the result. Disable, or mask, the interrupts for their duration, by manipulating the enable bits in the INTCON register.
Dr. Gheith Abandah 13
14
Timer Applications
(a) Measure the time between two events (b) Measure the time between two pulses (c) Measure a pulse duration
15
16
17
Option Register
T0CS: Clock source select T0SE: Source edge select PSA: Prescaler assignment bit PS2:PS0: Prescaler rate select
18
19
loop
21
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 22
23
24
Sleep Mode
When the sleep instruction 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
26
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.
27