0% found this document useful (0 votes)
8 views

Interrupts 1

Uploaded by

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

Interrupts 1

Uploaded by

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

INTERRUPTS AND ON-CHIP ANALOG TO DIGITAL

MODULE III
CONVERTER
Interrupts in PIC 16F877A - INTCON Register - Option Register - Interrupt
Sources - PIE and PIR registers - Enabling Interrupts - Peripheral Interrupts -
Interrupt Service Routine (ISR). - On-Chip Analog-to-Digital Converter (ADC)
block diagram - PIC16F877A ADC pins - ADC Configuration- ADC PORT
configuration - ADC Channel Selection – ADC voltage reference selection -
Resolution - ADC conversion clock source - Interrupt control - ADC result
format - ADC Registers: ADCON0 and ADCON1 - Sample Interrupt Codes in
MikroC.
INTERRUPTS
1. Interrupt means stop the continuous progress of (an
activity or process).
2. It will stop the process which is running currently and
then execute the process in ISR.
3. So it is a signal which may be disturbed or alter the
sequence of execution of the processor.
4. In other words, An interrupt is a signal to the
processor emitted by hardware or software indicating
an event that needs immediate attention.
5. ISR means Interrupt Service Routine. Whenever
an interrupt occurs, the controller completes the
execution of the current instruction and starts the
execution of an Interrupt Service Routine (ISR)
or Interrupt Handler.
Program Execution With Interrupts
In this method, it will execute the main
function. When an interrupt signal comes, it
will stop the main function execution and
suddenly it will go to the ISR function and
executes that. After execution of ISR and again
it will come to the main function.
Types of Interrupts
Interrupts have the highest priority than other signals, basic
type of interrupts are,
1. Hardware Interrupts
2. Software Interrupts

Hardware Interrupts
If the interrupt is coming from hardware or external
devices, it is called Hardware Interrupt. For example:
from the keyboard we will press the key to do some
action this pressing of a key on the keyboard will
generate a signal which is given to the processor to do
action, such interrupts are called hardware interrupts.
Software Interrupts
The interrupts which are caused by the software
instructions are called software instructions.
Interrupt Latency
When an interrupt occurs, the service of the interrupt
by executing the ISR may not start immediately by
context switching.
The time interval between the occurrence of interrupt
and the start of execution of the ISR is called interrupt
latency.
PIC 16F877A has the following 15 interrupt sources :
External
Timer 0 In this tutorial,
Timer 1 we will see
RB Port Change
USART Interrupts,
Parallel Slave Port Read/Write
A/D Converter Timer Interrupts,
USART Receive External Interrupts.
USART Transmit
Synchronous Serial Port
CCP1 (Capture, Compare, PWM)
CCP2 (Capture, Compare, PWM)
TMR2 to PR2 Match
Comparator
EEPROM Write Operation
Bus Collision
Registers Used for Interrupts
INTCON
OPTION_REG
PIE1
PIR1
PIE2
PIR2

INTCON Register
The INTCON register is a readable and writable register,
which contains various enable and flag bits for the
TMR0 register overflow, RB port change and external
RB0/INT pin interrupt.
bit 7:
GIE: The Global Interrupt Enable bit is like the master
switch for all the different interrupts.
SETTING this bit will enable all the interrupts to function,
CLEARING this bit will disable ALL interrupts.
1 = Enables all un-masked interrupts
0 = Disables all interrupts

PIE: Peripheral Interrupt Enable bit


1-Enables all unmasked peripheral interrupts
0-Disables all peripheral interrupts
TMR0IE: TMR0 Overflow Interrupt Enable bit
1-Enables the TMR0 interrupt
0-Disables the TMR0 interrupt
Timer overflow means the timer has reached is limit value.
When a timer overflow interrupt occurs, the timer overflow
bit TOVx will be set in the interrupt flag register TIFRx.

INTE: RB0/INT External Interrupt Enable bit


1 = Enables the RB0/INT external interrupt
0 = Disables the RB0/INT external interrupt

RBIE: RB Port Change Interrupt Enable bit


