PIC Interupts
-
Upload
adee-putra -
Category
Documents
-
view
219 -
download
0
Transcript of PIC Interupts
-
8/10/2019 PIC Interupts
1/44
1
InterruptsInterrupts
IntroductionIntroduction
Interrupts in the PIC16F877AInterrupts in the PIC16F877A
Setting up the InterruptsSetting up the Interrupts Using the InterruptsUsing the Interrupts
SummarySummary
-
8/10/2019 PIC Interupts
2/44
2
IntroductionIntroduction
An interrupt is a specific event that causes the normal programexecution to be suspended wherever it is and an interrupt function isexecuted.
Interrupts increases the program efficiency by allowing externaldevices or internal events to force a change in the executionsequence.
When an interrupt signal is received, the current instruction iscompleted and the address of the next instruction (the returnaddress) is pushed into the first available stack location.
Then the program jumps to program address 004, and continuesfrom there until it sees a Return From Interrupt (RETFIE) instruction.
When the interrupt function ends, the return address is pulled fromthe stack. Program execution then restarts at the original location.
Normal program execution continues when the interrupt functionreturns.
The PIC16F877A has a number of interrupt sources such as a timeroverflow, an incomingRS-232 character or a change on a pin.
-
8/10/2019 PIC Interupts
3/44
3
Interrupts in the PIC16F877AInterrupts in the PIC16F877A
The primary interrupt sources for PIC16F877AThe primary interrupt sources for PIC16F877Aare Timers and Port Bare Timers and Port B
The overflow of the Timers triggers the interruptThe overflow of the Timers triggers the interrupt
RB0/INT is an external interrupt input pin and isconfigured using the INTEDG bit(OPTION_REG)
Pins B4-B7 can be set up so that any change onPins B4-B7 can be set up so that any change onthese inputs initiates the interrupt (these inputs initiates the interrupt (interrupt on-
change feature) This could be used to detect when a button on a
keypad connected to Port B has been pressed
-
8/10/2019 PIC Interupts
4/44
4
Interrupts in the PIC16F877AInterrupts in the PIC16F877A
-
8/10/2019 PIC Interupts
5/44
5
Interrupts in the PIC16F877AInterrupts in the PIC16F877A
-
8/10/2019 PIC Interupts
6/44
6
Setting up the InterruptsSetting up the Interrupts
-
8/10/2019 PIC Interupts
7/44
7
Setting up the InterruptsSetting up the Interrupts
The Interrupt Control register (INTCON) records
individual interrupt requests in flag bits.
It also has individual and global interrupt enable bits.
INTCON contains various enable and flag bits for
the TMR0 register overflow, RB port change and
external RB0/INT pin interrupts.
INTEDG
is Interrupt Edge Select bit If set to 1 : Interrupt on rising edge of RB0/INT pin
If set to 0 : Interrupt on falling edge of RB0/INT pin
-
8/10/2019 PIC Interupts
8/44
8
Setting up the InterruptsSetting up the Interrupts
Interrupt flag bits are set when an interrupt condition
occurs regardless of the state of its corresponding
enable bit or the global enable bit, GIE (INTCON).
User software should ensure the appropriate interruptflag bits are clear prior to enabling an interrupt.
When bit GIEis enabled and an interrupts flag
bit and mask bit are set, the interrupt will vector
immediately.
The GIEbit is cleared on Reset.
-
8/10/2019 PIC Interupts
9/44
9
Setting up the InterruptsSetting up the Interrupts
-
8/10/2019 PIC Interupts
10/44
10
Setting up the InterruptsSetting up the Interrupts
The PIE1 register contains the individual
enable bits for the peripheral interrupts.
The peripheral interrupt sources are fromADC, Timer and Serial I/O
Bit PEIE (INTCON) must be set to
enable any peripheral interrupt.
-
8/10/2019 PIC Interupts
11/44
11
Setting up the InterruptsSetting up the Interrupts
-
8/10/2019 PIC Interupts
12/44
12
Setting up the InterruptsSetting up the Interrupts
The PIR1 register contains the individual
flag bits for the peripheral interrupts.
Interrupt flag bits are set when an interruptcondition occurs regardless of the state of
its corresponding enable bit or the global
enable bit, GIE (INTCON). User software should ensure the
appropriate interrupt bits are clear prior to
enabling an interrupt.
-
8/10/2019 PIC Interupts
13/44
13
Setting up the InterruptsSetting up the Interrupts
The bits in the PIR1 register
ADIF: A/D Converter Interrupt Flag bit
1 = An A/D conversion completed
0 = The A/D conversion is not complete TMR2IF: TMR2 to PR2 Match Interrupt Flag bit
1 = TMR2 to PR2 match occurred (must be cleared in
software)
0 = No TMR2 to PR2 match occurred TMR1IF: TMR1 Overflow Interrupt Flag bit
1 = TMR1 register overflowed (must be cleared in software)
0 = TMR1 register did not overflow
-
8/10/2019 PIC Interupts
14/44
14
Setting up the InterruptsSetting up the Interrupts
-
8/10/2019 PIC Interupts
15/44
15
Setting up the InterruptsSetting up the Interrupts
The PIE2 register contains the individual
enable bits for the CCP2 peripheral
interrupt, the SSP bus collision interrupt,EEPROM write operation interrupt and the
comparator interrupt.
-
8/10/2019 PIC Interupts
16/44
16
Setting up the InterruptsSetting up the Interrupts
-
8/10/2019 PIC Interupts
17/44
17
Setting up the InterruptsSetting up the Interrupts
The PIR2 register contains the flag bits for the
CCP2 interrupt, the SSP bus collision interrupt,
EEPROM write operation interrupt and the
comparator interrupt The peripheral interrupt flags are contained in the
Special Function Registers, PIR1 and PIR2.
The corresponding interrupt enable bits are
contained in Special Function Registers, PIE1 and
PIE2, and the peripheral interrupt enable bit is
contained in Special Function Register, INTCON
-
8/10/2019 PIC Interupts
18/44
18
Setting up the InterruptsSetting up the Interrupts
When an interrupt is responded to, the GIE bit is cleared to
disable any further interrupt, the return address is pushed
onto the stack and the Program Counter (PC) is loaded with
0004h.
Once in the Interrupt Service Routine, the source(s) of the
interrupt can be determined by polling the interrupt flag bits.
The interrupt flag bit(s) must be cleared in software before re-
enabling interrupts to avoid recursive interrupts.
For external interrupt events, such as the INT pin or PORTBchange interrupt, the interrupt latency will be three or four
instruction cycles.
-
8/10/2019 PIC Interupts
19/44
19
Setting up the InterruptsSetting up the Interrupts
By default, all the interrupts in the PIC16F877ABy default, all the interrupts in the PIC16F877A
are disabledare disabled
The interrupts can be enabled byThe interrupts can be enabled by
GIE=1; // Global interrupt enable
The interrupts can be disabled byThe interrupts can be disabled by
GIE=0; // Global interrupt disable
Individual interrupts can be enabled separately asIndividual interrupts can be enabled separately aswellwell
For example,For example, INTE=1; enables the externalenables the external
interrupt input from Pin RB0interrupt input from Pin RB0
-
8/10/2019 PIC Interupts
20/44
20
Using the InterruptsUsing the Interrupts
INT INTERRUPT
External interrupt on the RB0/INT pin is edge triggered, eitherrising if bit INTEDG (OPTION_REG) is set or falling if theINTEDG bit is clear.
When a valid edge appears on the RB0/INT pin, flag bit, INTF
(INTCON), is set. This interrupt can be disabled by clearing enable bit, INTE
(INTCON).
Flag bit INTF must be cleared in software in the InterruptService Routine before re-enabling this interrupt.
The INT interrupt can wake-up the processor from Sleep if bitINTE was set prior to going into Sleep.
The status of global interrupt enable bit, GIE, decides whetheror not the processor branches to the interrupt vector followingwake-up.
-
8/10/2019 PIC Interupts
21/44
21
Using the InterruptsUsing the Interrupts
TMR0 INTERRUPT
An overflow (FFh 00h) in the TMR0
register will set flag bit, TMR0IF
(INTCON).
The interrupt can be enabled/disabled by
setting/clearing enable bit, TMR0IE
(INTCON).
-
8/10/2019 PIC Interupts
22/44
-
8/10/2019 PIC Interupts
23/44
23
Using the InterruptsUsing the Interrupts
The interrupt function is designated by preceding it withinterrupt.
In the interrupt service routine functions can be specified bypreceding each with the proper name like RBIFfor the Port Bchange interrupt (RB4-B7)
An interrupt must be specifically enabled (For exampleRBIE=1for enabling Port B change interrupt) and interruptsmust be globally enabled (GIE=1). The GLOBALenable/disable controls whether any interrupts are serviced.
If interrupts are disabled and an interrupt event happens, thenthe interrupt function will be called when interrupts areenabled. If multiple interrupt events of the same type happenwhile interrupts are disabled, then the interrupt function iscalled only once when interrupts are enabled.
-
8/10/2019 PIC Interupts
24/44
24
Using the InterruptsUsing the Interrupts
In the case of Timer Interrupts,In the case of Timer Interrupts, interrupts aredisabled before the timer is read and combinedwith the overflow count.
This is done to prevent the following situation:
The timer value is read and it is 65535 The overflow interrupt happens and the counter
is incremented to 1
The program continues and reads the counter
as 1 The time is an assumed to be 65536+65535
when in fact the correct time is 65535
-
8/10/2019 PIC Interupts
25/44
25
Using the InterruptsUsing the Interrupts
An example (Using RB interrupts, continues on next slide):An example (Using RB interrupts, continues on next slide):#include
#include
#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
}
-
8/10/2019 PIC Interupts
26/44
26
Using the InterruptsUsing the Interrupts
An example (Using RB interrupts, continued from previous slide):An example (Using RB interrupts, continued from previous slide):
//--Main Funtion --
void main(void)
{ ADCON1 = 0b00000111; //Set Port A as digital I/O
TRISA=0xFF; // Ser PORT A as an OUTPUT
TRISB=0xF0; // Set PORT B as an IN/OUTPUT
nRBPU=0; //Enable Port B Internal Pull Up ResistorsTRISC=0; // Set PORT C as an OUTPUT
PORTC=0; // Set the value of PORT C to 0
INTE=1; // Enable external interrupts from RB0
RBIE=1; //Enable the RB port change interrupt
GIE=1; // Global interrupt enable
RBIF=0; //Clear RB port change interrupt flag
while(1) //Keeps running{ PORTB++;// Increase the PORT B value by 1.
for(i=0;i
-
8/10/2019 PIC Interupts
27/44
27
Using the InterruptsUsing the Interrupts
In this example, the RB interrupt is used
The input of pins B4 to B7 are monitoredusing this interrupt
Falling edge will trigger the interrupt
A falling edge at pin B7 will increase thecounter which output to Port C (Can be 8
LEDs or BCD 7 segment) And now construct the circuit for this
program
-
8/10/2019 PIC Interupts
28/44
28
Using the InterruptsUsing the Interrupts
Another example (Using Timer1 interrupts, continues on next slide):Another example (Using Timer1 interrupts, continues on next slide):#include #include
#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!
}
-
8/10/2019 PIC Interupts
29/44
29
Using the InterruptsUsing the Interrupts
Another example (Using Timer1 interrupt to generate pulse,Another example (Using Timer1 interrupt to generate pulse,continued from previous slide and continues on next slide):continued from previous slide and continues on next slide):
void main (void) //--Main Funtion --
{ unsigned int reading=0;
TRISA=0xFF; // Ser PORT A as an INPUT
TRISB=0xF0; // Set PORT B as an IN/OUTPUTnRBPU=0;
ADCON1=0b01001110; //Set ADFM (bit 7 of ADCON1) to 0 for left justified
//Only RA0 is selected as analog input
ADCON0=0b11000001;
T1CKPS1 = 0; /* Prescaler of 1 */
T1CKPS0 = 0; /* Prescaler of 1 */
TMR1CS = 0; /* Select internal Clock source */
TMR1ON = 1; /* Enable timer1 */
-
8/10/2019 PIC Interupts
30/44
30
Using the InterruptsUsing the Interrupts
Another example (Using Timer1 interrupts, continued from previous slide):Another example (Using Timer1 interrupts, continued from previous slide):
TMR1IE = 1; /* Enable timer1 interrupts */
TMR1L = 0x00; /* Low byte */
TMR1H = 0x00; /* High byte */
GIE=1; // Global interrupt enable
TMR1IF=0; //Clear TMR1 interrupt flagPEIE=1; //Peripheral Interrupt Enable
while (1)
{ __delay_ms(100);
ADCON0=ADCON0|1;
GO = 1; //Start A/D Conversion
while(GO==1); //Wait end of conversion (conversion complete)
reading = (65535-ADRESH*255); //Start A/D conversion and read the datacount1 = reading%256 ;
count2 = reading/256.0;
}
}
-
8/10/2019 PIC Interupts
31/44
31
Using the InterruptsUsing the Interrupts
In this example, the Timer1 interrupt is used
The interrupt will be triggered when Timer1 overflows
Each time the Timer1 interrupt is triggered, the LED atPin RB0 will change the lighting pattern
The counter values for Timer1 is changed according tothe reading from ADC
So the input from ADC will affect the speed of the LEDblinking
And now construct the circuit for this program Dont forget, enable the Peripheral Interrupt Enable bit
(PEIE, bit 7 of INTCON register) if you want to useinterrupt from Timers (1&2)
-
8/10/2019 PIC Interupts
32/44
32
Using the InterruptsUsing the Interrupts
Another example (Using Timer0 interrupt to generate pulse, continues onAnother example (Using Timer0 interrupt to generate pulse, continues onnext slide):next slide):
#include
#include
#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 Timer0else
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;
}
-
8/10/2019 PIC Interupts
33/44
33
Using the InterruptsUsing the Interrupts
Another example (Using Timer0 interrupt to generate pulse,Another example (Using Timer0 interrupt to generate pulse,continued from previous slide and continues on next slide):continued from previous slide and continues on next slide):
//--Main Funtion --
void main(void)
{ TRISA=0xFF; // Ser PORT A as an INPUT
TRISB=0xF0; // Set PORT B as an IN/OUTPUT
ADCON1=0b01001110; //Set ADFM (bit 7 of ADCON1) to 0 for left justified
//Only RA0 is selected as analog input
ADCON0=0b11000001;
T0CS = 0; /* Select internal Clock source */
PSA = 0; /* Prescaler is assigned to the Timer0 module */PS2 = 1;
PS1 = 1;
PS0 = 0; /* Set prescaler rate to 1:128 */
-
8/10/2019 PIC Interupts
34/44
34
Using the InterruptsUsing the Interrupts
Another example (Using Timer0 interrupt toAnother example (Using Timer0 interrupt togenerate pulse, continued from previous slide):generate pulse, continued from previous slide):
TMR0IE = 1; /* Enable timer0 interrupts */
TMR0 = 0x00; /* Timer0 Counter */
GIE=1; // Global interrupt enableTMR0IF=0; //Clear TMR1 interrupt flag
PEIE=1; //Peripheral Interrupt Enable
while (1)
{ __delay_ms(100);
ADCON0=ADCON0|1;
GO = 1; //Start A/D Conversion
while(GO==1); //Wait end of conversion (conversion complete)count1 = 255-ADRESH; //Start A/D conversion and read the data
}
}
//End of program
-
8/10/2019 PIC Interupts
35/44
35
Using the InterruptsUsing the Interrupts
In this example, the Timer0 interrupt is used forgenerating a pulse with varying pulse width
The interrupt will be triggered when Timer0 overflows
Each time the Timer0 interrupt is triggered, the LED at
Pin RB0 will turn on and off alternately The pulse width is changed according to the reading
from ADC
So the input from ADC will affect the ON time of the LED
Tap the pin of RB0 to an oscilloscope to check thewaveform
And now construct the circuit for this program
-
8/10/2019 PIC Interupts
36/44
36
Using the InterruptsUsing the Interrupts
Another example (Using Timer2 interrupt to generate pulse, continues onAnother example (Using Timer2 interrupt to generate pulse, continues onnext slide):next slide):
#include
#include
#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 flagTMR2IE=1; // TMR2 Overflow Interrupt Enable
}
GIE=1;
}
-
8/10/2019 PIC Interupts
37/44
37
Using the InterruptsUsing the Interrupts
Another example (Using Timer2 interrupt to generate pulse, continued from previousAnother example (Using Timer2 interrupt to generate pulse, continued from previousslide):slide):
//--Main Funtion --
void main(void)
{ TRISC=0x00; //Port C as outputs
TRISB=0x00; //Port B as outputs
T2CON = 0b00001001; // Timer 2 control register
//TOUTPS3:TOUTPS0: Timer2 Output Postscale Select to 1:2//T2CKPS1:T2CKPS0: Timer2 Clock Prescale Select to 4
TMR2IE = 1; /* Enable timer0 interrupts */
PR2 = 0xA0; /* Timer0 Counter */
GIE=1; // Global interrupt enable
TMR2IF=0; //Clear TMR1 interrupt flag
TMR2ON=1; //Turn on Timer 2
PEIE=1; //Peripheral Interrupt Enable
while (1); //Run Run Run!
}
//End of Program
-
8/10/2019 PIC Interupts
38/44
38
Using the InterruptsUsing the Interrupts
The previous example is modified to use the Timer2 interrupt forgenerating a pulse
The interrupt will be triggered when Timer2 overflows
Each time the Timer2 interrupt is triggered, the LED at Pin RB2 willturn on and off alternately (Toggling)
The Timer2 Control Register, T2CONsets Timer 2 with pre-scaler of4, counting from 0 to A0h, post-scaler is 2, so every 2 times Timer 2overflows, an interrupt is generated
If the clock frequency is 20MHz, the instruction clock frequency is5MHz (divide by 4)
Instruction clock period is 0.2 s, the time taken for Timer 2 tooverflow = 160 x 4 x 2 x 0.2 = 256 s
Tap the pin RB2 to an oscilloscope to check the waveform
-
8/10/2019 PIC Interupts
39/44
39
Using the Interrupts (Output toUsing the Interrupts (Output to
Virtual Terminal)Virtual Terminal) Do this example (Continues on next slide):Do this example (Continues on next slide):#include #include
#include
#include
#define _XTAL_FREQ 20000000
#define PUSH_BUTTON RB6
long int overflow_count;
void init_rs232()
{ BRGH = 1;
SPBRG = 129; // set baud rate: Fosc=20Mhz, BR=9600
TX9 = 0; // ninebits?1:0,,,8- or 9-bit transmission
SYNC = 0; // asynchronous
SPEN = 1; // enable serial port pins
TXEN = 1; // enable the transmitter & (automatically)TXIF=1
CREN = 1; // enable receptionSREN = 0; // no effect
TXIE = 0; // disable tx interrupts
RCIE = 0; // disable rx interrupts
}
-
8/10/2019 PIC Interupts
40/44
40
Using the Interrupts (Output toUsing the Interrupts (Output to
Virtual Terminal)Virtual Terminal) Do this example (Continues from previous slide):Do this example (Continues from previous slide):interrupt ISR(void) // Interrupt function definition{ if (TMR1IF) // If the TMR1 Overflow flag is 1, do .....
{ TMR1IE=0; // TMR1 Overflow Interrupt Disable
overflow_count++;
TMR1IF=0; // Reset the TMR1 Overflow interrupt flag
TMR1IE=1; // TMR1 Overflow Interrupt Enable
}
GIE=1;
}
void main( )
{ unsigned long int time ; //32 bit integer data
init_rs232();
TRISB=0xF0; // Set PORT B as an IN/OUTPUTnRBPU=0;
T1CKPS1 = 0; /* Prescaler of 1 */
T1CKPS0 = 0; /* Prescaler of 1 */
TMR1CS = 0; /* Select internal Clock source */
TMR1ON = 1; /* Enable timer1 */
TMR1IE = 1; /* Enable timer1 interrupts */
TMR1L = 0x00; /* Low byte */
TMR1H = 0x00; /* High byte */
GIE=1; // Global interrupt enable
TMR1IF=0; //Clear TMR1 interrupt flag
PEIE=1; //Peripheral Interrupt Enable
while(1){ while(PUSH_BUTTON);// Wait for press
TMR1H=0;
TMR1L=0;
overflow_count=0;
while(!PUSH_BUTTON);//Wait for release
GIE=0; //Disable all interrupts
time = TMR1H*256+TMR1L;
time = time + (overflow_count
-
8/10/2019 PIC Interupts
41/44
41
Using the Interrupts (Output toUsing the Interrupts (Output to
Virtual Terminal)Virtual Terminal)
In this example, the timer 1 overflow interrupt will
be used to extend the timer 1 timer from 16 bits
to 32 bits by counting the number of times the
timer overflows. When running the program, press the button and
release, the time it was held down is shown to 6
decimal places in the Monitor panel.
The %06lu format specifier is the same as %6lu
except leading zeros are printed.
-
8/10/2019 PIC Interupts
42/44
42
Using the Interrupts (Output toUsing the Interrupts (Output to
Virtual Terminal)Virtual Terminal)
The formula time/5000000in this printf("Time is %lu.%06lu seconds.\r\n",time/5000000, (time/5)%1000000);
Clock frequency is 20MHz, so instructionclock frequency is 5MHz = 5000000 Hz
The timer value will increase by one, eachand everytime the instruction clock
complete a cycle so the time to take for the timer value toproduce N cycles will become N/5000000
-
8/10/2019 PIC Interupts
43/44
43
SummarySummary
An interrupt causes the normal programexecution to be suspended and an interruptfunction is executed.Normal program executioncontinues when the interrupt function returns.
The PIC16F877A has a number of interruptsources such as a timer overflow, an incomingRS-232 character or a change on a pin (Port B4-B7).
Interrupts are disabled by default.Interrupts are disabled by default.
They must be enabled before being usedThey must be enabled before being used
-
8/10/2019 PIC Interupts
44/44