interrupts, counter and timers. interrupts (1) interrupt-driven i/o uses the processor’s interrupt...
TRANSCRIPT
![Page 1: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/1.jpg)
Interrupts, Counter and Timers
![Page 2: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/2.jpg)
Interrupts (1)
Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow execution of an I/O service routine
An interrupt is like a hardware-initiated subroutine call
2
![Page 3: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/3.jpg)
Interrupts (2)Advantages
Immediate response to I/O service request
Normal execution continues until it is known that I/O service is needed
DisadvantagesCoding complexity for interrupt service routines
Extra hardware neededProcessor’s interrupt system I/O device must generate an interrupt request3
![Page 4: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/4.jpg)
Basic Steps for an Interrupt (1)An interrupt cycle begins at the next fetch cycle if: Interrupts are enable (GIE = 1) Interrupt request is active (I=1)
Program counter (PC) is saved on the stackPC is loaded with address of interrupt service routine (ISR)
Different schemes for determining this address
Fixed location 0x0004 on the PIC processor
4
![Page 5: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/5.jpg)
Basic Steps for an Interrupt (2)
Interrupt service routine executesSaves any registers that will be altered so that normal program flow is not disturbed
Performs input and/or output operations to clear the interrupt request
Restores saved registersReturns
Normal execution resumes5
![Page 6: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/6.jpg)
Program Flow for an Interrupt
6
![Page 7: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/7.jpg)
Example Pseudo code for an ISR
ISR: save register(s)if (IN_RDY == 1)
input dataif (OUT_RDY == 1)
output datarestore register(s)return
7
![Page 8: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/8.jpg)
Example Error if Registers are not Saved before Executing ISR
8
![Page 9: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/9.jpg)
16F84 INTCON Register
9
![Page 10: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/10.jpg)
16F84 Interrupt Structure
10
![Page 11: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/11.jpg)
Outline of 16F84 Interrupt Structure
11
![Page 12: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/12.jpg)
16F84 Timer/Counter
12
![Page 13: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/13.jpg)
16F84 OPTION Register
13
![Page 14: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/14.jpg)
Relationship between TMR0 and Prescaler
14
![Page 15: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/15.jpg)
Timing Diagram of TMRO Interrupt
15
![Page 16: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/16.jpg)
Determining Timer SettingsConsider the “wait for 500ms” for LED
problem…Fosc = 4 MHz, internal timer can be driven
by Fosc/4 = 1MHzTo have a 500ms interrupt on overflow
interval, desired scale = 500ms/1μs = 500,000
Maximum scale provided by Timer0 is 256*256=65,536
Probably we can set up an interrupt interval shorter than 500ms, say M ms, then toggle the LED after every N interrupts, where M*N = 500. The possible value of M has constraints. It is helpful to
express the needed scale in terms of its prime factors, then allocate these among the prescaler, PR and the TMR0 register.
let’s try M=4 and N=125 for this example.16
![Page 17: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/17.jpg)
Initialize the Timer0Desired scale for 4ms timing is
4ms/1μs = 4,000. Play with the math we have
32*125 =4,000In order to get an exact 4 ms timing, we’ll need
to set the timer as follows:PS2 PS1 PS0 =100 ; set prescaler to 32TMR0 = 256-125 =131 ; initialize TMR0T0IF = 0 ; clear timer0 interrupt
flagTOIE =1 ; enable timer0 interrupt
17
![Page 18: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/18.jpg)
Timer0 Initialization CodeInitTmr0
bcf STATUS, RP1 ;select bank 1bsf STATUS, RP0movlw B’11010100’ ; set up OPTION, prescaler and timer0 resourcesmovwf OPTIONbcf STATUS, RP0 ; select bank 0movlw D’131’movwf TMR0 ; initialize TMR0 bcf INTCON, T0IF ; clear Timer0 interrupt flagbsf INTCON, T0IE ; enable Timer0 interrupt sourcebsf INTCON, GIE ; enable global interruptsreturn
18
![Page 19: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/19.jpg)
“Wait for 500ms” --- Use TimerSince our Timer is set to overflow every 4ms, waiting 4ms
really means waiting until the timer overflows. We will write an interrupt service routine to handle the timer overflow in a future class. Now we assume that in the interrupt service routine (ISR), BLNKCNT is decremented by 1 each time an overflow occurs.
Waiting 500ms means waiting timer overflow 125 times, which is counted by BLNKCNT.
Five00Msmovlw 124 ; initialize BLNKCNT to 124, why???movwf BLNKCNT ; BLNKCNT is a variable
wait500btfss BLNKCNT, 7 ; wait for 00000000 to 11111111 changegoto wait500 ; wait if notreturn ; and return if yes
19
![Page 20: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/20.jpg)
Put it All Together - The Mainline Program
Mainlinecall Initial ; Initialize PortBcall InitTmr0 ; Initialize Timer0
MainLoopcall Blink ; Blink LEDcall Five00ms ; Insert 500ms delaygoto MainLoop
Note: BLNKCNT will be decremented in the interrupt service routine which we will talk about in the future!20
![Page 21: Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow](https://reader031.vdocuments.site/reader031/viewer/2022032205/56649e7b5503460f94b7ce05/html5/thumbnails/21.jpg)
Watchdog TimerFree running counter with its own oscillatorCan be enabled or disabled through special
directives in the programUsually used to reset the controller if the
program gets stuck or works incorrectlyProgrammer needs to be careful when using
it so that it does not cause problems
21