Monday, 23 July 2018

External Interrupt Using PIC Microcontroller (PIC18F4520)

Aim:

Although micro controllers can accept inputs from Digital I/O Ports,interrupts are preferred for accepting inputs generated by external events. This is mainly because of the less attention required for an interrupt based program. An Interrupt Event directs the flow of program execution to a totally independent piece of code, known as “Interrupt Sub-Routine”. There are many sources of Interrupts that are available for a micro controller. Most of them are generated by the internal modules and are known as internal interrupts. And there are two pins that accept external signals directly and allows external sources to generate interrupts.

Description:


External interrupts on the RB0/INT0, RB1/INT1 and RB2/INT2 pins are edge-triggered. If the corresponding INTEDGx bit in the INTCON2 register is set (= 1), the interrupt is triggered by a rising edge; if the bit is clear, the trigger is on the falling edge. When a valid edge appears on the RBx/INTx pin, the corresponding flag bit, INTxIF, is set. This interrupt can be disabled by clearing the corresponding enable bit, INTxIE. Flag bit, INTxIF, must be cleared in software in the Interrupt Service Routine before re-enabling the interrupt. All external interrupts (INT0, INT1 and INT2) can wakeup the processor from Idle or Sleep modes if bit INTxIE was set prior to going into those modes. If the Global Interrupt Enable bit, GIE, is set, the processor will branch to the interrupt vector following wake-up. Interrupt priority for INT1 and INT2 is determined by the value contained in the Interrupt Priority bits, INT1IP (INTCON3<6>) and INT2IP (INTCON3<7>). There is no priority bit associated with INT0. It is always a high-priority interrupt source.

bit 7 RBPU: PORTB Pull-up Enable bit
        1 = All PORTB pull-ups are disabled
        0 = PORTB pull-ups are enabled by individual port latch values
bit 6 INTEDG0: External Interrupt 0 Edge Select bit
        1 = Interrupt on rising edge
        0 = Interrupt on falling edge
bit 5 INTEDG1: External Interrupt 1 Edge Select bit
        1 = Interrupt on rising edge
        0 = Interrupt on falling edge
bit 4 INTEDG2: External Interrupt 2 Edge Select bit
        1 = Interrupt on rising edge
        0 = Interrupt on falling edge
bit 3 Unimplemented: Read as ‘0’
bit 2 TMR0IP: TMR0 Overflow Interrupt Priority bit
        1 = High priority
        0 = Low priority
bit 1 Unimplemented: Read as ‘0’
bit 0 RBIP: RB Port Change Interrupt Priority bit
        1 = High priority
        0 = Low priority

Block Diagram

3 switch

Schematic


Code


// ******************************************************
// Project: Interfacing external interrupt to atmega8
// Author: Hack Projects India
// Module description: Operate single LED
// ******************************************************
#include<p18f4520.h>
#pragma config OSC=HS, FCMEN=ON, WDT=OFF, IESO=OFF, XINST=OFF, LVP=OFF
void chk_isr(void);
void delay(unsigned int);
#pragma code high_vector = 0x08
void interrupt_at_high_vector(void)
{
    _asm
    GOTO chk_isr
    _endasm
}
#pragma code //return to the default code section
#pragma interrupt chk_isr
void chk_isr(void)
{
    if(INTCONbits.INT0IF==1)
    {
        PORTCbits.RC3 = 1;/*!PORTCbits.RC3*/;
        delay(100);
        PORTCbits.RC3 = 0;
        INTCONbits.INT0IF=0; //clear INT0 flag
        INTCONbits.GIE=0;
    }
}
void delay(unsigned int z)
{
    unsigned int i;
    unsigned char j;
    for(i=0;i<z;i++)
    for(j=0;j<=165;j++);
}

void main(void)
{

    TRISCbits.TRISC3=0;
    TRISBbits.TRISB0=1;
    ADCON1=0x0f; //Make RB0(INT0) and RA0 as digital
    INTCONbits.INT0IF=0; //to delete the previous interrupts occurs
    INTCONbits.INT0IE=1;
    INTCONbits.GIE=1;
    while(1)
    {
        // Program run
    }
}


Downloads:

The code was compiled in Keil uvision4 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.

Further Reading suggestions:

No comments:

Post a Comment