Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to Embedded Systems, and the PIC Microcontroller Lecture 8: Interrupts The aims of this session are to explore the structure and application.

Similar presentations


Presentation on theme: "An Introduction to Embedded Systems, and the PIC Microcontroller Lecture 8: Interrupts The aims of this session are to explore the structure and application."— Presentation transcript:

1 An Introduction to Embedded Systems, and the PIC Microcontroller Lecture 8: Interrupts The aims of this session are to explore the structure and application of interrupts, i.e.: to review interrupt action; to introduce the generic and 16F84A interrupt structure; to write simple programs using single interrupts. The copyright to all diagrams is held by Microchip Technology, or T. Wilmshurst, unless otherwise stated For further information and background, read pages 120-126. August 2008

2 An Interrupt Review An Interrupt is an external input to the CPU. The interrupt facility allows the processor to respond rapidly to external changes. When an Interrupt is detected by the CPU, it: - completes the current instruction, - stores the address of the next instruction, and possibly other key variables (e.g. contents of Accumulator and Condition Code Register), onto the stack, - jumps to an interrupt service routine (ISR), whose address is determined by an "interrupt vector". Many interrupts can be masked, ie disabled, by setting a bit in a control register. In some processors however (but not PIC), some interrupts are not maskable. If an Interrupt is masked, then there is a possibility that it will not be detected. Therefore there are also Interrupt Flags, bits in SFRs, which are set whenever an associated interrupt occurs. These record the fact that an interrupt has occurred, even if the CPU is unable to respond to it. An Interrupt that has occurred, but has not received CPU response, is called a Pending Interrupt. In the case of several interrupts, one ISR is completed before the next interrupt is responded to.

3 A Generic Interrupt Structure

4 The Reset Vector Interrupt Routine always starts here Microcontroller Interaction with Peripherals, via SFR and Interrupt Interfacing with Peripherals Recalling Interrupt-Related Points that have already Come up

5 The PIC 16F84A Interrupt Structure Global Interrupt Enable External Interrupt EEPROM Write Complete Port B Change Timer Overflow Interrupt Flag External Interrupt input Note that the interrupt flags are set by the interrupt action, but must be cleared in the program, during the ISR. What happens if this isn’t done?

6 The PIC 16F84A INTCON Register

7 The PIC 16 Series Interrupt Response Note that this diagram shows what the PIC microcontroller itself does as an interrupt occurs. The programmer need not worry about any of these actions, but needs to know that they’re happening.

8 Programming with Single Interrupts Look at Program Example 6.1 for a simple program with one interrupt. Complete Simulation Exercise 6.1. It is comparatively easy to write simple programs with just one interrupt. For success, the essential points to watch are: Start the ISR at the Interrupt Vector, location 0004; Enable the interrupt that is to be used, by setting enable bits in the INTCON and/or PIE registers; Set the Global Enable bit, GIE; Once in the ISR, clear the interrupt flag; End the ISR with a retfie instruction; Ensure that the interrupt source, for example Port B or Timer 0, is actually set up to generate interrupts!

9 The INTCON Register of a PIC 16F84A is set as shown in a) below. a) Determine which interrupts are enabled. b) An interrupt occurs, and the INTCON register is found to have changed to b). Which interrupt source has called? c) Which bit must the user change before the end of the ISR? Interrupt Example 1 a) b)

10 org 0000 goto start org 0014 goto my_interrupt ;this is the interrupt routine my_interrupt movlw 0f addwf counter1,1 btfsc flags,2;test motor run flag clrf overflow return ;Program starts here start bsf status,5 … Interrupt Example 2 An inexperienced programmer writes the code opposite for a 16F84A to respond to an external interrupt. He correctly enables the interrupt, and no other interrupts are enabled. a) How should the INTCON register be set? b) What are the errors in the program excerpt? Note: Programming with a single interrupts is comparatively simple. Programming with multiple interrupts is NOT, and becomes quite an art form. We return to this topic in a later lecture. End of Lecture Note


Download ppt "An Introduction to Embedded Systems, and the PIC Microcontroller Lecture 8: Interrupts The aims of this session are to explore the structure and application."

Similar presentations


Ads by Google