Professional Documents
Culture Documents
Introduction
Interrupts in the PIC16F877A
Setting up the Interrupts
Using the Interrupts
Summary
Introduction
10
11
14
15
16
21
22
23
#include <htc.h>
#include <pic16f877a.h>
#define _XTAL_FREQ 20000000 // Set the oscillator frequency to 20MHz
int i=0;
int j=0;
//---Function Definition----//--Interrupt Service function-interrupt ISR (void) // Interrupt function definition
{
if (RBIF) // If the RB change flag is 1, do .....
{
RBIE=0; //Disable RB change interrupt
{
while (!RB7); //Detect Pin B7 to be logic low (Falling Edge)
__delay_ms(100);
PORTC=j;
j++;
}
RBIF=0; // Reset the external interrupt flag
RBIE=1; // RE-Enable RB change interrupt
}
GIE=1; //Enable Global Interrupt
}
25
26
#include <htc.h>
#include <pic16f877a.h>
#define _XTAL_FREQ 20000000 // Set the oscillator frequency to 20MHz
char count1=0, count2=0;
//--Interrupt Service function-interrupt ISR(void) // Interrupt function definition
{
if (TMR1IF) // If the TMR1 Overflow flag is 1, do .....
{
TMR1IE=0; // TMR1 Overflow Interrupt Disable
TMR1ON=0; //Turn off Timer 1
RB0=!RB0; //Blinking the LED RB0
TMR1L=count1; //Change the counter values for Timer1
TMR1H=count2; //Change the counter values for Timer1
TMR1IF=0; // Reset the TMR1 Overflow interrupt flag
TMR1IE=1; // TMR1 Overflow Interrupt Enable
TMR1ON=1; //Turn on Timer 1
}
GIE=1; //Enable back the global interrupt inputs, if not interrupt only happens for once! HahA!
}
28
29
}
30
#include <htc.h>
#include <pic16f877a.h>
#define _XTAL_FREQ 20000000 // Set the oscillator frequency to 20MHz
char count1=0, count2=0;
//--Interrupt Service function-interrupt ISR(void) // Interrupt function definition
{
if (TMR0IF) // If the TMR0 Overflow flag is 1, do .....
{
TMR0IE=0; // TMR0 Overflow Interrupt Disable
RB0=!RB0; //Blinking the LED RB0
if (count2++%2)
TMR0=count1; //Change the counter values for Timer0
else
TMR0=255-count1; //Change the counter values for Timer0
TMR0IF=0; // Reset the TMR0 Overflow interrupt flag
TMR0IE=1; // TMR0 Overflow Interrupt Enable
}
GIE=1;
}
32
33
34
#include <htc.h>
#include <pic16f877a.h>
#define _XTAL_FREQ 20000000 // Set the oscillator frequency to 20MHz
//--Interrupt Service function-interrupt ISR(void) // Interrupt function definition
{ if (TMR2IF) // If the TMR2 Overflow flag is 1, do .....
{
TMR2IE=0; // TMR2 Overflow Interrupt Disable
RB2=!RB2; //Blinking the LED RB2
TMR2IF=0; // Reset the TMR2 Overflow interrupt flag
TMR2IE=1; // TMR2 Overflow Interrupt Enable
}
GIE=1;
}
36
Another example (Using Timer2 interrupt to generate pulse, continued from previous
slide):
37
38
39
40
Summary
An interrupt causes the normal program
execution to be suspended and an interrupt
function is executed. Normal program execution
continues when the interrupt function returns.
The PIC16F877A has a number of interrupt
sources such as a timer overflow, an incoming
RS-232 character or a change on a pin (Port B4B7).
Interrupts are disabled by default.
They must be enabled before being used
43
Summary
To set up the interrupts, at first, clear the
interrupt flags and enable the interrupts at the
main function
For interrupt service routine, the function type
should be interrupt
In the interrupt service routine function, disable
the interrupts before doing the job
After finishing the jobs in interrupt service
routine, clear the interrupt flag and re-enable the
interrupts before leaving the interrupt service
routine
44