1 / 20

PIC course

2. PIC programming Interrupts. InterruptsUsed to tell' CPU that something has happened and to deal with it, e.g.:Serial data received; ready to transmitAnalogue data conversionTimer tick'External event on port change. 3. PIC programming Interrupts. Polling software alternativeContinua

bond
Download Presentation

PIC course

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. 1 PIC course Program design - Assignment hand-out Microchip – PICs and MPLAB 3) Input and Output – Digital and serial I/O 4) Debugging – using MPLAB simulator 5) Lab session 6) Analogue input 7) Interrupt programming – external and timers 8) Simulator - stimulus and DCMI 9) Test 2 – Assignment hand-in

    2. 2 PIC programming – Interrupts Interrupts Used to ‘tell’ CPU that something has happened and to deal with it, e.g.: Serial data received; ready to transmit Analogue data conversion Timer ‘tick’ External event on port change

    3. 3 PIC programming – Interrupts Polling – software alternative Continually look for events in software Interrupts more efficient 10 PIC interrupt registers ! RCON, INTCON, INTCON2, INTCON3 PIR1, PIR2, PIE1, PIE2, IPR1, IPR2

    4. 4 PIC programming – Interrupts Each interrupt – controlled by 3 bits: • Flag bit indicates that interrupt has occurred • Enable bit • Priority bit to select high or low priority Can globally enable or disable interrupts Individual interrupts set in INTCON register

    5. 5 PIC programming – Interrupts Process: Interrupt occurs, Current code is suspended, PC and registers saved on stack Code in the interrupt routine executes. (called ISR = interrupt service routine) At end of ISR code, a “return from interrupt” instruction is executed, Original program continues from where it left off. (PC and registers restored from stack).

    6. 6 PIC programming – Interrupts Interrupt priority Two levels: low and high priority. Which to use is part of the program design C can deal with both types of interrupts. Low priority interrupt saves only the PC High priority saves PC, WREG, BSR and STATUS registers. Saving and restoring registers takes time – interrupt latency. The code is called an Interrupt Service Routine (ISR). Interrupt code can also be interrupted. - Low priority can be interrupted by a high priority.

    7. 7 PIC programming – Interrupts An ISR is like any other C function It can have local variables and access global variables; Declared with no parameters and no return value ISR, in response to a hardware interrupt, is invoked asynchronously. ISR’s should not be called from other C functions. Global variables that are accessed by both an ISR and mainline functions should be declared volatile. A volatile variable has a value that can be changed by something other than user code A typical example is an input port or a timer register.

    8. 8 PIC programming – Interrupts C interrupt code: Processor jumps to the high or low vector address 0x08 or 0x18. Place a GOTO instruction at these addresses This transfers control to the ISR.

    9. 9 PIC programming – Interrupts void low_isr (void); // for the goto below // Low int vector at 0x18 - Add code to jump to low ISR. #pragma code low_vector = 0x18 // set code at 0x18 void interrupt_at_low_vector(void) { _asm GOTO low_isr _endasm }

    10. 10 PIC programming – Interrupts #pragma code // return to the default code section #pragma interruptlow low_isr // low priority ISR void low_isr (void) { /* Low ISR code here ... */ } // similar code needed for high ISR

    11. 11 PIC programming – Interrupts When an interrupt is responded to, Global Interrupt Enable (GIE) bit is cleared This disables further interrupts. In the ISR the interrupt source is determined by looking at the interrupt flag bits. The return from interrupt instruction sets the GIE bit and re-enables interrupts, So the interrupt flag bits must be cleared in software to avoid recursive interrupts.

    12. 12 PIC programming – Interrupts Exercise: Toggle an output when an external input (on PORTB) changes See the interrupt working by using a stimulus

    13. 13 PIC programming – Interrupts void HighISR (void); // needed for 'goto' #pragma code HIGH_INTERRUPT_VECTOR = 0x8 // set address 0x08 void HighVector (void) { _asm // assembly code goto HighISR // vector to High ISR _endasm // end asm code }

    14. 14 PIC programming – Interrupts #pragma code // return to the code section #pragma interrupt HighISR // tells compiler to add correct return instruction void HighISR(void) // this is the High ISR { INTCONbits.INT0IF = 0; // clear external int flag // output something printf(“High interrupt routine, port B = %x\n", PORTB); }

    15. 15 PIC programming – Interrupts Initialisation code: void EnableHighInterrupts (void) // Initialisation - Enable interrupt code { RCONbits.IPEN = 1; // enable int. (RCON bit 7) INTCONbits.GIEH = 1; // enable global interrupt }

    16. 16 PIC programming – Interrupts Main code: void main(void) // main code { EnableHighInterrupts(); // enable interrupts // enable specific interrupts – defined in PORTB.H // enable RB0/INT0 int, configure RB0 for input, // interrupt on button down OpenRB0INT ( PORTB_CHANGE_INT_ON & PORTB_PULLUPS_ON & FALLING_EDGE_INT); while (1); // and wait for button to be pressed }

    17. 17 PIC programming – Interrupts Test it - Stimulus

More Related