Slides created by: Professor Ian G. Harris Interrupts  Embedded systems often perform some tasks which are infrequent and possibly unpredictable Hang.

Slides:



Advertisements
Similar presentations
Interrupts, Low Power Modes and Timer A (Chapters 6 & 8)
Advertisements

More fun with Timer/Counters
CSCI 4717/5717 Computer Architecture
COMP3221: Microprocessors and Embedded Systems Lecture 16: Interrupts II Lecturer: Hui Wu Session 2, 2005.
Mark Neil - Microprocessor Course 1 Timers and Interrupts.
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
Computer System Structures memory memory controller disk controller disk controller printer controller printer controller tape-drive controller tape-drive.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
Pulse Width Modulation modes
The 8051 Microcontroller and Embedded Systems
INTERRUPTS PROGRAMMING
16-Bit Timer/Counter 1 and 3 Counter/Timer 1,3 (TCNT1, TCNT3) are identical in function. Three separate comparison registers exist. Thus, three separate.
1 Timing System Timing System Applications. 2 Timing System components Counting mechanisms Input capture mechanisms Output capture mechanisms.
Embedded Systems 7763B Mt Druitt College of TAFE
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
Ch. 9 Interrupt Programming and Real-Time Sysstems From Valvano’s Introduction to Embedded Systems.
7/23 Timers in Coldfire Processor Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee (480)
Timer/counter Chapter 12
A Few Words From Dilbert
Unit 10.2 Timer Examples. Example – Music Generation Channel 6 – Set up as a timer Output to Generate Square Waves Channel 4 – Set up as a timer Output.
MICROPROCESSOR INPUT/OUTPUT
MCU: Interrupts and Timers Ganesh Pitchiah. What’s an MCU ?
PIC16F877 ISR Points to note Interrupt Triggers –Individual interrupt flag bits are set, regardless of the status of their corresponding mask bit, PEIE.
Advanced uC Session Speaker : Chiraag Juvekar Jan 13, 2011 Speaker : Chiraag Juvekar Jan 13, 2011.
Timer Timer is a device, which counts the input at regular interval (δT) using clock pulses at its input. The counts increment on each pulse and store.
AVR Programming: Timers October 8, What is a timer? A register that keeps track of a current value This value is updated at a certain frequency.
13-Nov-15 (1) CSC Computer Organization Lecture 7: Input/Output Organization.
Interrupts An interrupt is an exception, a change of the normal progression, or interruption in the normal flow of program execution. An interrupt is essentially.
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
CS-280 Dr. Mark L. Hornick 1 Atmel Timer/Counter System Most microcontrollers include some type of timer system Facilitates real-time monitoring and control.
1 Interrupts, Resets Today: First Hour: Interrupts –Section 5.2 of Huang’s Textbook –In-class Activity #1 Second Hour: More Interrupts Section 5.2 of Huang’s.
1 68HC11 Timer HC11 or HC12: Chapter HC11 Timer Subsystem  Several timing functions: Basic timing Basic timing Real time interrupts Real time.
Interrupts  An interrupt is any service request that causes the CPU to stop its current execution stream and to execute an instruction stream that services.
Timers and Interrupts Mark Neil - Microprocessor Course.
Timers and Scheduled Interrupts
Processor Structure and Function Chapter8:. CPU Structure  CPU must:  Fetch instructions –Read instruction from memory  Interpret instructions –Instruction.
Interrupt On a very basic level, an interrupt is a signal that interrupts the current processor activity. It may be triggered by an external event (change.
MICROPROCESSOR DETAILS 1 Updated April 2011 ©Paul R. Godin prgodin gmail.com.
Lecture 3 CSE 341 – Microprocessors Lecture 3 Md. Omar Faruqe UB 1228
Mark Neil - Microprocessor Course 1 Timers and Interrupts.
CS-280 Dr. Mark L. Hornick 1 Sequential Execution Normally, CPU sequentially executes instructions in a program Subroutine calls are synchronous to the.
INSTITUTE: INSTITUTE:PARUL INSTITUTE OF TECHNOLOGY BRANCH : BRANCH :B.E. E.C.5 TH SEM. SUBJECT:MICROCONTROLLER & INTERFACING TOPIC:AVR INTTRUPT TOPIC:AVR.
16-Bit Timer/Counter 1 and 3
Timers and Scheduled Interrupts
Outline Introduction to Arduino UNO Programming environment setup GPIO
V.V.P. ENGINEERING COLLEGE,RAJKOT
Timer and Interrupts.
Interrupts In 8085 and 8086.
AVR Addressing Modes Subject: Microcontoller & Interfacing
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
COMP2121: Microprocessors and Interfacing
Timer/Counter Modified from Dr. Lam Phung’s Slides.
16-bit Timer/Counter1 Features
8-bit Timer/Counter0 with PWM
ATmega103 Timer0 and Interrupts
DC motor and PWM.
8-bit Timer/Counter2 with PWM and Asynchronous Operation
Wave Generation and Input Capturing
Interrupt Chapter 10.
Interrupt handling Explain how interrupts are used to obtain processor time and how processing of interrupted jobs may later be resumed, (typical.
Embedded System Development Lecture 7 2/21/2007
An Embedded Software Primer
Timer/Counter Timer/Counter 0 Timer/Counter 1 Timer/Counter 2 8 bit
COMP3221: Microprocessors and Embedded Systems
Presentation transcript:

Slides created by: Professor Ian G. Harris Interrupts  Embedded systems often perform some tasks which are infrequent and possibly unpredictable Hang up a VOIP phone when receiver is dropped Apply brakes when brake pedal is pressed  Regular tasks must be temporarily stopped to deal with the event  Interrupts are the unusual events  Interrupt handlers, or interrupt service routines, are programs which perform necessary tasks

Slides created by: Professor Ian G. Harris Interrupt Handling  Interrupt can be invoked at any time  Regular code must stop for a while

Slides created by: Professor Ian G. Harris Saving and Restoring Context  Interrupt should not interfere with normal tasks  Need to save all used registers at the beginning and restore them at the end  Stack is typically used for temporary storage Last in, first out (LIFO) push, pop

Slides created by: Professor Ian G. Harris Disabling Interrupts  Some events should be ignored completely  Some tasks are time-critical and should not be interrupted X-ray emitter in radiation therapy  Interrupts can be disabled (usually by setting a register)  Nonmaskable interrupt cannot be disabled For critical events (like loss of power)

Slides created by: Professor Ian G. Harris Interrupt Vectors  Interrupt vector is a pointer to an interrupt in memory  Interrupt number is used to index the table  Interrupt vector table holds pointers to all interrupts  Table location may be fixed or placed in a known register

Slides created by: Professor Ian G. Harris Shared-Data Problem  Interrupts should not change data that the another task is using R1 = 3 + 5; R1 = R1 / 2; print R1 R1 = R1 + 1 Main TaskInterrupt Saving and restoring registers helps Cannot do the same for memory - Hard to predict which locations to save

Slides created by: Professor Ian G. Harris Shared-Data Example Main task checks two temperatures to make sure they are equal Interrupt reads the two temperatures periodically Interrupt can make the temperatures seem out of sync Bugs are intermittent

Slides created by: Professor Ian G. Harris Shared-Data Fix? Maybe problem can be fixed Place read and compare on same line No, assembly does not match C code

Slides created by: Professor Ian G. Harris Shared-Data Solution  Identify each critical region where interrupts could be disruptive Identify code regions that use memory written by an interrupt Reading more than one address can lead to inconsistency  Disable interrupts before the region, enable interrupts after

Slides created by: Professor Ian G. Harris Shared-Data Solution Example  Critical region is where temps are both read by main task  Data reading is “atomic”

Slides created by: Professor Ian G. Harris Nested Critical Regions void functionA(void) { disableInterrupts( ); --- enableInterrupts( );} void functionB(void) { disableInterrupts( ); --- functionA( ); --- enableInterrupts( ); --- some code which may be interrupted }  Critical regions inside function calls can conflict  functionA terminates the critical region of functionB early

Slides created by: Professor Ian G. Harris Possible Nesting Solution Code can check to see if interrupts are disabled before disabling and then enabling them. void functionA(void) { if (InterruptsDisabled()) dis=0; else dis=1; if (dis) disableInterrupts( ); --- some code which must not be interrupted If (dis) enableInterrupts( ); }

Slides created by: Professor Ian G. Harris Another Nesting Solution Could use a counter in Enable/Disable routine keep track of “levels” of disabling. Enabled only when all counts are zero. int DisableCnt = 0; void functionA(void) { MyDisable(); --- some code MyEnable(); } void MyDisable() { disableInterrupts(); DisableCnt++; } void MyEnable() { DisableCnt--; If (DisableCnt == 0) enableInterrupts(); }

Slides created by: Professor Ian G. Harris Interrupt Latency How quickly does the system respond to an interrupt? 1.Maximum length of time when interrupts are disabled 2.Time required to execute higher priority interrupts 3.Time between interrupt event and running interrupt code 4.Time required to complete ISR code execution Contributing Factors:

Slides created by: Professor Ian G. Harris Reducing Interrupt Latency  Make interrupt code short Reduces ISR execution time and time for higher priority interrupts  Reduce time during which interrupts are disabled Minimize size of critical regions

Slides created by: Professor Ian G. Harris Interrupts in the ATmega Vector #AddressSourceDefnition 10$0012PCINT0Pin change interrupt 25$0030SPI, STCSPI serial transfer complete 30$003AADCADC conversion complete  Many possible interrupt sources  One interrupt vector for each source  Interrupt vector table shown in datasheet, Table 13-1  Source+”_vect” is the interrupt name recognized by your compiler  Check notes on this (avr-libc manual)

Slides created by: Professor Ian G. Harris Enabling/Disabling Interrupts  ATmega contains a status register called SREG  Bit 7 of SREG, the I bit, is the Global Interrupt Enable  Clearing I bit disables interrupts, setting I bit enables them  I bit is automatically cleared when an interrupt starts, set when interrupt is done Interrupts are not interrupted  Use the SEI() and CLI() macros to set and clear in C

Slides created by: Professor Ian G. Harris Defining Interrupts in C ISR(vector_name) - Defines an ISR for vector_name. Updates interrupt vector table automatically. Ex. ISR(ADC_vect) { printf(“Hello, world.”); } EMPTY_INTERRUPT(vector_name) - Defines an interrupt which does nothing. ISR_ALIAS(vector_name, target_vector_name) - Makes the ISR for vector_name the same as the ISR for target_vector_name. - Copies a pointer in the interrupt vector table

Slides created by: Professor Ian G. Harris ATmega Timers  ATmega 2560 has 6 timers, 2 8-bit and 4 16-bit  Detailed descriptions found in the datasheet  Timers can be used to generate interrupts  Can be used to generate pulse width modulated (PWM) signals PWM good for controlling motors (fake analog output) We won't look at these functions

Slides created by: Professor Ian G. Harris General Timer Control Need to control: 1. Start point of the timer (initial value) 2. “End” point of the timer (when the interrupt is generated) - May be just overflow event 3. Clock rate timer receives, to increase/decrease count speed - Typically uses a prescalar - Slows down clock by dividing down with another counter

Slides created by: Professor Ian G. Harris Output Compare Unit  TCNT0 – Value in the counter  OCR0A, OCR0B – Output compare registers  Interrupt can occur when TCNT0 == OCR0A

Slides created by: Professor Ian G. Harris Interrupt Flags When an interrupt occurs, a flag bit is set in a register TIFR0 – Timer/Counter Interrupt Flag Register - Contains the flags for Output Compare and Overflow TOV0 – Indicates that timer 0 overflow occurred OCF0A – Indicates that TCNT0 == OCR0A OCF0B – Indicates that TCNT0 == OCR0B All flags are cleared when the interrupt is executed You should not need to access this register directly

Slides created by: Professor Ian G. Harris Timer Interrupt Enable TIMSK0 – Timer/Counter Interrupt Mask Register - Select which timer interrupts are active TOIE0 – Timer overflow enable OCIE0A – Output compare “A” interrupt enable OCIE0B – Output compare “B” interrupt enable Need to enable the interrupt you want (in addition to GIE)

Slides created by: Professor Ian G. Harris Timer Counter Control Registers  TCCR0A and TCCR0B control different aspects of timer function Compare/Match Output Modes (COM0A1:0)  OC0A is an output pin of the Atmega 2560  Output comparison matching can drive the output pin  Typically used to generate regular waveforms (like PWM)  Can be used to synchronize system components  We will not use this feature

Slides created by: Professor Ian G. Harris Timer Counter Control Registers Waveform Generation Modes (WGM2:0)  Specify properties of PWM signals generated  Frequency, width, etc.  We will not use this feature

Slides created by: Professor Ian G. Harris Timer Counter Control Registers Force Output Compare (FOC0A, FOC0B)  Forces the output compare to evaluate true, even if it didn't occur  As if TCNT0 == OCR0A or TCNT0 == OCR0B  Used to alter waveform on OCOA or OCOB pins  We will not use this feature

Slides created by: Professor Ian G. Harris Timer Counter Control Registers Clock Select (CS0 2:0) Determine the speed of the clock received by the counter You will need this

Slides created by: Professor Ian G. Harris TCCR0A and TCCR0B Format TCCR0A Typical value: TCCR0A = 0b ; Do not drive compare/match outputs (OC0A, OC0B) Typical value: TCCR0B = 0b ; // no prescalar Last three bits are important, determine prescalar TCCR0B

Slides created by: Professor Ian G. Harris ATmega Timer Example #include #define TIMER0_CNT1000 main( ) { InitTimer0( ); // initialize hardware sei( ); // enable interrupts for ( ; ; ) { // background code goes here } ISR(TIMER0_COMPA_vect) { // Interrupt handler here } Using Timer 0 Match Interrupt

Slides created by: Professor Ian G. Harris Timer Initialization void InitTimer0( void ) { TCCR0A = 0b ; // No compare output, no waveform gen TCCR0B = 0b ; // No forcing, No prescalar OCR0A = TIMER0_CNT; // Load Compare value for timer 0 TIMSK0 = _BV(OCIE0A); // enable compare-match interrupt // _BV(n) == (1 << n) TCNT0 = 0; // Initialize timer to 0 }

Slides created by: Professor Ian G. Harris Setting a Specific Delay Need to compute how many cycles are needed to match required delay Need clock period T. T = 1/f Generate a constant square wave of ½ Hz 16-bit timer 50 kHz clock pre-scaler = up to divide-by-256 What delay is needed? - 1/½ Hz = 2000ms ms delay is needed (invert signal twice a period)

Slides created by: Professor Ian G. Harris Setting Prescalar How much prescalar is needed? - Can the counter count for 1000ms? - 16 bits, 65,536 is max value - System clock period is 1/50kHz = 20 microseconds - 65,536 * 20 microseconds = 1.31 seconds sec > 1000ms, so no prescalar is needed

Slides created by: Professor Ian G. Harris Setting Initial Timer Value Assume that we will use the Timer 0 Overflow interrupt Need counter to overflow after 1000ms 1000 ms / 20 microsec = 50,000 clocks Initialize timer to 65,536 – 50,000 = 15,536