0% found this document useful (0 votes)
25 views48 pages

Unit 3

Processor architecture Unit 1

Uploaded by

Kiran Dahake
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views48 pages

Unit 3

Processor architecture Unit 1

Uploaded by

Kiran Dahake
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

PIC Interrupts &

Interfacing-I
Interrupt Vs Polling

• There are 2 methods for communicating between the


microcontroller and the external system:
1. Polling
2. Interrupt
What is Interrupt:

► A signal to the CPU to take an immediate action is called an interrupt.


Thus, interrupt is a process with the help of which the CPU is notified
of requiring attention. The interrupt is considered as a hardware
mechanism. The interrupt requires the operating system to stop and
figure out what to do next.
What is Polling :-
► The process in which the CPU constantly checks the status of the
device- to see if it needs the CPU's attention is called polling. It is
basically a protocol in which the CPU services the I/O devices.
Thus, the polling is the process of periodically checking status of
a device to see if it is time for the next I/O operation.
Interrupt Vs Polling
Interrupt Polling
An interrupt is a process with the help of which the Polling is the process in which the CPU constantly
CPU is notified of requiring attention. checks the status of a device to see if it needs the
CPU's attention.
It is considered as a hardware mechanism. It is a protocol.
An interrupt handler services/works with the device. In this protocol, the CPU services the device.
Interrupt-request line indicates that the device needs The command-ready bit indicates that the device
to be serviced. needs to be serviced.
An interrupt handler services/works with the device. In this protocol, the CPU services the device.
Interrupt-request line indicates that the device needs The command-ready bit indicates that the device
to be serviced. needs to be serviced.
CPU is used only when a device requires servicing. CPU is used only when a device requires servicing.
Interrupts save the CPU cycles. Polling wastes many of the CPU cycles.
Interrupts can occur at any point in time. CPU polls the devices at regular intervals of time.

It becomes inefficient if devices frequently interrupt Polling becomes inefficient when the CPU rarely
the CPU. finds a device that is ready to be serviced.
Steps in executing an interrupt

• Upon activation of interrupt the microcontroller


• Finishes executing the current instruction
• Store next instruction address in PC & current status in stack memory.
• Processor Jumps to the interrupt vector table(IVT) to get the address of
ISR and jumps to it
• Transfer program control to the ISR
• Begin executing the ISR instructions to the last instruction of ISR (RETFIF)
• Executes RETFIE
• Pops the PC from the stack
• Starts to execute from the address of that PC

11-9
Classification of Interrupts

• Maskable Interrupts
• Nonmaskable Interrupts
• Vectored Interrupts
• Non-vectored
Interrupts
Note: Non Maskable : cannot be disabled or ignored by the instructions of CPU
Maskable : disabled or ignored by the instructions of CPU
Sources of Interrupts:

► External hardware interrupts : INT0, INT1 AND INT2


► Timer Interrupts : TMR0IF, TMR1IF, TMR2IF AND TMR3IF
► Serial communication interrupts : TXIF and RCIF
► RB port change interrupt : RBIF
► A/D converter interrupt : ADIF
► Compare, Capture, PWM interrupt : CCP1IF
Simplified interrupt structure of PIC 18
Enabling and Disabling Interrupts
GIE PEIE TMR0IE INT0IE RBIE

Bit 7 GIE Global Interrupt Enable bit


1 = Enables all unmasked interrupts
0 = Disables all interrupt
Bit 6 PEIE Peripheral Interrupt Enable bit
1 = Enable all unmasked peripheral interrupts
0 = Disable all peripheral interrupts
Bit 5 TMR0IE TMR0 Overflow Interrupt Enable bit
1 = Enables the TMR0 overflow interrupt
0 = Disables the INT0 overflow interrupt
Bit 4 INT0IE INT0 External Interrupt Enable bit
1 = Enables the INT0 external interrupt
0 = Disables the INT0 external interrupt
Bit 3 RBIE RB port change Interrupt Enable bit
1 = Enables the RB port change interrupt
0 = Disables the RB port change interrupt
Interrupt Registers

