Documente Academic
Documente Profesional
Documente Cultură
Interrupt Programming
• Contra and compare interrupts versus polling
• Explain the purpose of the ISR
• List all the major interrupts of the PIC18
• Explain the purpose of the IVT
• Enable and disable PIC18 interrupts
• Program the interrupt using c language
Interrupt
• Whenever any device needs the
microcontroller’s service the device notifies it by
sending an interrupt signal. Upon receiving an
interrupt signal, the microcontroller stops
whatever it is doing and serve the device. 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
Polling
• 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.
• Cannot assign priority because it checks all devices in
a round-robin fashion.
• Cannot ignore a devices for service
Interrupt Service Routine (ISR)
Power-on-Reset 0000
BSF INTCON,GIE
BCF INTCON,GIE
Timer Interrupts
Interrupt Flag Bit Register Enable Bit Register
#include <P18f458.h>
#define MyPB1bit PORTBbits.RB1 //input switch
#define MyPB1bit PORTBbits.RB7 //input switch
void T0_ISR(void);
void T1_ISR(void);
#pragma interrupt my_isr
void chk_isr(void)
{
If(INTCONbits.TMR0IF==1) //T0 causes int ?
T0_ISR //yes, execute T0 ISR
If(INTCONbits.TMR1IF==1) //T1 causes int ?
T1_ISR //yes, execute T0 ISR
}
#pragma code My_HiPrio=0x0008 //High priority int loc
void My_HiPrio_int(void)
{
_asm
GOTO chk_isr
_endasm
}
#pragma code
void main(void)
{
TRISBbits.TRISB1=0; //PB1 as output;
TRISBbits.TRISB7=0; //PB7 as output;
TRISC = 0xFF //PC as intput;
TRISD = 0x00 //PD as output;
T0CON = 0x00; //T0, 16-bit , no pre-scale
TMR0H = 0x35;
TMR0L = 0x00;
T1CON = 0x08; //T1, 16-bit , no pre-scale
TMR0H = 0x35;
TMR0L = 0x00;
INTCONbits.TMR0IF=0; //clear TF0;
PIR1bits.TMR1IF=0; //clear TF1;
INTCONbits.TMR0IE=1; //enable T0 int;
INTCONbits.TMR1IE=1; //enable T1 int;
INTCONbits.TMR0ON=1; //turn on T0;
INTCONbits.TMR1ON=1; //turn on T1;
INTCONbits.PEIE=1; //enable all pheperal int;
INTCONbits.GIE=1; //enable all int globally;
While(1) //keep looping until int occur
{
PORTD=PORTC //send data fromPC to PD
}
}
void T0_ISR(void)
{
myPB1bit=~myPB1bit; //toggle PB.1;
TMR0H = 0x35;
TMR0L = 0x00;
INTCONbits.TMR0IF=0; //clear TF0;
}
void T1_ISR(void)
{
myPB7bit=~myPB7bit; //toggle PB.7;
TMR1H = 0x35;
TMR1L = 0x00;
INTCONbits.TMR1IF=0; //clear TF1;
}
External Hardware Interrupts
Interrupt Flag Bit Register Enable Bit Register
Positive-edge-
triggered interrupt
EX. When ↑ (rise-edge) on pin INT0, LED will toggle.
#include <P18f458.h>
#define Mybit PORTBbits.RB7
void chk_isr(void);
void INT0_ISR(void);
void INT0_ISR(void)
{
mybit=~mybit; //toggle mybit;
INTCONbits.INT0IF=0; //clear INT0 flag;
}
Serial Communication Interrupts
Interrupt Flag Bit Register Enable Bit Register
TXIF TXIF PIR1 TXIE PIE1
(Trasmit)
#include <P18f458.h>
void chk_isr(void);
void TX_ISR(void);
void RC_ISR(void);
void main(void)
{
TRISD = 0xFF //PD as intput;
TRISB = 0x00 //PB as output;
TRISBbits.TRISC6=0; //TX pin output;
TRISBbits.TRISC7=1; //RC pin input;
TXSTA=0x20 //low baud rate, 8-bit
SPBRG=15 //9600 baud rate (XTA =10
RCSTAbits.CREN=1;
RCSTAbits.SPEN=1;
TXSTAbits.TXEN=1;
PIE1bits.RCIE= 1; //enable RC int
PIE1bits.TXIE= 1; //enable TX int
INTCONbits.PIE=1; //enable peripheral int;
INTCONbits.GIE=1; //enable all int globally;
While(1); //keep looping until int occur
}
void TX_ISR(void)
{
TXREG=PORTD;
}
void RC_ISR(void)
{
PORTB=RCREG;
}
}
PORTB-Change Interrupt
Differences Between External Hardware and
PORTB-Change Interrupt
External Hardware PORTB-Change
This (RB0-RB2) interrupts has its own pin Use all four (RB4-RB7) and considered to be
and is independent each other a single interrupt even though it can use up
to four pins
Has it own flag (INTxIF) and independent Single flag only (RBIF)
each other
#include <P18f458.h>
#define LED1 PORTCbits.RC1
#define LED2 PORTCbits.RC2
void chk_isr(void);
void RBINT_ISR(void);
void chk_isr(void)
{
If(INTCONbits.RBIF==1 //RBIF causes int ?
RBINT_ISR(); //yes, execute INT0 prg
}
void main(void)
{
TRISCbits.TRISC1=0; //PC4 as output;
TRISCbits.TRISC2=0; //PC5 as ouput;
TRISBbits.TRISB4=1; //PB4 input for int;
TRISBbits.TRISB5=1; //PB5 input for int;
INTCONbits.RBIF=0; //clear RIF;
INTCONbits.RBIE=1; //enable RB int;
INTCONbits.GIE=1; //enable all int globally;
}
void RBINT_ISR(void)
{
LED1 = PORTBbits.RB4;
LED2 = PORTBbits.RB5;
INTCONbits.RBIF=0; //clear RBIF flag;
}
Interrupt Priority
Interrupt ROM Location (hex)
Power-on-Reset 0000