1 = Enables the RB port change interrupt
0 = Disables the RB port change interrupt
TMR0IF: TMR0 Overflow Interrupt Flag bit
1-TMR0 register has overflowed (must be cleared in
software)
0-TMR0 register did not overflow
INTF: RB0/INT External Interrupt Flag bit
1 = The RB0/INT external interrupt occurred (must be
cleared in software)
0 = The RB0/INT external interrupt did not occur
RBIF: RB Port Change Interrupt Flag bit
1 = At least one of the RB7:RB4 pins changed state; a
mismatch condition will continue to set the bit. Reading
PORTB will end the mismatch condition and allow the bit to
be cleared (must be cleared in software).
0 = None of the RB7:RB4 pins have changed state
OPTION_REG
The OPTION_REG Register is a readable and writable register,
which contains various control bits to configure the TMR0
Prescaler/WDT Postscaler (single assignable register known also
as the Prescaler), the external INT interrupt, TMR0, and the weak
pull-ups on PORTB.
Pull Up Resistors
• Each of PIC16F877A microcontroller PORTB pins
has an internal weak pull-up.
• A single control bit can turn on all the pull-ups.
This is performed by clearing bit RBPU
(OPTION_REG<7>).
• The weak pull-up is automatically turned off when
the port pin is configured as an output.
• The pull-ups are disabled on Power-on Reset
• Here if there is no pull up, when switch is open the
input is in floating state that means it is neither
high nor low. You cannot know the state of the pin.
A pre-scaler is an electronic counting circuit
used to reduce a high frequency electrical
signal to a lower frequency by integer division.
A circuit that slows the rate of the interrupt
generation(or WDT reset)from a counter/timer by
dividing it down.
RBPU: PORTB Pull-up Enable bit (This bit is not used for
timers)
1 = PORTB pull-ups are disabled
0 = PORTB pull-ups are enabled by individual port latch
values
INTEDG Interrupt Edge Select bit
1 = Interrupt on the rising edge of RB0/INT pin
0 = Interrupt on the falling edge of RB0/INT pin

T0CS: TMR0 Clock Source Select bit


1 = Transition on T0CKI pin
0 = Internal instruction cycle clock (CLKO)
T0SE: TMR0 Source Edge Select bit
1 = Increment on high-to-low transition on T0CKI pin
0 = Increment on low-to-high transition on T0CKI pin

PSA: Prescaler Assignment bit


1 = Prescaler is assigned to the WDT
0 = Prescaler is assigned to the Timer0 module

PS2:PS0: Prescaler Rate Select bits


PIE1 Register
The PIE1 register contains the individual enable bits for
the peripheral interrupts.

PSPIE: Parallel Slave Port Read/Write Interrupt Enable


bit(1)
1 = Enables the PSP read/write interrupt
0 = Disables the PSP read/write interrupt
Note (1): PSPIE is reserved on PIC16F873A/876A devices; always
maintain this bit clear.
ADIE: A/D Converter Interrupt Enable bit
1 = Enables the A/D converter interrupt
0 = Disables the A/D converter interrupt
RCIE: USART Receive Interrupt Enable bit
1 = Enables the USART to receive interrupt
0 = Disables the USART receive interrupt
TXIE: USART Transmit Interrupt Enable bit
1 = Enables the USART to transmit interrupt
0 = Disables the USART transmit interrupt
SSPIE: Synchronous Serial Port Interrupt Enable bit
1 = Enables the SSP interrupt
0 = Disables the SSP interrupt
CCP1IE: CCP1 Interrupt Enable bit
1 = Enables the CCP1 interrupt
0 = Disables the CCP1 interrupt
TMR2IE: TMR2 to PR2 Match Interrupt Enable bit
1 = Enables the TMR2 to PR2 match interrupt
0 = Disables the TMR2 to PR2 match interrupt

TMR1IE: TMR1 Overflow Interrupt Enable bit


1 = Enables the TMR1 overflow interrupt
0 = Disables the TMR1 overflow interrupt
PIR1 Register
The PIR1 register contains the individual flag bits for the
peripheral interrupts.
Note: Interrupt flag bits are set when an interrupt condition
occurs regardless of the state of its corresponding enable bit or
the global enable bit, GIE (INTCON<7>). User software should
ensure the appropriate interrupt bits are clear prior to enabling
an interrupt.

PSPIF: Parallel Slave Port Read/Write Interrupt Flag bit(1)