► For PIC18F458 microcontroller, 13 registers are used to control


interrupt operation. These registers are :
1. RCON
2. INTCON
3. INTCON2
4. INTCON3
5. PIR1, PIR2, PIR3
6. PIE1, PIE2, PIE3
7. IPR1, IPR2, IPR3
RCON Register : Reset control register
IPEN SBOREN - R1 TO PD POR BOR

IPEN INTERRUPT PRIORITY ENABLE BIT


1= ENABLE PRIORITY LEVELS ON INTERRUPT
0= DISABLE PRIORITY LEVELS ON INTERRUPT

SBOREN BOR SOFTWARE ENABLE BIT

R1 RESET INSTRUCTION FLAG BIT

TO WATCHDOG TIME OUT FLAG BIT

PD POWER DOWN DETECTION FLAG BIT

POR POWER ON RESET FLAG BIT

BOR BROWN OUT RESET STATUS BIT


INTCON REGISTERS

► THE INTCON registers are readable and writable registers that


contains various enable, priority and flag bits. Because of the
number of interrupts to be controlled, pic18F devices have three
INTCON registers.
► 1. INTCON registers
► 2. INTCON2 registers
► 3. INTCON3 registers
INTCON register:-
GIE/GIEH PEIE/GIEL TMR0IE INT0IE RBIE TMR0IF INT0IF RBIF

GIE/GIEH When IPEN=0, 1=enables all unmasked interrupts 0=disable


When IPEN=1, 1=enable all high priority interrupts 0=disable

PEIE/GIEL When IPEN=0, 1=enables all unmasked peripheral interrupts 0=disable


When IPEN=1, 1=enable all low priority peripheral interrupts 0=disable

TMR0IE 1=enable the TMR0 overflow interrupt 0=disable

INT0IE 1=enables the INT0 external interrupt 0=disable


RBIE 1=enables the RB port change interrupt 0=disable
TMR0IF 1=The timer 0 register has overflowed 0=-not overflow
INT0IF 1=The INT0 external interrupt occurred 0=disable
RBIF 1=At least one of the RB7 : RB4 pins changed state
0=None of the pins changed
INTCON2 REGISTER:
RBPU INTEDG0 INTEDG1 --- --- TMROIP ----- RBIP
INTCON3 REGISTER:
INT2IP INT1IP ---- INT2IE INT1IE ---- INT2IF INT1IF
INTCON GIE/GIEH PEIE/GIEL TMR0IE INT0IE RBIE TMR0IF INT0IF RBIF

INTCON2 RBPU INTEDG0 INTEDG1 --- --- TMROIP ----- RBIP

INTCON3
INT2IP INT1IP ---- INT2IE INT1IE ---- INT2IF INT1IF
Show the instructions to

•A. Enable (unmask) the Timer 0 interrupt and


external hardware interrupt 0 (INT0)
BSF INTCON, TMR0IE; enable Timer 0
Interrupt BSF INTCON, INT0IE ; enable
external interrupt BSF INTCON, GIE; allow
interrupts to come in
Show the instructions to

B. Disable (mask) the Timer 0 interrupt, then


BCF INTCON, TMR0IE ; mask (disable) Timer 0 Interrupt

C. Show how to disable (mask) all the


interrupts with a single instruction.
BCF INTCON,GIE ;mask all interrupts globally
PIR registers:
► The peripheral Interrupt Request (PIR) register contains the individual flag bits for the
peripheral interrupts.
► Due to the number of peripheral interrupt sources, there are three peripheral interrupt
request registers (PIR1, PIR2, PIR3)
PIR1 (Peripheral Interrupt Request Register 1)

