PW 4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

ELECTRICAL ENGINEERING DEPARTMENT

ACADEMIC SESSION: SESSION 2 2022/2023


DEC40053 – EMBEDDED SYSTEM APPLICATIONS

PRACTICAL WORK 4 : INTERRUPT PROGRAMMING IN PIC


PRACTICAL WORK
DATE :
LECTURER’S NAME:
GROUP NO. :
TOTAL
STUDENT ID : STUDENT NAME : MARKS
(100%)

(1)

(2)

(3)

SUBMIT DATE: RETURN DATE:


DEC40053 – EMBEDDED SYSTEM APPLICATIONS
PRACTICAL WORK 1 / 2 / 3 / 4 / 5 / 6

NO REG. NO. NAME OF TOTAL MARK


STUDENT
1 /100

2 /100

3 /100
PRACTICAL SKILL ASSESSMENT RUBRIC - CLO 3:
Construct and simulate real-time embedded system application based on PIC16F/PIC18F microcontroller effectively. (P4,
PLO5)
SCORE weight Score
NO CRITERIA
1 2 3 4 5 S1 S2 S3
Program code The program The program code The program The program
is incomplete code is is complete with a code is complete code is
with complete with little inconsistence with a little complete
inconsistent some code typing and inconsistent code and well
code typing inconsistence well organized typing and well organized.
1. Write
and not well code typing and after being organized with a X4
program organized well organized assisted by minimum assist
after being after being lecturer by lecturer
assisted by assisted by
lecturer lecturer
The program The program The program code The program The program
code still have code have three have three or code have one code
an error. or more error. more error. or two error. successfully
Compile and Student Student can Student can fixed Student can run
2. execute cannot fixed
the error after
fixed the error
after being
the error with a
minimum assist by
fixed the error
with a minimum
X4
program being assisted assisted by lecturer assist by lecturer
by lecturer. lecturer.

Student The circuit The circuit Can construct a Can


cannot fixed connection have connection have hardware circuit construct a
the circuit three or more three or more one or two error. hardware
Construct connection by error. Student error. Student can or circuit.
3. circuit using itself after can fixed the fixed the error Can produce a or X4
hardware or being assisted error after being with a minimum schematic in Can produce
software by lecturer. assisted by assist by lecturer Proteus one or neat
lecturer. two error. schematic in
(Proteus) Proteus
Unsuccessful Successful Successful Successful Successful
program with program and program and program and program and
completely manage to get manage to get manage to get manage to get
incorrect the correct the correct output on the correct output the correct
output on output on hardware or Proteus on hardware or output on
hardware or hardware or with a minimum Proteus. Student hardware or
Demonstrate assist by lecturer. understand most
Proteus. Proteus after Proteus.
circuit using Student understand of the program and
4. hardware or
Student being assist by most can demonstrate the Student X4
having lecturer. Student understand
software of the program circuit.
difficulties having and can demonstrate the program
(Proteus) to difficulties to the circuit. and can
understand understand the demonstrate
the program. Program the circuit.
demonstrate the
circuit.

TOTAL
/80 /80 /80
MARKS
No Activity Observation Marks
Task Result
1  Program /5
 Circuit /5
2 Discussion /5
3 Conclusion /5
Total /20
1 LEARNING OUTCOMES (LO):

1. : Construct and simulate real time embedded system application


based on PIC16F/PIC18F microcontroller effectively .( CLO3 - P4,
PLO5 - DK6, DP1, DP3 & DP5 )

2 OBJECTIVE

At the end of the practical session, student should be able to:


i. Write C program to enable interrupt in PIC18F
ii. Modify a program based on the given problem.

3 THEORY

External Interrupts INT0, INT1 and INT2

The PIC18 has three external hardware interrupts. Pins RB0, RB1, and RB2,
designated as INT0, INT1, and INT2 respectively, are used as external
hardware interrupts. See Figure 4-1. Upon activation of these pins, the PIC18
gets interrupted in whatever it is doing and jumps to the interrupt vector to
perform the Interrupt Service Routine. All three hardware interrupts are direct
to vector table location at 0008H memory.

Figure 4-1: External Hardware Interrupt Pins

These interrupts are edge triggered interrupts i.e. triggered by either rising edge
or by falling edge. The edge trigger bit is present in an INTCON2 register.

INTCON2: Interrupt Control Register

