PLT 206 MICROCONTROLLER SYSTEMS Lec 8 Interrupt Programming

  • Slides: 49
Download presentation
PLT 206 MICROCONTROLLER SYSTEMS Lec 8: Interrupt Programming

PLT 206 MICROCONTROLLER SYSTEMS Lec 8: Interrupt Programming

OBJECTIVES Contra and compare interrupts versus pooling Explain the purpose of the ISR List

OBJECTIVES Contra and compare interrupts versus pooling Explain the purpose of the ISR List all the major interrupts of the PIC 16 Enable and disable PIC 16 interrupts Program the PIC 16 interrupt (timer interrupt and external interrupt) using C language

INTERRUPT Interrupts can be generated by various internal or external hardware events. Interrupts are

INTERRUPT Interrupts can be generated by various internal or external hardware events. Interrupts are internally or externally generated asynchronous hardware signals that force the processor to stop its current program and carry out the function called. The function called by the interrupt is often referred to as an interrupt service routine (ISR). The processor’s current register contents and status must be saved and the current program address stored on the stack so that the background task can be resumed when the ISR has finished.

INTERRUPT – PRIORITY If the program used multiple interrupts, one ISR may be interrupted

INTERRUPT – PRIORITY If the program used multiple interrupts, one ISR may be interrupted by another. The interrupts may need to be assigned an order of priority, so that a less important task does not interrupt a more important one. When the higher-priority ISR is being executed, the lower-priority interrupt can be disabled or masked, until it is finished. In more complex programs, numerical levels of priority can be assigned, with higher priorities taking precedence. Unfortunately, the 16 series PIC is not well suited to this, as it does not have a built-in priority system, unlike more powerful processors. Further, the different interrupt sources have to be identified explicitly by a user routine.

Interrupt A single microcontroller can serve several devices. There are two (2) methods by

Interrupt A single microcontroller can serve several devices. There are two (2) methods by which devices receive service from the microcontroller: Polling Interrupts

Interrupt vs. Polling POLLING METHOD In this method, microcontroller accesses at the exact time

Interrupt vs. Polling POLLING METHOD In this method, microcontroller accesses at the exact time interval the external device, and gets the required information. The microcontroller continuously monitors the status of a given device; when the status condition met, it performs the service. After that, it moves on to monitor the next device until each one is service. The time periods is determined by user. When using the Polling method, the processor must access the device itself and request the desired information that is needed to be processed. Cannot assign priority because it checks all devices in a round-robin fashion. Cannot ignore a devices for service The main drawback of Polling method when writing program is waste of time of microcontroller. The microprocessor needs to wait and check whether new information arrived.

Interrupt vs. Polling INTERRUPT METHOD q q q Whenever any device needs the microcontroller’s

Interrupt vs. Polling INTERRUPT METHOD q q q Whenever any device needs the microcontroller’s service, the device notifies it by sending an interrupt signal. Interrupt is the signal sent to the microprocessor to mark the event that requires immediate attention. Upon receiving an interrupt signal, the microcontroller stops current program and serve the device (execute ISR). The program associated with the interrupt is called ISR (interrupt service routine) or interrupt handler. Each device can get the attention of the microcontroller based on the priority assign to it. Can ignore a device request for service

Interrupt Service Routine (ISR) For every interrupt, there must be an interrupt service routine

Interrupt Service Routine (ISR) For every interrupt, there must be an interrupt service routine (ISR) or interrupt handler. When an interrupt is invoked, the µC runs the ISR. Generally, in most microprocessors, for every interrupt there is a fixed location in memory that holds the address of its ISR. The group of memory locations set aside to hold the addresses of ISRs is called the interrupt vector table (IVT). Interrupt ROM Location (Hex) Power-on-Reset 0000 h Interrupt Vector 0004 h (Default upon power-on reset) Interrupt Vector Table for PIC 16 F 877 A Fixed Location in Memory

Steps in Executing Interrupt 1. 2. 3. 4. Upon receives interrupt signal, µC finishes

Steps in Executing Interrupt 1. 2. 3. 4. Upon receives interrupt signal, µC finishes the instruction it is executing and saves the address of the next instruction (program counter) on the stack It jumps to a fixed location in memory (interrupt vector table (IVT)). The IVT directs the microcontroller to the address of the ISR. The microcontroller gets the address of the ISR from the IVT and jumps to it. It start to execute the interrupt service subroutine until it reaches the last instruction of the subroutine RETFIE (Return from Interrupt Exit) Upon executing the RETFIE instruction, the microcontroller returns to the place where it was