PSPIF: Parallel Slave Port Read/Write Interrupt SSPIF: Synchronous Serial Port (SSP) Interrupt Flag bit
Flag bit(1) CCP1IF: CCP1 Interrupt Flag bit
1 = A read or a write operation has taken place TMR2IF: TMR2 to PR2 Match Interrupt Flag bit
0 = No read or write has occurred
1 = TMR2 to PR2 match occurred
ADIF: A/D Converter Interrupt Flag bit 0 = No TMR2 to PR2 match occurred
1 = An A/D conversion completed TMR1IF: TMR1 Overflow Interrupt Flag bit
0 = The A/D conversion is not complete
1 = TMR1 register overflowed
RCIF: USART Receive Interrupt Flag bit 0 = TMR1 register did not overflow
1 = The USART receive buffer is full
0 = The USART receive buffer is empty
TXIF: USART Transmit Interrupt Flag bit
1 = The USART transmit buffer is empty
0 = The USART transmit buffer is full
PIR2 (Peripheral Interrupt Request Register 2)

CMIF: Comparator Interrupt Flag bit BCLIF: Bus Collision Interrupt Flag bit
1 = The comparator input has changed 1 = A bus collision has occurred
in the SSP when configured for I2C
0 = The comparator input has not changed
Master mode
0 = No bus collision has occurred
EEIF: EEPROM Write Operation Interrupt Flag
bit
CCP2IF: CCP2 Interrupt Flag bit
1 = The write operation completed
0 = The write operation is not complete or
has not been started
PIR3
PIE Register (Peripheral Interrupt
Enable)
► PIE Register (Peripheral Interrupt Enable) contain the individual enable bits for
the peripheral interrupts.
► Due to the number of peripheral interrupt sources, there are three peripheral
Interrupt Enable registers (PIE1, PIE2, PIE3)
► When IPEN is clear, the PEIE bit must be set to enable any of these peripheral
interrupts.
PIE Register 1 (Peripheral Interrupt Enable)

PSPIE: Parallel Slave Port Read/Write TXIE: USART Transmit Interrupt Enable bit
Interrupt Enable bit(1) 1 = Enables the USART to transmit interrupt
1 = Enables the PSP read/write interrupt 0 = Disables the USART transmit interrupt
0 = Disables the PSP read/write interrupt SSPIE: Synchronous Serial Port Interrupt Enable bit
ADIE: A/D Converter Interrupt Enable bit 1 = Enables the SSP interrupt
1 = Enables the A/D converter interrupt 0 = Disables the SSP interrupt
0 = Disables the A/D converter interrupt CCP1IE: CCP1 Interrupt Enable bit
RCIE: USART Receive Interrupt Enable bit 1 = Enables the CCP1 interrupt
1 = Enables the USART to receive interrupt 0 = Disables the CCP1 interrupt
0 = Disables the USART receive interrupt TMR2IE: TMR2 to PR2 Match Interrupt Enable bit
RCIE: USART Receive Interrupt Enable bit 1 = Enables the TMR2 to PR2 match interrupt
1 = Enables the USART to receive interrupt 0 = Disables the TMR2 to PR2 match interrupt
0 = Disables the USART receive interrupt
CMIE: Comparator Interrupt Enable bit
1 = Enables the comparator interrupt
0 = Disable the comparator interrupt

EEIE: EEPROM Write Operation Interrupt Enable bit


1 = Enable EEPROM write interrupt
0 = Disable EEPROM write interrupt

BCLIE: Bus Collision Interrupt Enable bit


1 = Enable bus collision interrupt
0 = Disable bus collision interrupt

CCP2IE: CCP2 Interrupt Enable bit


1 = Enables the CCP2 interrupt
0 = Disables the CCP2 interrupt
PIE Register 3 (Peripheral Interrupt Enable)
IRXIE WAKIE ERRIE TXB2IE TXB1IE TXB0IE RXB1IE RXB0IE
IPR (Interrupt Priority Request)
IPR 1 (Interrupt Priority Request)
IPR 2 (Interrupt Priority Request)
IPR 3(Interrupt Priority Request)
Steps for programming PIC18 timer using
Interrupt:

