ECE 2560 L17 – Interrupts Department of Electrical and Computer Engineering The Ohio State University ECE 2560 1 Interrupts What happens during interrupts How to set them up An important register – the flags register ECE 2560 2 Microcontroller interrupts An example – Zilog Z8051 An early micro controller On many early microcontrollers physical pins were the interrupt pins IRQ – Maskable interrupt pin FIRQ – Fast Interrupt On these chips there is a flag in the status register (SR) that enables interrupts or disables interrupt. ECE 2560 3 The MSP430 Interrupts The status register GIE- General Interrupt Enable Interrupts can be generated on most external pins – any digital I/O Interrupts also generated by internal devices like the timers. ECE 2560 4 Steps need to use interrupts For a port Configure the pin Set the GIE bit in SR to allow interrupts. Instruction The 430’s General Interrupt can be set using EINT Enable (general) Interrupts The 430’s GIE bit can be cleared using DINT Disable (general) Interrupts ECE 2560 5 Interrupts on I/O pins Setting up the port The I/O registers P1SEL,P1IN, P1OUT, P1DIR, P1IE,P1IES P1IFG – Port 1 Interrupt flag register each bit of the register is the flag for that pin a r/w register – bit is set when pin is an input and proper transition occurs – bit must be cleared using software instruction. A program can be written that checks this flag. ECE 2560 6 Polling the interrupt flag Port 1 and 2 are set up for interrupts Each have a PxIFG register Setting Up the port mov.b#0x00,P1SEL ;i/o mov.b#0xF7,P1DIR ;P1.3 input mov.b#0x08,P1IE ;P1.3 int set mov.b#0x08,P1IES ;falling ed bis.b #0x08,P1REN ;resistor mov.b#0x08,P1OUT ;pullup NOW can demo flag being set ECE 2560 7 Reading flag Can read the flags register, P1IFG Once set up, when the button is pressed, the flag is set to 1 Flag stays 1 until cleared. Demo of iflg.asm to see this in action ECE 2560 8 Using a port flag Can create a program to poll the switch flag and take action when it is pressed. The action Change the state of which led is on when pressed. Start out with P1.0 on. ECE 2560 9 Set up port – previous ;turn on a light bis.b #0x01,P1OUT ;set up loop to alternate which led is on mov.b P1OUT,R4 bw bit.b #0x08,P1IFG jz bw bit.b #0x01,P1OUT ;led 0 on? jz not0 ;led 6 is on – 0 on 6 off bic.b #0x01,P1OUT bis.b #0x40,P1OUT clr P1iFG jmp bw ;back to busy wait ;led 0 is on – 6 on 0 off not0 bis.b #0x01,P1OUT bic.b #0x01,P1OUT clr P1IFG jmp bw ECE 2560 10 Polling the Flag What is the downside of polling? Have to busy wait of periodically check flag. To service immediately loose ability to use the microcontroller for anything else If no continually checking do not have immediate service. ECE 2560 11 Interrupt vector A small area of memory that hold the address to load into the PC when an interrupt occurs – several locations – called the Interrupt Vector Table On the MSP 430 Reset,external hard reset NMI, osc fault Watchdog Timer+ Timer A1_A3 (CCIFG) I/O Port 2 I/O Port 1 ECE 2560 FFFEh FFFCh (int10) FFF4h (int13) (int03) FFE4h (int02) FFE2h 12 Interrupt service routine The top two elements on the stack are the saved SR and the return address. Return is the next instruction in the program that was interrupted. ISR does not want to disturb the state of the program that was interrupted so any register to be used should be saved on the stack (and restored) At end of ISR an RETI is executed. ECE 2560 13 Interrupt action ECE 2560 14 Demo program Set up port 1 mov.b mov.b mov.b bis.b bis.b bis.b bis.b clr.b bis.b #0x00,P1SEL ;I/O for all #0xF7,P1DIR ;P1.3 in #0x00,P1OUT ;all 0’s #0x08,P1IE ;sw enable int #0x08,P1ES ;falling edge #0x08,P1REN ;res enable #0x08,P1OUT ;pullup P1IFG ;clear flags #0x08,SR ;enable int ;or could use EINT instruction ECE 2560 15 To use interrupts? Can use interrupts but the IFG flag must be cleared in the port flag register. To actually use an interrupt service routine the bit must be cleared while program is waiting for interrupt. One of the actions of the service routine is to clear the flag prior to return. Note that this is at the port level. ECE 2560 16 What happens during? When an interrupt occurs The current instruction completes execution The PC value is pushed onto the stack The SR value is pushed onto the stack The PC is loaded with the contents of the word stored at what is call an interrupt vector. Execution continues in what is termed an interrupt service routine, ISR. ECE 2560 17 Summary - Assignment Try out the code – There is code on the webpage – intdemo.asm intdemo2.asm intdemo3.asm Try modifications to the code. No new assignment. Quiz when we come back on Dec 1st. ECE 2560 18
© Copyright 2024