Types of Interrupts There are two types of interrupts: Software interrupts: come from a

Types of Interrupts There are two types of interrupts: Software interrupts: come from a program that runs by the processor and “request” the processor to stop running the program, go to make an interrupt and then to return to continue to execute the program. Example: Procedure - when there is a procedure call, the processor stops the execution of the program, jumps to the place in memory that reserved for a procedure – executes the procedure and only then returns back to the program and continues to execute.

Types of Interrupts q Hardware interrupts: q q q Hardware interrupts are sent to

Types of Interrupts q Hardware interrupts: q q q Hardware interrupts are sent to microcontroller by external hardware devices. Some of the interrupts can be “masked” by Interrupt Enable bit (IE). When the interrupt is masked, the microcontroller does not “see" the request for an interrupt, therefore won’t be available to execute it. The masked interrupt will not be executed until it will be unmasked – “mask” is removed. There are some interrupts that can not be masked - NMI Non Maskable Interrupts. These are used to report on critical hardware issues, such as the drop of voltage. We want an immediate response from the microcontroller to these kind of interrupts, without the ability to ignore them.

Sources of Interrupt in PIC 16 F 87 XA In this chapter, only these

Sources of Interrupt in PIC 16 F 87 XA In this chapter, only these will be explained Example of sources of interrupts: Each Timers (Timer 0, 1, 2) 1 interrupt for external hardware: Pin RB 0 (INT) 2 interrupts for serial communication USART (Receive and Transmit) : Pin RC 6 (TX) & RC 7 (RX) The PORTB-Change interrupt: Pin RB 7 - RB 4 The ADC (Analog-to-Digital Converter) The CCP (Compare Capture PWM)

PIC 16 F 877 Interrupts High Priority Low Priority

PIC 16 F 877 Interrupts High Priority Low Priority

Enabling and Disabling Interrupt The PIC 16 F 87 x. A has 15 interrupt

Enabling and Disabling Interrupt The PIC 16 F 87 x. A has 15 interrupt sources. INTCON register records individual interrupt requests in flag bits. Also has individual and global interrupt enable bits. In general each interrupt source have following related bits. Enable Bit The are suffixed with xx. IE (Interrupt Enable). It can be used to enable/disable the related interrupt. When set to '1' it enables the interrupt. Flag Bit The are suffixed with xx. IF (Interrupt Flag). It is set automatically by the related hardware when the interrupt condition occurs. When it is set to '1' we know that interrupt has occurred.

Enabling and Disabling Interrupt Upon reset, all interrupts are disabled (masked). None of interrupt