1. Enable GIE,PEIE,TMRxIE
2. Configure the TxCON register
3. Clear TMRxIF Timer interrupt flag.
4. Load the count in Timer register TMRx.
5. Set TMRxON to start Timer operation.
6. When TMRxIF =1 , code will jump to ISR to execute it, and after
excecution control returns to the main program.
Example: Write a C program for the PIC18 to generate a square wave of 2 KHz frequency
on RB4 using Timer0 ISR and a square wave of 5 KHz frequency on RB5 using TIMER1
ISR. Assume XTAL=10 MHz
Answer : Given : Fosc = 10 MHz , No Prescaler Ftimer =Fosc /4 = 10/4= 2.5 MHz
Period = 1/Timer= 1/ 2.5 MHz = 0.4 µsec.

For 2 KHz ► For 5KHz


T= 1/2KHz = 0.5 µsec. T= 1/5KHz = 0.2 µsec.
Ton = Toff = T/2 = .25 µsec. Ton = Toff = T/2 = .1 µsec.
Count = Desired Delay/ Timer Period Count = Desired Delay/ Timer Period
= .25 µsec./ .4 µsec. = 625 = .1 µsec./ .4 µsec. = 250
The value to be loaded in 16 bit The value to be loaded in 16 bit
Timer0 register is : 65536- period Timer1 register is : 65536-period
Count=65536-625 = 65911 = FD8FH Count=65536-250 = 65286 = FF06H
#include<P18F4550.h> TOCON=0x08; void Timer0_ISR(void)
void Timer0_ISR(void); INTCONbits.TMROIF
void CHK_ISR(void); =0; {
#define PORTBit1 PORTbits.RB4 TMR0H=0xFD; PORTBit1= ~PORTBit1;
#define PORTBit2 PORTbits.RB5 TMR0L=0X8F; TMR0H=0xFD;
#pragma interrupt CHK_ISR INTCONbits.TMR0IE=1;
void CHK_ISR(void) TMR0L=0x8F;
{ T1CON=0x08;
if (INTCONbits.TMR0IF=1) PIR1bits.TMR1IF =0; INTCONbits.TMR0IF=0;
TIMER0_ISR(); TMR1H=0xFF; }
if (PIR1bits.TMR1IF=1) TMR1L=0X06; void Timer1_ISR(void)
TIMER1_ISR(); PIE1bits.TMR1IE=1;
} {
#pragma code TOCONbits.TMR0ON=1 PORTBit2= ~PORTBit2;
Void main(void) ; TMR1H=0xFF;
{ T1CONbits.TMR1ON=1;
TRISBbits.TRISB4=0; While(1); TMR1L=0x06;
TRISBbits.TRISB5=0; } PIRbits.TMR1IF=0;
INTCONbits.GIE=1; }
INTCONbits.PEIE=1;
Interfacing of LED
► Current sinking and sourcing capacities in the PIC18 family is about 25 mA.
► We can drive LED using PORT in 2 ways: 1. Current Sourcing mode 2. Current sinking
mode.
► Source current is the ability of the digital output/input port to supply current. Sink current
is the ability of the port to receive current.

