An Overview Of External Interrupt
An interrupt of the MCU is an event that triggers a notification to the CPU. The CPU itself could responds to that events, or could ignore. Responding to the interrupt takes a very short time to handle and come back to the program main loop. Interrupt is very fast. It make the embedded controller program more responsive and more effective.
In the mid-range PIC microcontroller, an interrupt could create by an external events or by an internal peripheral operation. The external event may cause by any logic change to any digital input pin. A communication to the external device also create an interrupt, but we don't want to talk more about this here.
Programming Sample Of External Interrupt |
For PIC16F887 the interrupt creates by the digital input output pin has two categories - an external interrupt creates by RB0-INT input pin, and the interrupt on change creates by any pin on PORTB input.
In this post we gonna talk about the external interrupt creates by INT pin. There are two choices of logic changes that could trigger an interrupt on this pin - on the falling edge and on the rising edge. The OPTION_REG register configures each choices.
The OPTION_REG register |
Bit 6 (INTEDG) selects between the rising and the falling edge of interrupt creates by the pin INT. Programming the external interrupt we need to configure the following stuffs:
- Set RB0-INT pin as a digital input
- Optionally turn on its individual weak pull up resistor.
- Turn on the external interrupt enable (INTE) bit and the global interrupt enable (GIE) bit in the interrupt control register (INTCON).
- Select between that two interrupt edge
- Clear the external interrupt flag (INTF) of the INTCON register.
- In MikroC, write the Interrupt Service Routine (ISR) to handle the job. Within this routine the programmer must firstly test the INTF to get the external interrupt event, then writing a block of codes to for this event. Within the final line of this block, the INTF bit must be clear by software.
The Interrupt Control Register (INTCON) has many configurations and flag as lists below.
Interrupt Control Register (INTCON) |
External Interrupt Programming In MikroC
This example may become difficult to some novice microcontroller programmer. To get a simple introductory programming example about using the external interrupt in MikroC, see this post.
Here, the MCU display the counting data on a multiplexed 7-segments display. The counting has a 99 maximum decimal values. After reaching the maximum value it rolls back to 0.
The counting increment creates by the falling edge of the INT pin. As mentioned earlier, using the interrupt the main program keeps executing its main task. The interrupt event occurs in a very short time and the main program doesn't need to poll for this event.
MikroC Program
#define SW PORTB.RB0 #define RATE 10 char CNT=0; void interrupt() { if(INTCON.INTF) CNT++; INTCON.INTF=0; } void SSD(char TEN,char ONE) { char LED[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; // ROUTINE FOR SEVEN SEGMENTS PORTA=LED[TEN]; PORTB=0x80; delay_ms(RATE); PORTB=0x00; PORTA=LED[ONE]; PORTB=0x40; delay_ms(RATE); PORTB=0x00; } void main() { char CNT1=0; PORTA=0x00; // CLEAR PORTBA PORTB=0x00; // CLEAR PORTB TRISA=0x00; // SET PORTA AS OUTPUT TRISB=0x01; // SET RB0 AS INPUT ANSELH=0x00; // DISABLE ANALOG FUNCTION ON PORTB OSCCON|=0x70; // SELECT 8MHz INTERNAL OSCILLATOR OPTION_REG.NOT_RBPU=0; // Enable Global WPUB OPTION_REG.INTEDG=0; // Interrupt from High TO Low WPUB=0x01; // Enable WPUB for RB0 INTCON.GIE=1; // Enable Global Interrupt INTCON.INTE=1; // Enable External Interrupt INTCON.INTF=0; // Clear External Interrupt Flag // MAIN ACTIVITY while(1) { if(CNT==10) { CNT=0; CNT1++; } if (CNT1==10) CNT1=0; SSD(CNT1,CNT); } }
No comments:
Post a Comment