10.1 10.2 Disclaimer 1 • This is just an introduction to the topic of interrupts. You are not meant to master these Unit 10 right now but just start to use them • We will cover more about them as we investigate other modules that can make use Exceptions & Interrupts of them 10.3 10.4 An Analogy Exceptions • In computer systems we may NOT know when • Scenario: – External hardware events will occur. – You're studying (i.e. listening to music and • Can you think of an example? watching Netflix) but all of a sudden you get a text – Errors will occur message. What do you do? • Exception processing refers to – You stop what your doing and message back – Handling events whose timing we cannot predict – When you're done you go back to studying (i.e. • 3 questions to answer: playing a video game or going to get coffee) – Q: Who detects these events and how? A: The hardware – Q: How do we respond? A: Calling a pre-defined SW function • This is what computers do when an – Q: What is the set of possible events? A: Specific to each processor ______________________ occurs
10.5 10.6 What are Exceptions? Interrupt Exceptions • Two methods for processor and I/O devices to notify each other of • Definition : Any event that causes a _______________ events ___________________ – _________________________ (responsibility on proc.) • Processor has responsibility of checking each I/O device – "Exceptions" is a broad term to catch many kinds of events • Many I/O events happen infrequently (1-10 ms) with respect to the processors that interrupt normal software execution ability to execute instructions (1-100 ns) causing the loop to execute many times • Examples – _________________ (responsibility on I/O device) • I/O device notifies processor only when it needs attention – Hardware Interrupts / __________ Events [Focus for today] • PC: Handling a keyboard press, mouse moving, USB data transfer, etc. Recall: Once the A-to-D converter has started we need With Interrupts: We can ask the ADC to to wait until the ADSC bit is 0 (i.e. keep waiting 'while' "interrupt" the processor when its done so the • Arduino: Value change on a pin, ADC conversion done, Timers, etc. ADSC bit is 1) processor doesn't have to sit there polling – Error Conditions [Focus for some other time] while (ADCSRA & (1 << ADSC)); • Invalid address, illegal memory access, arithmetic error I/O Device (ADC) I/O Device (ADC) (e.g. divide by 0) ADCSRA ADCSRA Proc. Proc. – System Calls / Traps [Focus for some other time] ADSC ADSC • User applications calling OS code Polling Loop Interrupt 10.7 10.8 Recall: Instruction Cycle HW Detects Exceptions • There's actually a 4 th step • Processor hardware performs the same 3-step • After finishing each instruction Did an exception 4 occur? the processor hardware checks process over and over again Processor Processor Add the Add the Arithmetic for ____________________ Arithmetic 3 3 as it executes a software specified values specified values Circuitry Circuitry automatically (i.e. this is built into program Decode Decode 2 2 It’s an ADD It’s an ADD Circuitry the hardware) Circuitry – Fetch an instruction from – Fetch an instruction from memory Fetch Fetch 1 1 memory Instruction Instruction – Decode the instruction – Decode the instruction • Is it an ADD, SUB, etc.? – Execute the instruction • Is it an ADD, SUB, etc.? ADD ADD SUB • Perform the specified operation SUB – Execute the instruction CMP CMP – Check for exceptions • Perform the specified operation Memory • If so, pause the current program and Memory • This process is known as the go execute other software to deal with the exception Instruction Cycle
10.9 10.10 SW Handles Exceptions When Exceptions Occur… #include<avr/io.h> #include<avr/interrupt.h> #include<avr/io.h> • When exceptions occur, what • How does the processor know #include<avr/interrupt.h> should happen? void codeToHandleInterrupt(); which function to call unsigned char value = 0; – We could be anywhere in our int main() void adcFinished(); "automatically" when an { software program…who knows // this is just generic code int main() interrupt occurs where // for a normal application { PORTC |= (1 << PC2); // this is just generic code • We must tell the processor in • Common approach… int cnt = 0; PORTC |= (1 << PC2); If an interrupt while(1){ ADCMUX = 0x61; – 1. ___________ in current code __________ which function to happens here… if( PINC & (1 << PC2) ) { // start first conversion cnt++; ADCSRA |= (1 << ADSC); and disable other ___________ associate (i.e. call) with the If an interrupt PORTD = segments[cnt]; while(1) happens here… – 2. Automatically have the } { /* do useful work here */} various exceptions it will check } return 0; …the processor will processor call some return 0; } automatically call a for function/subroutine to handle } ADC_vect is not an argument. It identifies the predetermined function ISR as being for the ADC… the issue (a.k.a. ____________ (a.k.a. ISR) • Just like a waiver forms asks for ISR() ISR(ADC_vect) __________________ or ISR ) { { an emergency contact to call if // do something in response // ADC is now done – 3. ___________ interrupts & // to the event value = ADCH; …then resume the code …and the processor } something bad happens, we // output value resume normal processing back it was executing will automatically call a PORTD = value; previously predetermined function in original code indicate what function to call // start next conversion Important Point: ADCSRA |= 0x40; HW detects exceptions. when an interrupt occurs } Software handles exceptions. 10.11 10.12 Function Calls vs. Interrupts Normal function calls Interrupts • _______________ : Called • _____________ : Called whenever an event occurs (can be anywhere in our whenever the program reaches program when the ISR needs to be that point in the code called) • Programmer can pass arguments – Requires us to know in advance and receive return values which ISR to call for each possible exception/interrupt – Use ISR( interrupt_type ) naming scheme in the Arduino to make this AVR INTERRUPT SOURCES association • No ____________ or _______ values – How would we know what to pass if we don't know when it will occur – Generally interrupts update some global variables
Recommend
More recommend