1 = A read or a write operation has taken place (must be cleared in
software)
0 = No read or write has occurred
Note (1): PSPIF is reserved on PIC16F873A/876A devices; always
maintain this bit clear.
ADIF: A/D Converter Interrupt Flag bit
1 = An A/D conversion completed
0 = The A/D conversion is not complete
RCIF: USART Receive Interrupt Flag bit
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

SSPIF: Synchronous Serial Port (SSP) Interrupt Flag bit


CCP1IF: CCP1 Interrupt Flag bit
TMR2IF: TMR2 to PR2 Match Interrupt Flag bit
1 = TMR2 to PR2 match occurred (must be cleared in software)
0 = No TMR2 to PR2 match occurred
TMR1IF: TMR1 Overflow Interrupt Flag bit
1 = TMR1 register overflowed (must be cleared in software)
0 = TMR1 register did not overflow

PIE2 Register
The PIE2 register contains the individual enable bits for the CCP2
peripheral interrupt, the SSP bus collision interrupts, EEPROM
writes operation interrupt, and the comparator 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
PIR2 Register
The PIR2 register contains the flag bits for the CCP2
interrupt, the SSP bus collision interrupt, EEPROM write
operation interrupt and the comparator interrupt.

CMIF: Comparator Interrupt Flag bit


1 = The comparator input has changed (must be cleared in
software)
0 = The comparator input has not changed
EEIF: EEPROM Write Operation Interrupt Flag bit
1 = The write operation completed (must be cleared in software)
0 = The write operation is not complete or has not been started

BCLIF: Bus Collision Interrupt Flag bit


1 = A bus collision has occurred in the SSP when configured for
I2C Master mode
0 = No bus collision has occurred

CCP2IF: CCP2 Interrupt Flag bit


• In Microcontrollers there are two main types
of interrupts.
• They are External Interrupt and Internal
Interrupt.
• The internal Interrupts occur inside the
Microntroller for performing a task, for
example Timer Interrupts, ADC Interrupts
etc..
• These interrupts are triggered by the software
to complete the Timer operation or ADC
operation respectively.
• The external interrupt is the one that can get
triggered by the user.
• In this program we will learn how to use an
External interrupt by using a push button to
trigger an interrupt.
• We will use an LCD to display numbers
incrementing from 0 to 1000 and when an
interrupt is triggered we should notify about it
from the Interrupt Service Routine ISR and
then continue back to incrementing the
numbers.
• We have enabled internal pull up resistors on
PORT B, hence we can directly connect the
RB0 pin to ground via a Push button.
• So whenever this pin gets LOW an interrupt
will be triggered
• Like all programs we have to begin the code by
defining the pin configuration for the pins that
we use in our program.
• Also here we need to define that we are using
RB0/INT as an external interrupt pin and not
as a input or output pin.
• The below line of code enables the internal
pull-up resistor on portB by making the 7th bit
as 0.
• Then we enable the Global/Peripheral
interrupts and declare that we are using RB0
as an external interrupt pin.
• Once the RB0 pin is defined as an external
interrupt pin, each time it gets low the
external interrupt flag INTF will become 1 and
the code inside the void interrupt function will
get executed since the Interrupt Service
Routine(ISR) will be called.
• It is very important to clear the interrupt
flag once you are done with the routine. Only
then the program will return back to void
main function. This clearing has to be done by
software using the line
• inside the main function, we just increment a
number for every 500 ms and display it on the
LCD screen.
• We do not have any specific line to check the
status of the RB0 pin
• . The interrupt will always stay active and
whenever the push button is pressed it will
jump out of void main and execute the lines in
the ISR.
• Working of PIC16F877A Interrupts:
• This program given is a very basic example of
external interrupt where it just changes the
display of the LCD screen when an interrupt is
detect.
External Interrupt – PIC16F877A

• LED – RD0
Switch – RB0
Whenever anyone of the RB4, RB5, RB6, RB7 state
changes, Interrupt will happen. In ISR, toggling the LED
in PORTD.
A push button switch is connected to the External
Interrupt pin INT of the PIC Microcontroller.
When this button is pressed, the microcontroller is
interrupted and the ISR is executed.
The ISR toggles the status of PORTC for 1 second.

You might also like