Enabling and Disabling Interrupt Upon reset, all interrupts are disabled (masked). None of interrupt will be responded to µC if they are activated. Interrupts must be enabled (unmasked) by software in order for µC to respond The D 7 bit of INTCON (Interrupt Control) register is responsible for enabling and disabling interrupts globally - GIE (Global Interrupt Enable GIE = INTCON<7>

The required registers to work with interrupts The settings of the interrupts are done

The required registers to work with interrupts The settings of the interrupts are done by using 3 interrupt control registers: INTCON Register – contains peripheral interrupt enable bit PIE (PIE 1, PIE 2) Register – contains corresponding peripheral interrupt enable bits PIR (PIR 1, PIR 2) Register – contains peripheral interrupt flag bits Note: Please refer Microchip PIC 16 F 87 XA Data Sheet (PDF file) to look at the interrupt control registers in more details.

INTCON Register A readable and writable register which contains various enable and flag bits

INTCON Register A readable and writable register which contains various enable and flag bits for TMR 0 register overflow, RB port change and external RB 0/INT pin interrupts. Global Interrupt Enable: GIE (INTCON<bit 7>) GIE = 1 : enabling all interrupts (unmask interrupt) GIE = 0 : disabling all interrupts globally (mask interrupt) Interrupts are allow to happen. Each interrupt source is enabled by setting to HIGH the corresponding interrupt enable (IE) bit. No interrupt is acknowledged, even if the corresponding interrupt enable (IE) bit is HIGH. Individual interrupts can be disabled through their corresponding enable bits in various registers. Individual interrupt flag bits are set regardless of status of their corresponding mask bit or GIE bit.

INTCON Register GIE bit is cleared on Reset. Upon activation of interrupt, GIE bit

INTCON Register GIE bit is cleared on Reset. Upon activation of interrupt, GIE bit is cleared (GIE = 0) to make sure another interrupt cannot interrupt the u. C while it is servicing the current one. At the end of ISR, GIE is set HIGH (GIE = 1) to allow another interrupt to come in. PEIE (INTCON<bit 6>) This bit, along with GIE, must be set HIGH (PEIE = 1) to enable any peripheral interrupt such as Timers and serial port. TMR 0 IE (INTCON<bit 5>) & INTE (INTCON<bit 4>) These bits, along with GIE, must be set HIGH for an interrupt to be responded to

INTCON register

INTCON register

PIE Register PIE 1: Contains the individual enable bits of the peripheral interrupts. PIE

PIE Register PIE 1: Contains the individual enable bits of the peripheral interrupts. PIE 2: Contains the individual enable bits for the CCP 2 peripheral interrupt, SSP bus collision interrupt, EEPROM write operation interrupt and comparator interrupt. Note: Along with GIE, bit PEIE (INTCON<bit 6>) must be SET (PEIE = 1) to enable any peripheral interrupt

PIE 1 register

PIE 1 register

PIE 2 register

PIE 2 register

PIR Register PIR 1: Contains the individual flag bits of the peripheral interrupts. PIR

PIR Register PIR 1: Contains the individual flag bits of the peripheral interrupts. PIR 2: Contains the individual flag bits for the CCP 2 peripheral interrupt, SSP bus collision interrupt, EEPROM write operation interrupt and comparator interrupt. Note: Interrupt flag bits are SET when an interrupt condition occurs, regardless of state of its corresponding enable bit or GIE (INTCON<7>). User software should ensure the appropriate interrupt flag bits are CLEAR prior to enabling an interrupt.

PIR 1 register

PIR 1 register

PIR 2 register

PIR 2 register

Programming Timer Interrupt Polling Method Used in “Timer Programming” chapter, Timer 0 and 1

Programming Timer Interrupt Polling Method Used in “Timer Programming” chapter, Timer 0 and 1 using polling method are discussed. In polling, have to wait until the timer flag, TMR 0 IF (INTCON<2>) or TMR 1 IF (PIR 1<0>) is raised when timer rolls over. Drawback: µC is tied down waiting for timer flag to be raised, and cannot do anything else. Interrupt Method Used in this chapter, utilize interrupts to program PIC 16 timers. If the timer interrupt in the interrupt register is enabled, TMR 0 IF or TMR 1 IF is raised whenever the timer rolls over, and µC jumps to the interrupt vector table to service ISR. Advantage: µC can do other things until it is notified that the timer has rolled over.

Programming Timer Interrupt Figure shows a program sequence where a timer is run to

Programming Timer Interrupt Figure shows a program sequence where a timer is run to generate an output pulse interval. An interrupt routine (ISR) has been written and assigned to the timer interrupt. The timer is set up during program initialization and started by preloading or clearing it. The main program and timer count then proceed concurrently, until a time-out occurs and the interrupt is generated. The main program is suspended and the ISR executed. When finished, the main program is resumed at the original point. If the ISR contains a statement to toggle an output bit, a square wave could be obtained with a period of twice the timer

Programming Timer Interrupt To use an interrupt, we must enable the interrupt because all

Programming Timer Interrupt To use an interrupt, we must enable the interrupt because all interrupts are masked upon power-on reset. TMRx. IE bit: enables interrupt for Timer x Interrupt Flag Bit Register Enable Bit Register Timer 0 TMR 0 IF INTCON TMR 0 IE INTCON Timer 1 TMR 1 IF PIR 1 TMR 1 IE PIE 1 Timer 2 TMR 2 IF PIR 1 TMR 2 IE PIE 1 Timer Interrupt Flag Bits and Associated Registers INTCON Register with Timer 0 Interrupt Enable and Interrupt Flag

Timer Interrupt See Chapter “Timer Programming” , Lab Module 6 “Timer” and PIC 16

Timer Interrupt See Chapter “Timer Programming” , Lab Module 6 “Timer” and PIC 16 F 87 XA Data Sheet for more details. Timer 0 Interrupt An overflow (FFH → 00 H) in the TMR 0 register will set flag bit, TMR 0 IF (INTCON<2>). The interrupt can be enabled/disabled by setting/clearing enable bit, TMR 0 IE (INTCON<5>). Timer 1 Interrupt An overflow (FFFFH → 0000 H) in the TMR 1 register (TMR 1 H: TMR 1 L) will set flag bit, TMR 1 IF (PIR 1<0>). The interrupt can be enabled/disabled by setting/clearing enable bit, TMR 1 IE (PIE 1<0>).

Steps in Programming Timer Interrupt 1. 2. 3. 4. 5. 6. 7. Initialize PIC.

Steps in Programming Timer Interrupt 1. 2. 3. 4. 5. 6. 7. Initialize PIC. Off the Timer 0 or Timer 1 by loading the value into the OPTION_REG or T 1 CON register. Load reg. TMR 0 or TMR 1 with initial value. Clear the TMR 0 IF flag or TMR 1 IF flag. INTCONbits. TMR 0 IF=0; // Timer 0 PIR 1 bits. TMR 1 IF=0; // Timer 1 Enable the Timer Interrupt TMR 0 IE or TMR 1 IE INTCONbits. TMR 0 IE=1; // Timer 0 PIE 1 bits. TMR 1 IE=1; // Timer 1 Enable peripheral interrupts INTCONbits. PEIE=1; Enable all interrupts INTCONbits. GIE=1;

Steps in Programming Timer Interrupt Start the Timer 0 or Timer 1. Write Main

Steps in Programming Timer Interrupt Start the Timer 0 or Timer 1. Write Main Function. Do main function while waiting for interrupt. When interrupt raised (TMR 0 IF=1 or TMR 1 IF =1), jump to ISR: void interrupt ISR_label (void) In ISR: “interrupt” is a reserved keyword 8. 9. 10. 11. 1. 2. 3. 4. 5. 6. Stop the Timer 0 or Timer 1. Write ISR function. Reload TMR 0 or TMR 1 register. Clear the TMR 0 IF flag or TMR 1 IF flag. Re-enable interrupts. Start Timer 0 or Timer 1 (if required in ISR).

Example 1 Write a program to generate a square wave with a period of

Example 1 Write a program to generate a square wave with a period of 2 ms on pin RB 5. Use Timer 0 with 1: 256 prescaler. Compare with Lab Module 6 “Timer” Design Question 1 #include <htc. h> __CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF); #define _XTAL_FREQ 20000000 #define LED 5 RB 5 // LED for Timer 1 void interrupt chk_isr(void) ; // declare interrupt check ISR function void Timer 0_ISR(void); // declare Timer 0 ISR function void main(void) { TRISB=0 b 0000; // RB 5 as output PORTB=0 b 0000; // LED RB 5 off OPTION_REG=0 b 00100111; //Off Timer 0, Timer 0 module, 1: 256 TMR 0=0 x. EC; // load Timer 0 register with initial value INTCONbits. TMR 0 IF=0; // clear Timer 0 interrupt flag bit TMR 0 IF (INTCON<2>); INTCON=0 b 0000; INTCONbits. TMR 0 IE=1; // enable Timer 0 interrupt; INTCON=0 b 11100000 INTCONbits. PEIE=1; INTCONbits. GIE=1;

while(1) // keep looping until interrupt comes, jumps to interrupt function { OPTION_REG=0 b

while(1) // keep looping until interrupt comes, jumps to interrupt function { OPTION_REG=0 b 00000111; // turn on Timer 0 T 0 CS (OPTION_REG<5>) } “interrupt” is a reserved keyword } void interrupt chk_isr(void) // interrupt function { if(INTCONbits. TMR 0 IF==1) // Timer 0 causes interrupt? Timer 0_ISR(); // Yes. Execute Timer 0 ISR function { } } void Timer 0_ISR(void) // Timer 0 ISR function { OPTION_REG=0 b 00100111; // OFF Timer 0 LED 5 = !LED 5; // toggle RB 5 TMR 0=0 x. EC; // re-load Timer 0 register INTCONbits. TMR 0 IF=0; // clear Timer 0 interrupt flag bit TMR 0 IF (INTCON<2>) INTCONbits. TMR 0 IE=1; // enable Timer 0 interrupt; INTCON=0 b 11100000 INTCONbits. PEIE=1; INTCONbits. GIE=1; }

Example 2 Write a program to generate a square wave with a period of

Example 2 Write a program to generate a square wave with a period of 2 ms on pin RB 5. Use Timer 1 without prescaler. Compare with Lab Module 6 “Timer” Design Question 2 #include <htc. h> __CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF); #define _XTAL_FREQ 20000000 #define LED 7 RB 7 // LED for Timer 1 void interrupt chk_isr(void) ; void Timer 1_ISR(void); void main(void) { TRISB=0 b 0000; // RB 7 as output PORTB=0 b 0000; // LED RB 7 off T 1 CON=0 b 0000; / / off Timer 1 TMR 1 H=0 x. EC; // load initial value in TMR 1 register TMR 1 L=0 x 78; PIR 1 bits. TMR 1 IF=0; // clear Timer 1 interrupt flag bit TMR 1 IF (PIR 1<0>); PIR 1=0 b 0000; PIE 1 bits. TMR 1 IE=1; // enable Timer 0 interrupt: TMR 1 IE(PIE 1<0>); PIE 1=0 b 00000001; INTCONbits. PEIE=1; // PEIE(INTCON<6>), GIE(INTCON<7>); INTCON=0 b 11000000 INTCONbits. GIE=1;

while(1) // keep looping until interrupt comes, jumps to interrupt function { T 1

while(1) // keep looping until interrupt comes, jumps to interrupt function { T 1 CON=0 b 00000001; // turn on Timer 1 } } void interrupt chk_isr(void) // interrupt function { if(PIR 1 bits. TMR 1 IF==1) // Timer 1 causes interrupt? Timer 1_ISR(); // Yes. Execute Timer 1 ISR function { } } void Timer 1_ISR(void) // Timer 1 ISR function { T 1 CON=0 b 0000; // off Timer 1 LED 7 = !LED 7; // toggle RB 7 TMR 1 H=0 x. EC; // re-load TMR 1 register TMR 1 L=0 x 78; PIR 1 bits. TMR 1 IF=0; // clear Timer 1 interrupt flag bit TMR 1 IF (PIR 1<0>); PIR 1=0 b 0000; PIE 1 bits. TMR 1 IE=1; // enable Timer 0 interrupt: TMR 1 IE(PIE 1<0>); PIE 1=0 b 00000001; INTCONbits. PEIE=1; // PEIE(INTCON<6>), GIE(INTCON<7>); INTCON=0 b 11000000 INTCONbits. GIE=1; }

Write a program to generate a square wave: (a) with a period of 2

Write a program to generate a square wave: (a) with a period of 2 ms on pin RB 5. Use Timer 0 with 1: 256 prescaler. (b) With a period of 1 ms on pin RB 7. Use Timer 1 without prescaler Example 3 Compare with Lab Module 6 “Timer” Laboratory Exercise #include <htc. h> __CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF); #define _XTAL_FREQ 20000000 #define LED 5 RB 5 // for Timer 0 #define LED 7 RB 7 // for Timer 1 void pic_init(void); // declare function void timer_init(void); void interrupt chk_isr(void); void Timer 0_ISR(void); void Timer 1_ISR(void); void main(void) // main function { pic_init(); timer_init(); intr_init(); } OPTION_REG=0 b 00000111; // turn on Timer 0 T 1 CON=0 b 00000001; // turn on Timer 1 while(1); // keep looping until interrupt comes, jumps to interrupt function

void pic_init(void) // pic initialization { TRISB=0 b 0000; // RB 5 & RB

void pic_init(void) // pic initialization { TRISB=0 b 0000; // RB 5 & RB 7 as output PORTB=0 b 0000; // LED RB 5 & RB 7 off } void timer_init(void) // timer initialization { OPTION_REG=0 b 00100111; // off Timer 0 TMR 0=0 x. EC; // load Timer 0 register with initial value T 1 CON=0 b 0000; // off Timer 1 TMR 1 H=0 x. F 6; // load register TMR 1 with initial value TMR 1 L=0 x 3 C; } void intr_init(void) // interrupt initialization { INTCONbits. TMR 0 IF=0; // clear Timer 0 interrupt flag bit TMR 0 IF (INTCON<2>); INTCONbits. TMR 0 IE=1; // enable Timer 0 interrupt: TMR 0 IE(INTCON<5>) PIR 1 bits. TMR 1 IF=0; // clear Timer 1 interrupt flag bit TMR 1 IF (PIR 1<0>); PIR 1=0 b 0000; PIE 1 bits. TMR 1 IE=1; // enable Timer 1 interrupt: TMR 1 IE(PIE 1<0>); PIE 1=0 b 00000001; INTCONbits. PEIE=1; // PEIE(INTCON<6>), GIE(INTCON<7>); INTCON=0 b 11000000 INTCONbits. GIE=1; }

void interrupt chk_isr(void) // interrupt function. Check if Timer 0 or Timer 1 is

void interrupt chk_isr(void) // interrupt function. Check if Timer 0 or Timer 1 is overflow { if(INTCONbits. TMR 0 IF==1) // Timer 0 causes interrupt? { Timer 0_ISR(); // Yes. Execute Timer 0 ISR function } if(PIR 1 bits. TMR 1 IF==1) // Timer 1 causes interrupt? { Timer 1_ISR(); // Yes. Execute Timer 1 ISR function } } void Timer 0_ISR(void) { OPTION_REG=0 b 00100111; // OFF Timer 0 LED 5 = !LED 5; // toggle RB 5 TMR 0=0 x. EC; //reload TMR 0 value intr_init(); // re-enabling (initialize) interrupts OPTION_REG=0 b 00000111; } // turn on Timer 0

void Timer 1_ISR(void) { T 1 CON=0 b 0000; // off Timer 1 LED

void Timer 1_ISR(void) { T 1 CON=0 b 0000; // off Timer 1 LED 7 = !LED 7; // toggle RB 7 TMR 1 H=0 x. F 6; // re-load TMR 1 value ` TMR 1 L=0 x 3 C; intr_init(); T 1 CON=0 b 00000001; } // re-enabling (initialize) interrupts // turn on Timer 1

Programming External Hardware Interrupt PIC 16 F 87 XA has 1 interrupt for external

Programming External Hardware Interrupt PIC 16 F 87 XA has 1 interrupt for external hardware: INT (Pin RB 0) Upon activation of this pin, PIC 16 gets interrupted in whatever it is doing and jumps to the vector table to perform ISR. On default, the hardware interrupts are directed to vector table location 0004 H, unless specified otherwise. INT is a positive-edge-triggered interrupt (default for power-on reset): L H signal is applied to pin RB 0, the INTF bit is raised, causing u. C to be interrupted. The raising of INTF forces u. C to jump to location 0004 H in the vector table to service ISR. Interrupt Flag Bit Register Enable Bit Register INT (RB 0) INTF INTCON INTE INTCON Hardware Interrupt Flag Bits and Associated Registers

External Interrupt (INT) External interrupt on the RB 0/INT pin is edge triggered, either

External Interrupt (INT) External interrupt on the RB 0/INT pin is edge triggered, either rising if bit INTEDG (OPTION_REG<6>) is set or falling if the INTEDG bit is clear. When a valid edge appears on the RB 0/INT pin, flag bit INTF (INTCON<1>), is set. This interrupt can be disabled by clearing enable bit, INTE (INTCON<4>). Flag bit INTF (INTCON<1>) must be cleared in software in the ISR before re-enabling this interrupt. The INT interrupt can wake-up the processor from Sleep if bit INTE was set prior to going into Sleep. The status of global interrupt enable bit, GIE, decides whether or not the processor branches to the interrupt vector following wake-up. See Section 14. 14 “Power-down Mode (Sleep)” for details on Sleep mode in PIC 16 F 87 XA Data Sheet.

Rising edge = positive edge (L H) Falling edge = negative edge (H L)

Rising edge = positive edge (L H) Falling edge = negative edge (H L) OPTION_REG register

Steps in Programming External Hardware Interrupt 1. Initialize PIC – TRISBbits. TRISB 0=1; (RB

Steps in Programming External Hardware Interrupt 1. Initialize PIC – TRISBbits. TRISB 0=1; (RB 0 as input for interrupt) 2. 3. 4. 5. 6. 7. 8. Clear the external interrupt flag INTCONbits. INTF=0; Enable Timer interrupt INTCONbits. INTE=1; Enable all interrupts INTCONbits. GIE=1; Write Main Function. Check for external interrupt come from RB 0/INT pin. When interrupt raised (INTCONbits. INTF=1 ), jump to ISR. In ISR: 1. Clear the external interrupt flag

Example 4 Assume pin RB 0/INT is connected to a pulse generator (external hardware).

Example 4 Assume pin RB 0/INT is connected to a pulse generator (external hardware). (a) (b) The LED and buzzer will turn on and off at the same rate as the pulse is applied to the RBO/INT pin The LED and buzzer will turn on and off frequently when RB 0/INT=1 The program will play a sound from a buzzer and a LED will blinking, when every time there is an external interrupt that is generated through RB 0 pin. In this program we will not connect any external device to the PORTB 0. Instead, we will “create an external interrupt” using our program itself, by incrementing the value of PORTB by 1 (PORTB++). When we are increasing the value by 1, the last bit (LSB) will vary each cycle of the program from "0" to "1" and vice versa.

Example 4 (a) The LED and buzzer will turn on and off at the

Example 4 (a) The LED and buzzer will turn on and off at the same rate as the pulse is applied to the RBO/INT pin #include <htc. h> __CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF); #define _XTAL_FREQ 20000000 #define LED 7 RD 7 // define LED #define BZ // define buzzer RC 2 void interrupt beep(void); //declare interrupt beep function void main(void) //main function { TRISB=0 b 0000; // interrupt from RB 0/INT pin PORTB=0 x 00; “create an external interrupt” using our program itself: TRISB=0 x 00 If connect to any external device: TRISB=0 x 01 (RB 0 as input) TRISC=0 x 00; // Port C as output PORTC=0 x 00; // set value of Port C to 0 TRISD=0 x 00; // Port D as output PORTD=0 x 00; // set value of Port D to 0 INTCONbits. INTF=0; // clear external interrupt flag INTCONbits. INTE=1; // enable external interrupt from pin RB 0/INT INTCONbits. GIE=1; // global interrupt enable; enable all interrupts

Example 4 (a) - continued while(1) { Logic change in the pin RB 0,

Example 4 (a) - continued while(1) { Logic change in the pin RB 0, "0" "1", the external interrupt flag will be set (INTF == 1). PORTB=PORTB++; // increment the Port B value by 1 __delay_us(100); // a simple delay if(RB 0==1) // check for external interrupt INTR/RB 0 is coming or not { INTCONbits. INTF=1; // Yes. jump to interrupt beep ISR } } } // interrupt beep ISR void interrupt beep(void) // run if external interrupt flag INTF==1 { INTCONbits. INTF=0; } // reset the external interrupt flag LED 7=!LED 7; // toggle LED 5 BZ = !BZ; // on off Buzzer

Example 4 (b) The LED and buzzer will turn on and off frequently when

Example 4 (b) The LED and buzzer will turn on and off frequently when RB 0/INT=1 #include <htc. h> __CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF); #define _XTAL_FREQ 20000000 #define LED 5 RB 5 // define LED #define BZ // define buzzer RC 2 int i=0; void interrupt beep(void); //declare interrupt beep function void main(void) { TRISB=0 b 0000; // RB 5 as output, RB 0/INT check for interrupt PORTB=0 b 0000; // LED RB 5 off TRISC=0 x 00; // Port C as output PORTC=0 x 00; // set value of Port. C to 0 INTCONbits. INTF=0; // clear external interrupt flag INTCONbits. INTE=1; // enable external interrupt from RB 0 INTCONbits. GIE=1; // global interrupt enable; enable all interrupts

Example 4 (b) - continued while(1) { PORTB=PORTB++; // increment the Port B value

Example 4 (b) - continued while(1) { PORTB=PORTB++; // increment the Port B value by 1 __delay_us(100); // a simple delay if(RB 0==1) // check for external interrupt INTR/RB 0 is coming or not { INTCONbits. INTF=1; // Yes. jump to interrupt beep ISR } } } void interrupt beep(void) // run if external interrupt flag==1 { INTCONbits. INTF=0; // reset the external interrupt flag for(i=0; i<10; i++) // a loop for creating LED and Buzzer on off { } } LED 5=!LED 5; // toggle LED 5 BZ = !BZ; // on off Buzzer

END OF CHAPTER

END OF CHAPTER