1 / 49

Eet203 microcontroller systems design

Eet203 microcontroller systems design. Lec 9: Interrupt Programming. OBJECTIVES. Contra and compare interrupts versus pooling Explain the purpose of the ISR List all the major interrupts of the PIC16 Enable and disable PIC16 interrupts

bernhard
Download Presentation

Eet203 microcontroller systems design

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. Eet203 microcontroller systems design Lec 9: Interrupt Programming

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

  3. 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.

  4. 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.

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

  6. 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.

  7. Interrupt vs. Polling INTERRUPT METHOD • 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

  8. 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 Vector Table for PIC16F877A Fixed Location in Memory

  9. Steps in Executing Interrupt • 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 interrupted

  10. 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.

  11. Types of Interrupts • Hardware interrupts: • 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.

  12. Sources of Interrupt in PIC16F87XA • Example of sources of interrupts: • Each Timers (Timer 0, 1, 2) • 1 interrupt for external hardware: Pin RB0 (INT) • 2 interrupts for serial communication USART (Receive and Transmit) : Pin RC6 (TX) & RC7 (RX) • The PORTB-Change interrupt: Pin RB7 - RB4 • The ADC (Analog-to-Digital Converter) • The CCP (Compare Capture PWM) • And many more… In this chapter, only these will be explained

  13. PIC16F877 Interrupts High Priority Low Priority

  14. Enabling and Disabling Interrupt • The PIC16F87xA 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 xxIE (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 xxIF (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.

  15. 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 D7 bit of INTCON (Interrupt Control) register is responsible for enabling and disabling interrupts globally - GIE (Global Interrupt Enable • GIE = INTCON<7>

  16. 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 (PIE1, PIE2) Register – contains corresponding peripheral interrupt enable bits • PIR (PIR1, PIR2) Register – contains peripheral interrupt flag bits  Note: Please refer Microchip PIC16F87XA Data Sheet (PDF file) to look at the interrupt control registers in more details.

  17. INTCON Register • A readable and writable register which contains various enable and flag bits for TMR0 register overflow, RB port change and external RB0/INT pin interrupts. Global Interrupt Enable: GIE (INTCON<bit7>) • GIE = 1 : enabling all interrupts (unmask interrupt) • Interrupts are allow to happen. Each interrupt source is enabled by setting to HIGH the corresponding interrupt enable (IE) bit. • GIE = 0 : disabling all interrupts globally (mask interrupt) • 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.

  18. 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 uC 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. TMR0IE (INTCON<bit 5>) & INTE (INTCON<bit 4>) • These bits, along with GIE, must be set HIGH for an interrupt to be responded to

  19. INTCON register

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

  21. PIE1 register

  22. PIE2 register

  23. PIR Register • PIR1: Contains the individual flag bits of the peripheral interrupts. • PIR2: Contains the individual flag bits for the CCP2 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.

  24. PIR1 register

  25. PIR2 register

  26. Programming Timer Interrupt Polling Method Interrupt Method • Used in “Timer Programming” chapter, Timer 0 and 1 using polling method are discussed. • In polling, have to wait until the timer flag, TMR0IF (INTCON<2>) or TMR1IF (PIR1<0>) is raised when timer rolls over. • Drawback: µC is tied down waiting for timer flag to be raised, and cannot do anything else. • Used in this chapter, utilize interrupts to program PIC16 timers. • If the timer interrupt in the interrupt register is enabled, TMR0IF or TMR1IF 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.

  27. 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 delay.

  28. Programming Timer Interrupt • To use an interrupt, we must enable the interrupt because all interrupts are masked upon power-on reset. • TMRxIE bit: enables interrupt for Timer x Timer Interrupt Flag Bits and Associated Registers INTCON Register with Timer0 Interrupt Enable and Interrupt Flag

  29. Timer Interrupt See Chapter “Timer Programming” , Lab Module 6 “Timer” and PIC16F87XA Data Sheet for more details. Timer0 Interrupt • An overflow (FFH → 00H) in the TMR0 register will set flag bit, TMR0IF (INTCON<2>). • The interrupt can be enabled/disabled by setting/clearing enable bit, TMR0IE (INTCON<5>). Timer1 Interrupt • An overflow (FFFFH → 0000H) in the TMR1 register (TMR1H:TMR1L) will set flag bit, TMR1IF (PIR1<0>). • The interrupt can be enabled/disabled by setting/clearing enable bit, TMR1IE (PIE1<0>).

  30. Steps in Programming Timer Interrupt • Initialize PIC. • Off the Timer0 or Timer1 by loading the value into the OPTION_REG or T1CON register. • Load reg. TMR0 or TMR1 with initial value. • Clear the TMR0IF flag or TMR1IF flag. INTCONbits.TMR0IF=0; // Timer0 PIR1bits.TMR1IF=0; // Timer1 • Enable the Timer Interrupt TMR0IE or TMR1IE INTCONbits.TMR0IE=1; // Timer0 PIE1bits.TMR1IE=1; // Timer1 • Enable peripheral interruptsINTCONbits.PEIE=1; • Enable all interruptsINTCONbits.GIE=1;

  31. Steps in Programming Timer Interrupt • Start the Timer0 or Timer1. • Write Main Function. Do main function while waiting for interrupt. • When interrupt raised (TMR0IF=1 or TMR1IF =1), jump to ISR: void interruptISR_label (void) • In ISR: • Stop the Timer0 or Timer1. • Write ISR function. • Reload TMR0 or TMR1 register. • Clear the TMR0IF flag or TMR1IF flag. • Re-enable interrupts. • Start Timer0 or Timer1 (if required in ISR). “interrupt” is a reserved keyword

  32. Write a program to generate a square wave with a period of 2 ms on pin RB5. Use Timer0 with 1:256 prescaler. Example 1 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 LED5 RB5 // LED for Timer1 void interrupt chk_isr(void) ; // declare interrupt check ISR function void Timer0_ISR(void); // declare Timer0 ISR function void main(void) { TRISB=0b00000000; // RB5 as output PORTB=0b00000000; // LED RB5 off OPTION_REG=0b00100111; //Off Timer0, Timer0 module, 1:256 TMR0=0xEC; // load Timer0 register with initial value INTCONbits.TMR0IF=0; // clear Timer0 interrupt flag bit TMR0IF (INTCON<2>); INTCON=0b00000000; INTCONbits.TMR0IE=1; // enable Timer0 interrupt; INTCON=0b11100000 INTCONbits.PEIE=1; INTCONbits.GIE=1;

  33. “interrupt” is a reserved keyword while(1) // keep looping until interrupt comes, jumps to interrupt function { OPTION_REG=0b00000111; // turn on Timer0 T0CS (OPTION_REG<5>) } } void interrupt chk_isr(void) // interrupt function { if(INTCONbits.TMR0IF==1) // Timer0 causes interrupt? { Timer0_ISR(); // Yes. Execute Timer0 ISR function } } void Timer0_ISR(void) // Timer0 ISR function { OPTION_REG=0b00100111; // OFF Timer0 LED5 = !LED5; // toggle RB5 TMR0=0xEC; // re-load Timer0 register INTCONbits.TMR0IF=0; // clear Timer0 interrupt flag bit TMR0IF (INTCON<2>) INTCONbits.TMR0IE=1; // enable Timer0 interrupt; INTCON=0b11100000 INTCONbits.PEIE=1; INTCONbits.GIE=1; }

  34. Write a program to generate a square wave with a period of 2 ms on pin RB5. Use Timer1 without prescaler. Example 2 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 LED7 RB7 // LED for Timer1 void interrupt chk_isr(void) ; void Timer1_ISR(void); void main(void) { TRISB=0b00000000; // RB7 as output PORTB=0b00000000; // LED RB7 off T1CON=0b00000000; / / off Timer1 TMR1H=0xEC; // load initial value in TMR1 register TMR1L=0x78; PIR1bits.TMR1IF=0; // clear Timer1 interrupt flag bit TMR1IF (PIR1<0>); PIR1=0b00000000; PIE1bits.TMR1IE=1; // enable Timer0 interrupt: TMR1IE(PIE1<0>); PIE1=0b00000001; INTCONbits.PEIE=1; // PEIE(INTCON<6>),GIE(INTCON<7>); INTCON=0b11000000 INTCONbits.GIE=1;

  35. while(1) // keep looping until interrupt comes, jumps to interrupt function { T1CON=0b00000001; // turn on Timer1 } } void interrupt chk_isr(void) // interrupt function { if(PIR1bits.TMR1IF==1) // Timer1 causes interrupt? { Timer1_ISR(); // Yes. Execute Timer1 ISR function } } void Timer1_ISR(void) // Timer1 ISR function { T1CON=0b00000000; // off Timer1 LED7 = !LED7; // toggle RB7 TMR1H=0xEC; // re-load TMR1 register TMR1L=0x78; PIR1bits.TMR1IF=0; // clear Timer1 interrupt flag bit TMR1IF (PIR1<0>); PIR1=0b00000000; PIE1bits.TMR1IE=1; // enable Timer0 interrupt: TMR1IE(PIE1<0>); PIE1=0b00000001; INTCONbits.PEIE=1; // PEIE(INTCON<6>),GIE(INTCON<7>); INTCON=0b11000000 INTCONbits.GIE=1; }

  36. Write a program to generate a square wave: • with a period of 2 ms on pin RB5. Use Timer0 with 1:256 prescaler. • With a period of 1 ms on pin RB7. Use Timer1 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 LED5 RB5 // for Timer0 #define LED7 RB7 // for Timer1 void pic_init(void); // declare function void timer_init(void); void intr_init(void); void interrupt chk_isr(void); void Timer0_ISR(void); void Timer1_ISR(void); void main(void) // main function { pic_init(); timer_init(); intr_init(); OPTION_REG=0b00000111; // turn on Timer0 T1CON=0b00000001; // turn on Timer1 while(1); // keep looping until interrupt comes, jumps to interrupt function }

  37. void pic_init(void) // pic initialization { TRISB=0b00000000; // RB5 & RB7 as output PORTB=0b00000000; // LED RB5 & RB7 off } void timer_init(void) // timer initialization { OPTION_REG=0b00100111; // off Timer0 TMR0=0xEC; // load Timer0 register with initial value T1CON=0b00000000; // off Timer1 TMR1H=0xF6; // load register TMR1 with initial value TMR1L=0x3C; } void intr_init(void) // interrupt initialization { INTCONbits.TMR0IF=0; // clear Timer0 interrupt flag bit TMR0IF (INTCON<2>); INTCONbits.TMR0IE=1; // enable Timer0 interrupt: TMR0IE(INTCON<5>) PIR1bits.TMR1IF=0; // clear Timer1 interrupt flag bit TMR1IF (PIR1<0>); PIR1=0b00000000; PIE1bits.TMR1IE=1; // enable Timer1 interrupt: TMR1IE(PIE1<0>); PIE1=0b00000001; INTCONbits.PEIE=1; // PEIE(INTCON<6>),GIE(INTCON<7>); INTCON=0b11000000 INTCONbits.GIE=1; }

  38. void Timer0_ISR(void) { OPTION_REG=0b00100111; // OFF Timer0 LED5 = !LED5; // toggle RB5 TMR0=0xEC; //reload TMR0 value intr_init(); // re-enabling (initialize) interrupts OPTION_REG=0b00000111; // turn on Timer0 } void interrupt chk_isr(void) // interrupt function. Check if Timer0 or Timer1 is overflow { if(INTCONbits.TMR0IF==1) // Timer0 causes interrupt? { Timer0_ISR(); // Yes. Execute Timer0 ISR function } if(PIR1bits.TMR1IF==1) // Timer1 causes interrupt? { Timer1_ISR(); // Yes. Execute Timer1 ISR function } }

  39. void Timer1_ISR(void) { T1CON=0b00000000; // off Timer1 LED7 = !LED7; // toggle RB7 TMR1H=0xF6; // re-load TMR1 value ` TMR1L=0x3C; intr_init(); // re-enabling (initialize) interrupts T1CON=0b00000001; // turn on Timer1 }

  40. Programming External Hardware Interrupt • PIC16F87XA has1 interrupt for external hardware: INT (Pin RB0) • Upon activation of this pin, PIC16 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 0004H, unless specified otherwise. • INT is a positive-edge-triggered interrupt (default for power-on reset): LH signal is applied to pin RB0, the INTF bit is raised, causing uC to be interrupted. • The raising of INTF forces uC to jump to location 0004H in the vector table to service ISR. Hardware Interrupt Flag Bits and Associated Registers

  41. External Interrupt (INT) • External interrupt on the RB0/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 RB0/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 PIC16F87XA Data Sheet.

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

  43. Steps in Programming External Hardware Interrupt • Initialize PIC – TRISBbits.TRISB0=1;(RB0 as input for interrupt) • Clear the external interrupt flagINTCONbits.INTF=0; • Enable Timer interruptINTCONbits.INTE=1; • Enable all interruptsINTCONbits.GIE=1; • Write Main Function. • Check for external interrupt come from RB0/INT pin. • When interrupt raised (INTCONbits.INTF=1 ), jump to ISR. • In ISR: • Clear the external interrupt flag INTCONbits.INTF=0; • Write ISR function. • Re-enable interrupts.

  44. Example 4 Assume pin RB0/INT is connected to a pulse generator (external hardware). • 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 RB0/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 RB0 pin. In this program we will not connect any external device to the PORTB0. 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. Thus, when there is a logic change in the pin RB0, that is from "0" to "1", the external interrupt flag will be set (INTF == 1).

  45. 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 LED7 RD7 // define LED #define BZ RC2 // define buzzer void interrupt beep(void); //declare interrupt beep function void main(void) //main function { TRISB=0b00000000; // interrupt from RB0/INT pin PORTB=0x00; TRISC=0x00; // Port C as output PORTC=0x00; // set value of Port C to 0 TRISD=0x00; // Port D as output PORTD=0x00; // set value of Port D to 0 INTCONbits.INTF=0; // clear external interrupt flag INTCONbits.INTE=1; // enable external interrupt from pin RB0/INT INTCONbits.GIE=1; // global interrupt enable; enable all interrupts “create an external interrupt” using our program itself: TRISB=0x00 If connect to any external device: TRISB=0x01 (RB0 as input)

  46. Example 4 (a) - continued Logic change in the pin RB0, "0"  "1", the external interrupt flag will be set (INTF == 1). while(1) { PORTB=PORTB++; // increment the Port B value by 1 __delay_us(100); // a simple delay if(RB0==1) // check for external interrupt INTR/RB0 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 LED7=!LED7; // toggle LED5 BZ = !BZ; // on off Buzzer }

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

  48. Example 4 (b) - continued while(1) { PORTB=PORTB++; // increment the Port B value by 1 __delay_us(100); // a simple delay if(RB0==1) // check for external interrupt INTR/RB0 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 { LED5=!LED5; // toggle LED5 BZ = !BZ; // on off Buzzer } }

  49. End Of Chapter

More Related