EXPERIMENT
TITLE
OBJECTIVES
MATERIAL AND
APPARATUS
4
INTERRUPT PROGRAMMING IN C
At the end of this lab session, the student should be able to:
1. Know PIC interrupts.
2. Understand PIC interrupt programming in C.
3. Apply interrupt programming in PIC.
ESPIC40C Trainer board , Personal Computer with MPLAB IDE, Serial Bootloader and,
PIC18F45K22 Datasheet.
Interrupts, as the name suggests interrupts the normal execution and Requests and
urgent attention of CPU. Interrupts are situations that the CPU cant predict when they
will happen, they can happen any time, so the CPU does not wait for them. So the CPU
keeps on doing its normal job unless and interrupt occurs. For example when the USART
(Serial Communication Hardware) will receive data is unknown, it can receive data any
time. So the CPU keeps on doing its normal job, which may be for example read
temperature using LM35 sensor and display on LCD. The CPU keeps on doing the
"normal" job, but as soon as the USART receive data it informs the CPU using an
interrupt. The CPU save its current state (so that it can resume), and jumps to the ISR
(interrupt service routine) immediately. Where we can process the command or put it in
a FIFO queue (to process latter). The ISR is generally kept very small and fast. As soon as
the ISR ends, the CPU restores its saved state and resumes where it left. In this way CPU
does not missed any data byte.
THEORY
Flag Bit It is set automatically by the related hardware when the interrupt condition
occurs. It is generally suffixed with IF (Interrupt Flag). When it is set to 1, know that
interrupt has occurred. For example when TMR0IF is set by TIMER0, it indicates that
TIMER0 has overflowed.
Priority Bit - Can leave this for now to keep things simple.
In global level there are following bits to control the interrupts globally:
GIE Global Interrupt Enable, enables/disables interrupts globally.
PEIE Enable/disable all peripheral interrupts.
PROCEDURES:
EXERCISES 1 (HANDLING INTERRUPT IN C)
1.
Open existing Project . 1st Open the project file. Then, double click the project file with MPLAB logo.
File -> New...
2.
Write the program -> compile , connect the trainer board and write the programe.
3.
while (1);
}
//Main Interrupt Service Routine (ISR)
void interrupt ISR( )
{
//Check if it is TMR0 Overflow ISR
if(TMR0IE && TMR0IF)
{
//TMR0 Overflow IS
counter++;//Increment Over Flow Counter
if(counter==30)
{
//Toggle RB1 (LED)
if(RB1==0)
RB1=1;
else
RB1=0;
counter=0; //Reset Counter
}
//Clear Flag
TMR0IF=0;
}
}
EXERCISE 2
Develop the schemathic diagram by using Proteus that will be blinking when the external interrupt button is
push (Figure 2a) and it will stop blinking when the same button is pressed (Figure 2b). The buzzer will be on for a
few second everytime the button is pushed. Reset the voltage of buzzer to be 1V by double click on it.
(Figure 2a)
(Figure 2b)
QUESTION
1.
List the comparison between Interrupt by using timer technique and polling technique .