INTEDG0: External Interrupt 0 Edge select bit


1= Interrupt on Rising Edge
0= Interrupt on Falling Edge

INTEDG1: External Interrupt 1 Edge select bit


1= Interrupt on Rising Edge
0= Interrupt on Falling Edge

INTEDG2: External Interrupt 2 Edge select bit


1= Interrupt on Rising Edge
0= Interrupt on Falling Edge

INTCON

GIE/GIEH: Global Interrupt Enable bit


When IPEN is disabled, GIE enables all interrupts that sets
When IPEN is enabled, GIEH enables all high priority interrupts that sets
PEIE/GIEL: Peripheral Interrupt Enable bit
When IPEN is disabled, PEIE enables all peripheral interrupts that sets
When IPEN is enabled, GIEL enables all low priority interrupts
INT0IE: INT0 External Interrupt Enable bit. Enables the INT0 external interrupt that sets
INT0IF: INT0 External Interrupt Flag bit. Sets when INT0 external interrupt occu r
Figure 4-2: Programming External Interrupt

TMR0 Interrupt

In 8-bit mode (which is the default), an overflow in the TMR0 register (FFh -->
00h) will set flag bit, TMR0IF. In 16-bit mode, an overflow in the
TMR0H:TMR0L register pair (FFFFh ---> 0000h) will set TMR0IF. The interrupt can
be enabled/disabled by setting/clearing enable bit, TMR0IE of the INTCON register.
Interrupt priority for Timer0 is determined by the value contained in the interrupt
priority bit, TMR0IP of the INTCON2 register.

Figure 4-3: Programming Timer Interrupt

4 EQUIPMENT / TOOLS
1. PC installed software:
a. MPLAB IDE integrates with XC8 compiler.
b. PROTEUS 8 Professional
5 SAFETY OPERATING PROCEDURE (SOP)
This is a partial list of basic safety precautions to use when working on a
computer:
 Keep food and drinks out of your workspace.
 Keep your workspace clean and free of clutter.
 Remove your watch and jewelry and secure loose clothing.
 Make sure the computer equipment is plug in and turn ON the power
switch.
 Never unplug a power supply while the equipment working.
 Do not touch areas at hot or that use a high voltage.
 Know where the fire extinguisher is located and how to use it.
 Bend your knees when lifting heavy objects to avoid injuring your back.
 Follow electrical safety guidelines to prevent electrical fires, injuries, and
fatalities in the workplace. Power supplies and monitors contain a high
voltage.

CAUTION
Do not repairing power supplies or monitors if broken. Only experienced
technicians should attempt to repair power supplies and monitors.
6.0 PROCEDURE
6.1 PART A : External Interrupt
To begin, create a subfolder named PW4 on your DEC40053 folder. Create a
new PROTEUS project named PW4A and save it in the PW4 folder. Select
PIC18F4550 and MPLAB XC8 compiler in Firmware.

Circuit : Schematic diagram

6.2 Write the given program code below into your source code file and then click on
build project button until successful. If any error occurs, fix it. Show your
programs that have successfully build to your lecturer.
#include <xc.h>
#define _XTAL_FREQ 4000000

void main(void)
{
TRISB=0b00000011; // Set RB0 & RB1 as Input Pin
ADCON1=0x0E; //Set analog port to digital port
INT0IF = 0; //Reset the External Interrupt Flag
INTEDG0 = 1; //Interrupt on the Rising Edge
INT0IE = 1; //Enable the External Interrupt
GIE = 1; //Set the Global Interrupt Enable

while (1){
LB6=1;
__delay_ms(500);
LB6=0;
__delay_ms(500);
}
}
void interrupt isr()
{
INT0IF = 0; //reset the interrupt flag
LB7 = ~LB7; //flip the bit
}
Code1: Coding for External interrupt INT0

RUN Simulation, Press and Release push button SW1. Observe the output.
Modify code 1 above, change interrupt source from INT0 to INT1 and maintain
the others operation. than build the program until successful. If any error occurs,
fix it. Press and Release push button SW2. Show your result to your lecture.

6.3 PART B: TIMER0 INTERRUPT

Save As your project with PW4B file name. Modified the schematic diagram
shown below.

Circuit : Schematic Diagram


