Anda di halaman 1dari 4

POLYTECHNIC of TUANKU SULTANAH BAHIYAH

ELECTRICAL ENGINEERING DEPARTMENT


DEC5052 EMBEDDED SYSTEM APPLICATION

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

Example of sources of Interrupts in PIC18


External interrupts Namely INTx (like INT0, INT1 etc), they provide a means for
external hardware to generate interrupts. Like if connect a touchscreen controller to the
PIC MCU. Then the touchscreens PENINT (pen interrupt) can be connected to INT0 (or
any other INTx). Then whenever the pen (or stylus) touches the screen it will interrupt
the CPU.
TIMER interrupts They are also very common in MCUs. In most simple situation they
can act like alarm clocks that can interrupt the CPU at predefined intervals.
Analog to Digital Converter Interrupts A/D Converter takes some time to complete its
operation. So the CPU can either wait for it to complete or set up an AD conversion
complete interrupt. In the latter case CPU can do other tasks while A/D converter
converts the input. As soon as A/D converter completes its job it will inform CPU to read
the value from its buffer.
Data Interrupts MCUs have many different types of data i/o engines, like USART, SPI,
I2C, Parallel etc. They can also interrupt the CPU when data transmission is complete or
data arrives from external source. The example of application such as RFID reader send a
packet because a card near the reader or the GSM module detected an incoming call.
How interrupts are managed?
In general each interrupt source have following related bits:
Enable Bit The are suffixed with IE (Interrupt Enable) example TMR0IE stands for
TIMER0 Interrupt Enable. It can be used to enable/disable the related interrupt. When
set to 1 it enables the interrupt.

POLYTECHNIC of TUANKU SULTANAH BAHIYAH


ELECTRICAL ENGINEERING DEPARTMENT
DEC5052 EMBEDDED SYSTEM APPLICATION

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.

Observe the pattern of output.


/*Example to demonstrate the use of PIC Timers 8 Bit Mode. Clock Source from Prescaler.Prescaler = FCPU/256
(Note: FCPU= Fosc/4). Over flow INT enabled. As the FCPU=20MHz/4 (Running from 20MHz XTAL)=5MHz.
Time Period = 0.2us. Prescaller Period = 0.2 x 256 = 51.2uS, Overflow Period = 51.2 x 256 = 13107.2 uS=
0.0131072 sec. So, need 1/0.0131072 Over Flows to count for 1 sec= 76.2939 Overflows. A counter need to keep
track of overflows. When an over flow occurs, the PIC jumps to ISR to increment counter and when counter
becomes 76,toggle RB1 pin. This pin is connected to LED. Therefore, have a LED which is ON for 1 sec and Off
for 1sec.*/
//Reading header file
#include <p18f45k22.h>
#define RB1
LATBbits.LATB0
unsigned char counter=0;
//Function proto-type declaration;
void main()
{
TRISB= 0b11111110;
//Setup Timer0
T0PS0=1;
T0PS1=1;
T0PS2=1;
PSA=0;
T0CS=0;
T08BIT=1;
TMR0IE=1;
PEIE=1;
GIE=1;
TMR0ON=1;

POLYTECHNIC of TUANKU SULTANAH BAHIYAH


ELECTRICAL ENGINEERING DEPARTMENT
DEC5052 EMBEDDED SYSTEM APPLICATION

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)

POLYTECHNIC of TUANKU SULTANAH BAHIYAH


ELECTRICAL ENGINEERING DEPARTMENT
DEC5052 EMBEDDED SYSTEM APPLICATION

(Figure 2b)

QUESTION
1.

List the comparison between Interrupt by using timer technique and polling technique .

DICUSSION AND CONCLUSION


1.
2.

Write your discussion.


Write your conclusion.

*Submit the group report within 7 days.

Anda mungkin juga menyukai