Unit IV

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 56

Unit IV

Real World
Interfacing Part I
I/O port
PORT STRUCTURE OF PIC
TRIS register in outputting data
TRIS register in inputting data
INTERRUPT STRUCTURE OF PIC
Basic Concepts of Interrupts
• An interrupt is a communication process
• A device
• Requests the MPU to stop processing
• Internal or external
• The MPU
• Acknowledges the request
• Attends to the request
• Goes back to processing where it was interrupted

330_10 22
Types of Interrupts

330_10 23
PIC18 Interrupts
• PIC18 Microcontroller family
• Has multiple sources that can send interrupt requests
• Does not have any non-maskable or software interrupts
• All interrupts are maskable hardware
• Has a priority scheme divided into two groups
• High priority and low priority
• Uses many Special Function Registers (SFRs) to implement the interrupt
process

330_10 24
PIC18 Interrupt Sources
• External sources
• Three pins of PORTB
• RB0/INTO, RB1/INT1,and RB2/INT2
• Can be used to connect external interrupting sources
• Keypads or switches
• PORTB Interrupt (RBI)
• Change in logic levels of pins RB4-RB7
• Internal peripheral sources
• Examples
• Timers
• A/D Converter
• Serial I/O

330_10 25
PIC18 Interrupt Sources
• Special Function Registers (SFRs)
• RCON
• Priority Enable
• INTCON
• External interrupt sources
• IPR, PIE, and PIR
• Internal peripheral interrupts
• Valid interrupt
• Interrupt request bit (flag)
• Interrupt enable bit
• Priority bit

330_10 26
Interrupt Priority
• Interrupt priorities
• High-priority interrupt vector 000008H
• Low-priority interrupt vector 000018H
• A high-priority interrupt can interrupt a low-priority
interrupt in progress.
• Interrupt priority enable
• Bit7 (IPEN) in RCON register

RCON

330_10 27
External Interrupts
• INTCON Register

• INTCON2 Register

• INTCON3 Register

330_10 28
Interrupt Service Routine (ISR)
• Similar to a subroutine
• Attends to the request of an interrupting source
• Clears the interrupt flag
• Should save register contents that may be affected by the code in the ISR
• Must be terminated with the instruction RETFIE
• When an interrupt occurs, the MPU:
• Completes the instruction being executed
• Disables global interrupt enable
• Places the return address on the stack

330_10 29
Interrupt Service Routine (ISR)
• High-priority interrupts
• The contents of W, STATUS, and BSR registers are
automatically saved into respective shadow registers.
• Low-priority interrupts
• These registers must be saved as a part of the ISR
• If they are affected

• RETFIE [s] ;Return from interrupt


• RETFIE FAST ;FAST equivalent to s = 1
• If s =1: MPU also retrieves the contents of W, BSR, and
STATUS registers

330_10 30
Internal Interrupts
• PIC18 MCU internal interrupt sources
• Timers
• A/D converter
• Serial I/O
• Each interrupt has three bits
• Interrupt priority bit
• Interrupt enable bit
• Interrupt request bit (flag)
• Interrupt registers
• IPR: Interrupt Priority Register
• PIE: Peripheral Interrupt Enable
• PIR: Peripheral Interrupt Request (Flags)
330_10 31
Interrupt Registers
• IPR1

• PIE1

• PIR1

330_10 32
Multiple Interrupt Sources
• All interrupt requests are directed to one of two memory locations
(interrupt vectors)
• 000008H (high-priority)
• 000018H (low-priority)
• When there are multiple requests
• The interrupt source must be identified by checking the interrupt flags

330_10 33
TIMERS IN PIC18
• A prescaler is an electronic counting circuit used to reduce a high
frequency electrical signal to a lower frequency by integer division.
• The prescaler takes the basic timer clock frequency (which may be the
CPU clock frequency or may be some higher or lower frequency) and
divides it by some value before feeding it to the timer, according to
how the prescaler register(s) are configured.
• The prescaler values that may be configured might be limited to a few
fixed values (powers of 2), or they may be any integer value from 1 to
2^P, where P is the number of prescaler bits.
Prescaling of PIC timer
Interfacing of LED
Steps (Algorithm)
• Initialize Port B as output.
• Initialize Pin RC0 & RC1 as input. (where switch is connected)
• Send data as 00 to LEDs. (initially all the LEDs will be off)
• Continuously monitor for the key press.
• If key is pressed toggle the LEDs & call delay.
#include <p18f4550.h>
/*The following lines of code perform interrupt vector relocation to work with the USB bootloader. These must be used with every
application program to run as a USB application.*/
extern void _startup (void);
#pragma code _RESET_INTERRUPT_VECTOR = 0x1000
void _reset (void)
{
_asm goto _startup _endasm
}
#pragma code
#pragma code _HIGH_INTERRUPT_VECTOR = 0x1008
void high_ISR (void)
{
}
#pragma code
#pragma code _LOW_INTERRUPT_VECTOR = 0x1018
void low_ISR (void)
{
}
#pragma code
/*End of interrupt vector relocation*/
void MsDelay (unsigned int time)
{
unsigned int i, j;
for (i = 0; i < time; i++)
for (j = 0; j < 700; j++);/*Calibrated for a 1 ms delay in MPLAB*/
}
#define lrbit PORTBbits.RB0
#define rlbit PORTBbits.RB1
#define buzzer PORTCbits.RC0
#define relay PORTCbits.RC1

void main()
{
unsigned char val=0;
unsigned int k;
INTCON2bits.RBPU=0;
ADCON1 = 0x0F;

TRISBbits.TRISB0=1;
TRISBbits.TRISB1=1;
TRISCbits.TRISC0=0;
TRISCbits.TRISC1=0;

TRISB = 0x03;
PORTB = 0x10;
buzzer = 0;

relay = 0;

while (1)
{
if (!(lrbit))
val = 1;
if (!(rlbit))
val = 0;

if (val)
{
buzzer = 1;
relay = 1;
LATB = LATB >>1;
MsDelay(250);
if (LATB == 0x10 |LATB == 0x00)
{
LATB = 0x80;
MsDelay(250);
}
}
else
{
buzzer = 0;
relay = 0;
LATB = LATB <<1;
MsDelay(250);
if (LATB == 0x80 |LATB == 0x00)
{
LATB = 0x10;
MsDelay(250);
}
}
}
}
Interfacing of LCD with PIC 18F
Interfacing of keypad with PIC
18F
References
• Book : PIC microcontroller & embedded systems- mazidi

• Port structure of PIC 18 (refer chapter 4)


• Interrupt structure of PIC 18 (refer chapter 11 )
• Interfacing of LEDs (chapter 17) + lab 1 program
• Interfacing of LCDs & Keyboard (chapter 12) + lab 2 & lab 3 program

You might also like