6.4
Refer to T0CON Register above, complete the given program below.
Rewrite this code into your source file and then build the program until
successful. If any error occurs, fix it.
 Setting Timer0 are:
 Timer0 is ON;
 8-bit Mode;
 Transition on T0CKI pin;
 Increment on low-to-high transition on T0CKI pin;
 Timer0 clock input bypasses prescaler.

Bit-7 Bit-6 Bit-5 Bit-4 Bit-3 Bit-2 Bit-1 Bit-0

T0CON = 0 1 1 0 1 0 0 0
6.5
Show your programs that have successfully build to your lecturer. RUN
simulation code 2, Press and Release SW1 four (4) time.

#include <xc.h>
#define _XTAL_FREQ 4000000

void main(void) {
TRISA4 = ; //Set RA4 as input pin
TRISB = ; //Set PORTB as Output
ADCON1 = 0x ; //Set Analog Port to Digital Port
T0CON = 0b ; //Value T0CON in Binary
TMR0L = 252;
GIE = ; //Set the Global Interrupt Enable
TMR0IE = ; //Enable external interrupt
TMR0IP = ; //Enable interrupt priority bit
TMR0IF = ; //Reset the external interrupt flag
while (1) {
LB6=1;
__delay_ms(500);
LB6=0;
__delay_ms(500);
}
}

void interrupt isr() {


LB7 ^=1; //flip the bit
__delay_ms(100);
TMR0L = 252;
TMR0IF = 0; //reset the interrupt flag
}

Code 2: Event Counter Interrupt

6.6 Modify code 2 above, change the counting numbers from FOUR (4) to SEVEN (7)
and maintain the others operation. RUN Simulation, Press and Release push
button SW1 SEVEN (7) times. Show your result to your lecture.

7 TASK

TASK 1

A security door is attached to a SENSOR switch (active LOW) which is


connected to RB2/INT2 and a OUTPUT buzzer (active HIGH) is connected to
RD7. While door close LED Green ON (RD0) and when door open LED Red ON
(RD1). Design a control system, so that every time the door is opened, the
buzzer beeps twice. Apply external hardware interrupt programming for this
system. Set the interrupt to be triggered on rising edge.
TASK 2

ABC Company Sdn. Bhd has assigned by Majlis Perbandaran Kota Bharu to
install a traffic lights systems at the busiest traffic Jalan Kuala Krai - Kota Bharu.
As a technical support, you are responsible to design the system for pedestrian
to cross the road. The specifications are given by the engineer as below:
(a) Use PIC18F4550 Microcontroller, an active LOW push button, an active
HIGH LED for vehicles (red, yellow and green) and active HIGH LED for
pedestrians (green and red).
(b) Push button must be connected to RA4 while LED are connected to
PORTD.
(c) To cross the road, pedestrian has to press the push button that is
programmed as timer interrupt. The systems will function as normal after
the interrupt.

MAIN ROAD

U1
R1 2
RA0/AN0 RC0/T1OSO/T1CKI
15
10k 3 16
RA1/AN1 RC1/T1OSI/CCP2/UOE
4 17
RA2/AN2/VREF-/CVREF RC2/CCP1/P1A
5 23
RA3/AN3/VREF+ RC4/D-/VM
6 24
RA4/T0CKI/C1OUT/RCV RC5/D+/VP
7 25
RA5/AN4/SS/LVDIN/C2OUT RC6/TX/CK
14 26
RA6/OSC2/CLKO RC7/RX/DT/SDO
13
OSC1/CLKI
SWITCH 33 19
RB0/AN12/INT0/FLT0/SDI/SDA RD0/SPP0
34 20
RB1/AN10/INT1/SCK/SCL RD1/SPP1
35 21
RB2/AN8/INT2/VMO RD2/SPP2
36 22
RB3/AN9/CCP2/VPO RD3/SPP3
37 27
RB4/AN11/KBI0/CSSPP RD4/SPP4
38 28
RB5/KBI1/PGM RD5/SPP5/P1B
39 29
RB6/KBI2/PGC RD6/SPP6/P1C
40 30
RB7/KBI3/PGD RD7/SPP7/P1D
8 ZEBRA CROSSING
RE0/AN5/CK1SPP
9
RE1/AN6/CK2SPP
10
RE2/AN7/OESPP
18 1
VUSB RE3/MCLR/VPP
PIC18F4550

VDD

R4
10k

TASK RESULT
8
9 DISCUSSION

10 CONCLUSION

You might also like