Saturday, August 14, 2021

Programming the External Interrupt of PIC16F818 in XC8

 

Introduction

In microcontroller, interrupt is hardware or software event notification to the system. Each time interrupt occurs the system could respond or ignore to that event. Amount of interrupt capabilities of microcontroller is different. Interrupt triggered by internal or external events.

Programming the External Interrupt of PIC16F818 in XC8
Simulation Sample of this programming example


PIC16F818 has many interrupt sources that we can not list them all here. External interrupt is an interrupt source triggered by external logic change on pin RB0 of port B in PIC16F818/819. It can be an interrupt on falling or rising edge depends on user configuration in software.

External Interrupt Preparation

There are many steps of software configuration in programming. We will need to configure port data direction, interrupt edge, global interrupt control, etc.

Port Data Direction

Pin RB0 must be a digital input as it’s a logic input to the microcontroller. An optional weak pull-up resistor could save the number of external add-on components and circuit wiring. Hence the user must clear nRBPU of the OPTION register.

Programming the External Interrupt of PIC16F818 in XC8
OPTION Register

But the interrupt edge must selected to work in falling edge of input logic.

Interrupt Edge Selection

Edge of external interrupt are rising and falling edge. The user select between these two mode using Interrupt Edge Select Bit (INTEDG) of the OPTION register.

Programming the External Interrupt of PIC16F818 in XC8
Interrupt Edge Select Bit

Clearing this bit to select the interrupt on falling edge from external input device – for example a motion sensor.

Turning on External Interrupt

Interrupt Control Register (INTCON) consists of many interrupt source setting and interrupt flag, including the external interrupt. An interrupt flag in PIC microcontroller is set whenever the corresponding interrupt source occurs. The user must test and clear this flag in software at the time it occurs and ended.

Programming the External Interrupt of PIC16F818 in XC8
Interrupt Control Register (INTCON)

Bit 7 of INTCON is a Global Interrupt Enable bit (GIE) is a general interrupt switch. Setting to ‘1’ before other interrupt sources are enabled.

Bit 4 is an External Interrupt Enable bit (INTE). Setting this bit to turn on external interrupt on RB0.

Bit 1 is an External Interrupt Flag bit (INTF). This bit must be cleared first in the first program configuration for the external interrupt. In the Interrupt Service Routine (ISR), the code must test this bit to find its occurrence.

Interrupt Service Routine

An Interrupt Service Routine (ISR) is a piece of code written in the interrupt vector of PIC microcontroller. It responses to the interrupt whenever it’s enabled an occured.

In mid-range PIC microcontroller as PIC16F818 there is only one interrupt vector locates at the address 0x04 in program memory. However in a high level programming language like XC8 we don’t care about this vector. ISR in XC8 is just a C function but it’s reserved for interrupt vector.

void interrupt _ISR_NAME(void){
//Code here
}

In ISR the user must test the interrupt flag, writing codes to response to interrupt, and clear interrupt flag.

XC8 Programming for External Interrupt

This simple external interrupt programming example show a blinking LED as its normal main program routine. Whenever the external interrupt occurs, it toggles an output LED.

Schematic Diagram

External interrupt attaches to RB0 while two output LED are the main program loop blinking and another LED is interrupt response.

Programming the External Interrupt of PIC16F818 in XC8
Schematic diagram for this interrupt programming example

SW2 triggers the external interrupt at whenever it’s pressed.

XC8 Programming

Software setting configure this controller to use its internal oscillator clocks at 4MHz. Other remaining code are written to support the interrupt capabilities.

/*PIC16F818 External Interrupt Example
 * Using MPLABX XC8
 */
#include <xc.h>

// PIC16F818 Configuration Bit Settings
// CONFIG
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTRC oscillator; port I/O function on both RA6/OSC2/CLKO pin and RA7/OSC1/CLKI pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital I/O, MCLR internally tied to VDD)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB3/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off)
#pragma config CCPMX = RB2      // CCP1 Pin Selection bit (CCP1 function on RB2)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

#define _XTAL_FREQ 1000000

/*Naming the Output LED*/
#define toggleLed   RB7
#define blinkLed    RB6

void main(void){
    /*Oscillator Setup 1MHz*/
    OSCCONbits.IRCF=0x04;
    /*Clear Port B*/
    PORTB=0x00;
    /*RB0 Input */
    TRISB=0x01;
    /*Turn On Pull Up Resistor*/
    nRBPU=0;
    /*Enable Global Interrupt*/
    GIE=1;
    /*Enable External Interrupt*/
    INTE=1;
    /*Select Interrupt on Falling Edge*/
    INTEDG=0;
    /*Clear Interrupt Flag*/
    INTF=0;
    while(1){
        blinkLed^=1;
        __delay_ms(250);
    }
}

void interrupt _ISR(void){
  /*Test external interrupt flag*/
    if(INTF){
      /*Clear flag*/
        INTF=0;
      /*Toggle LED*/
        toggleLed^=1;
    }
}

Click here to download this example archive.

No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (54) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (46) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (66) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (2) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (30) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)