a. LED connected in current source mode b. LED connected in current sink mode
Q. Write an embedded pic18 c program to
flash the LED connected to pin 0 of PORTB
Assume that Ton and Toff of LED = PortBit =0;
500 msec.
DELAY(500);
#include<pic18f4550.h>
}
#define PortBit PORTbits.RB0
}
void DELAY(unsigned int);
Void DELAY (unsigned int t);
void main(void)
{ {

TRISBits.TRISB=0; unsigned int I,j;


While(1); for(i=0; i<t; i++)
{ for(j=0; j<165; j++);
PortBit =1; }
DELAY(500);
Q. Write an embedded PIC18C program to
blink the LED connected to PORTC
Assume that Ton and Toff of LED = PORTC=0xFF;
500 msec.
DELAY(500);
#include<pic18f4550.h> }
void DELAY(unsigned int); }
void main(void) void DELAY (unsigned int t);
{ {
TRISC=0; unsigned int I,j;
While(1); for(i=0; i<t; i++)
{ for(j=0; j<165; j++);
PORTC=0x00; }
DELAY(500);
Q. An LED connected to each pin of PORT D. Write a
PIC18 C program that will turn each LED from pin D0 to
D7. Call a delay module before turning on the next LED
#include<pic18f4550.h> { PD6 = 1;
#define PD0 PORTDbits.RD0 DELAY(500);
PD0 = 1;
PD7 = 1;
#define PD1 PORTDbits.RD1 DELAY(500); DELAY(500);
#define PD2 PORTDbits.RD2 }
PD1 = 1;
}
#define PD3 PORTDbits.RD3
DELAY(500); void DELAY (unsigned int t);
#define PD4 PORTDbits.RD4 {
PD2 = 1;
#define PD5 PORTDbits.RD5 unsigned int I,j;
DELAY(500); for(i=0; i<t; i++)
#define PD6 PORTDbits.RD6
for(j=0; j<165; j++);
PD3= 1;
#define PD7 PORTDbits.RD7
DELAY(500); }
void DELAY(unsigned int);
void main(void) PD4 = 1;

{ DELAY(500);
TRISD=0; PD5 = 1;
While(1); DELAY(500);
Interfacing of LCD:-
RD0 DB0 Vcc
RD1 DB1
RD2 DB2 16 character * 2 line VEE
RD3 DB3
RD4 DB4 LCD display module
RD5 DB5 VSS
RD6 DB6
PIC18XXX RD7 DB7 RS RW E

RE0
RE1
RE2

RS: RS=0 data register is selected


RS=1 Command register is selected
R/W : =0 then write operation
=1 then reading operation
E : E=1 enable
Program:
DELAY(15); LCD(‘E’,1);
#include<PIC18F4550.h>
LCD(0x06,0); //shift cursor right }
#define LCDData PORTD
DELAY(15); void LCD (unsigned char value, unsigned
#define RS PORTBbits.RE0 char flag)
LCD(0x86,0); //line 1 position 6
#define RW PORTBbits.RE1 {
DELAY(15);
#define EN PORTBbits.RE2 LCDdata = value;
LCD(‘W’,1);
void LCD (unsigned char, unsigned char); RS=flag;
DELAY(15);
void DELAY (unsigned int); RW=0;
LCD(‘E’,1);
void main (void)
DELAY(15); EN=1;
{
LCD(‘L’,1); DELAY(1);
TRISD = 0;
DELAY(15); EN=0;
TRISE = 0;
LCD(‘C’,1); }
EN=0;
DELAY(15); Void DELAY(unsigned int cnt)
LCD(0x38,0) //initialize LCD
LCD(‘O’,1); {
DELAY(250);
DELAY(15); Unsigned int I,j;
LCD(0x0E,0); //Display ON
LCD(‘M’,1); For(i=0; i<cnt; i++)
DELAY(15);
DELAY(15); for(j=0;j<165;j++);
LCD(0x01,0); //Clear LCD
}
Interfacing Relay:- #include<PIC18F4550.h>
#define PORTBIT PORTBbits.RB0
void main(void)
{
unsigned int i;
TRISBbits.TRISB0=0;
PORTBIT=1;
DELAY(2000);
PORTBIT=0;
}
void DELAY(unsigned int cnt)
{
PIC 18FXXX unsigned int i,j;
for(i=0; i<cnt; i++)
for(j=0; j<165; j++)
}
Interfacing keyboard (4*4 Matrix) :-
► A 4x4 keypad has a total of 8
connections, where 4 of them are
connected to the column and the
remaining rows of the matrix of
switches. When an individual button
is pressed, a connection is established
between one of the rows and
columns.
The Steps in algorithm are as follows:
1. Initialize RB4-RB7 as inputs i.e write ‘1’ to these pins.
2. Check if all the keys are released by writing ‘0’ to RB3-RB0 and check if all
return lines are in state ‘1’. If NO then wait. If Yes then go to step 3
3. Call debounce.
4. Wait for key closure. Ground all scan lines by writing ‘0’ and then check if at
least one of return lines shows ‘0’ level. Key pressed ?No step 4 Yes step 5
5. Call debounce.
6. Is key really pressed? If NO then step 4 and If YES then step 7
7. Find key code and display the key pressed on 7 segment display.
8. Go to step 1
Interfacing of LCD and Keyboard with PIC18F4550

You might also like