Professional Documents
Culture Documents
Unit3 Notes
Unit3 Notes
Interrupt Vs Polling:
In the context of PIC microcontrollers, both interrupts and polling are methods
used to handle external events:
Polling in PIC: Polling in PIC involves the processor continuously checking a device
or condition to see if it needs attention. This method is simpler but less efficient
than interrupts because it wastes CPU cycles by constantly monitoring devices.
IVT:
The Interrupt Vector Table (IVT) in PIC microcontrollers is a table that holds the
addresses of interrupt service routines (ISRs) for various interrupt sources. When
an interrupt occurs, the processor looks up the corresponding address in the IVT
to jump to the appropriate ISR to handle the interrupt. This allows for efficient
interrupt handling in PIC microcontroller systems.
* Set up the PIC: Initialize the PIC by configuring its registers to recognize the
interrupt sources and assign them priorities.
* Configure interrupt sources: Enable the interrupt sources that you want to
trigger the interrupt.
* Enable interrupts: Enable interrupts globally by setting the appropriate bits
in the PIC and CPU registers.
* Define interrupt service routines: Write the Interrupt Service Routines
(ISRs) to handle the interrupt requests. These routines should execute the
necessary actions when the interrupt occurs.
* Trigger the interrupt: The interrupt source will generate the interrupt
request. When the interrupt occurs, the PIC will handle the interrupt by
invoking the corresponding ISR.
* Handle the interrupt: The ISR associated with the interrupt source will be
executed, performing the required tasks and then returning control to the
main program.
The steps to enable and disable interrupts in PIC microcontrollers are as follows:
Enabling Interrupts:
Set the Global Interrupt Enable (GIE) bit in the STATUS register by executing the
following instruction: assembly BSF INTCON,GIE
Enable specific interrupt sources by setting their corresponding enable bits in the
INTCON register. For example, to enable external interrupts (INT0 and INT1), you
can set the INT0IE and INT1IE bits in the INTCON register.
Disabling Interrupts:
Clear the Global Interrupt Enable (GIE) bit in the STATUS register by executing the
following instruction: assembly BCF INTCON,GIE
Interrupt registers:
This register contains various bits related to interrupt control, such as enabling
specific interrupt sources, clearing interrupt flags, and controlling interrupt
priorities.
These registers are used to enable or disable individual peripheral interrupts. Each
bit in these registers corresponds to a specific peripheral interrupt source.
These registers hold the status of pending interrupts for each peripheral. When
an interrupt occurs, the corresponding bit in these registers is set.
These registers are used to set the priority levels of different interrupts. Lower
priority interrupts can be made to wait until higher priority interrupts are
serviced.
This register includes the IPEN bit, which enables or disables interrupt priority
levels. When IPEN is set, interrupt priority levels are active. The specific registers
and their functionalities may vary depending on the PIC microcontroller model.
It's crucial to refer to the datasheet and reference manual of the specific PIC
microcontroller you are working with for detailed information on interrupt
registers and their usage.
Priority of interrupts:
In PIC microcontrollers, interrupt priority levels can be set to manage the order in
which interrupts are serviced. The interrupt priority feature allows you to assign
different priority levels to interrupt sources, ensuring that higher priority
interrupts get serviced before lower priority interrupts. Here is a general overview
of interrupt priority levels in PIC microcontrollers:
PIC microcontrollers typically support two levels of interrupt priority: high priority
and low priority.
Some PIC microcontrollers offer multiple interrupt priority levels, allowing you to
assign different priorities to various interrupt sources.
The number of available priority levels can vary based on the specific PIC
microcontroller model.
Priority Control:
The IPEN bit (Interrupt Priority Enable) in the RCON register is used to enable
interrupt priority levels. When IPEN is set, interrupt priority levels are active.
Priority Assignment:
Interrupts with the same priority level are serviced based on the order of
occurrence.
Interrupt Nesting:
Configure the timer module by setting the timer prescaler, timer mode, and timer
period according to your requirements. For example, setting up Timer1 in 16-bit
mode with a prescaler of 1:4: c T1CON = 0b00000001; // Timer1 module ON with
1:4 prescaler TMR1H = 0x0B; // Load the high byte of the initial timer value
TMR1L = 0xDB; // Load the low byte of the initial timer value
Configure Interrupts:
Write the Timer1 interrupt service routine to handle the timer overflow event.
For example: c void __interrupt() Timer1_ISR(void) { if(PIR1bits.TMR1IF) { // Your
code here // Clear the Timer1 interrupt flag PIR1bits.TMR1IF = 0; } }
Main Function:
In the main function,the timer can be started by setting the TMR1ON bit: c
T1CONbits.TMR1ON = 1; // Start Timer1
Configure the specific pin for external interrupt. Each PIC microcontroller has
specific registers for external interrupts. c TRISBbits.TRISB0 = 1; // Set RB0 pin as
input INTCONbits.INTE = 1; // Enable external interrupt on RB0
OPTION_REGbits.INTEDG = 1; // Interrupt on rising edge
Write the ISR to handle the external interrupt event. For example: c void
__interrupt() External_ISR(void) { if(INTCONbits.INTF) { // Your code here // Clear
the external interrupt flag INTCONbits.INTF = 0; } }
Main Function:
In the main function, enable global interrupts and start monitoring the external
interrupt. ```c INTCONbits.GIE = 1; // Enable global interrupts INTCONbits.INTE =
1; // Enable external interrupts
Interfacing of LED:
Circuit Setup:
Connect an LED with a current-limiting resistor (e.g., 220 ohms) to one of the
GPIO pins of the PIC microcontroller (e.g., RB0).
Code Example:
#include <xc.h>
// Configuration bits
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data
EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write
protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code
protection off)
// Main code
void main() {
while(1) {
__delay_ms(500); // Delay
RS (Register Select) pin of the LCD to a GPIO pin of the PIC (e.g., RB0).
EN (Enable) pin of the LCD to another GPIO pin of the PIC (e.g., RB1).
D0-D7 pins of the LCD to the GPIO pins of the PIC (e.g., RD0-RD7).
Code Example:
Library that be used is like the XC8 LCD library to simplify the interfacing process.
Below is a basic example using the library:
#include <xc.h>
#include <stdint.h>
void main() {
Lcd_Init();
Lcd_Set_Cursor(1, 1); // Set the cursor to the first row, first column
Lcd_Write_String("Hello, LCD!");
while(1) {
ou will need to download and include the LCD library in your project. You can find
various LCD libraries for PIC microcontrollers online.
Library Functions:
Typical functions in an LCD library include initializing the LCD, setting cursor
position, writing characters or strings to the LCD, etc.
To interface a 4x4 matrix keyboard with a PIC microcontroller, you can follow
these steps:
* Connect the rows and columns of the keypad to the I/O pins of the PIC
microcontroller.
* Set the column pins as outputs and the row pins as inputs.
* Write a program to scan the keypad by sequentially driving each column
low and reading the corresponding row inputs to detect key presses.
* Identify the key pressed based on the row and column intersection in the
matrix.
* Perform the desired action based on the key pressed.
* Make sure to debounce the keys to handle any noise or multiple key
presses.
The code assumes that the column pins are connected to PORTB and the row pins
are connected to PORTD:
#include <xc.h>
// Define keymap for the keypad
char keys[4][4] = {
};
void keypad_init() {
TRISD = 0x0F;
char keypad_scan() {
__delay_ms(5);
__delay_ms(5);
The example assumes that the relay is connected to pin RD0 and the buzzer is
connected to pin RD1:
#include <xc.h>
void relay_buzzer_init() {
void relay_on() {
void relay_off() {
void buzzer_on() {
void buzzer_off() {
void main() {