PIC Peripherals: I/O, Timers & Interrupts in Focus

Welcome to this tutorial on core PIC peripherals! In this guide, we will explore three fundamental elements of PIC microcontrollersIntroduction to PIC: Exploring the Basics of Microcontroller ArchitectureIntroduction to PIC: Exploring the Basics of Microcontroller ArchitectureExplore the core principles of PIC microcontroller architecture, including Harvard design, RISC processing, and efficient memory organization.:

1. Digital Input/OutputMastering Digital I/O on PIC MCUs with Practical ExamplesMastering Digital I/O on PIC MCUs with Practical ExamplesLearn hands-on techniques for configuring and using digital I/O pins on PIC microcontrollers to control LEDs, sensors, and more in practical projects. (I/O) configuration and usage.

2. Timer modules for event timing, delays, and wave generation.

3. InterruptImplementing Interrupt-Driven Systems for Real-Time ApplicationsImplementing Interrupt-Driven Systems for Real-Time ApplicationsLearn to configure and optimize PIC microcontroller interrupts for real-time performance. Enhance responsiveness and efficiency using best practices. mechanism for responsive, event-driven applications.

By the end, you will have a solid foundation to build and debugDebugging and Troubleshooting Techniques with ICD and MPLAB XDebugging and Troubleshooting Techniques with ICD and MPLAB XMaster real-time PIC microcontroller debugging with MPLAB X and ICD tools. Discover breakpoint setup, variable inspection, and performance techniques. applications leveraging these essential peripherals. This tutorial is aimed at engineering students, electronics hobbyists, embedded developers, and professionals seeking to sharpen their PIC skills.

Understanding PIC I/O🔗

PIC microcontrollersIntroduction to PIC: Exploring the Basics of Microcontroller ArchitectureIntroduction to PIC: Exploring the Basics of Microcontroller ArchitectureExplore the core principles of PIC microcontroller architecture, including Harvard design, RISC processing, and efficient memory organization. offer flexible digital I/OMastering Digital I/O on PIC MCUs with Practical ExamplesMastering Digital I/O on PIC MCUs with Practical ExamplesLearn hands-on techniques for configuring and using digital I/O pins on PIC microcontrollers to control LEDs, sensors, and more in practical projects. pins. Depending on the device, you may have several ports labeled (for example) PORTA, PORTB, PORTC, etc. Each port has corresponding TRIS registers to control its data direction and LAT (or PORT) registers to read from or write to the pin states.

Configuring I/O Direction

Below is a simple snippet in C for configuring PORTB pins as outputs and toggling an LEDMastering Digital I/O on PIC MCUs with Practical ExamplesMastering Digital I/O on PIC MCUs with Practical ExamplesLearn hands-on techniques for configuring and using digital I/O pins on PIC microcontrollers to control LEDs, sensors, and more in practical projects.:

#include <xc.h>
// Assume FOSC, WDTE, and other config bits are set appropriately
void main(void) {
    TRISB = 0x00;  // Configure all PORTB bits as output
    LATB = 0x00;   // Initialize PORTB output to logic LOW
    while(1) {
        LATBbits.LATB0 = 1;  // Turn on LED connected to RB0
        __delay_ms(500);     // Delay 500 ms
        LATBbits.LATB0 = 0;  // Turn off LED
        __delay_ms(500);
    }
}
Tip: Some PIC devices use PORTx = to write outputs, but modern devices often recommend using LATx =. Whenever reading an input state, we typically use PORTx.

Exploring PIC Timers🔗

Timers allow you to measure or generate precise time intervals without constantly monitoring in software. PIC devices commonly feature multiple timers (e.g., Timer0, Timer1Building Real-Time Projects with PIC Using Timer1 and Input CaptureBuilding Real-Time Projects with PIC Using Timer1 and Input CaptureDiscover how to leverage Timer1 and Input Capture on PIC microcontrollers for precise real-time applications, pulse measurements, and periodic interrupts., Timer2, etc.). Each timer might have different bit-widths (8-bit or 16-bit), prescalers, and modes (timer or counter).

Timer0 Overview

Timer1 Overview

Timer2 Overview

  • Often 8-bit.
  • Contains a dedicated postscaler and period register (PR2).
  • Commonly used for Pulse-Width Modulation (PWM) generation.

Below is a table highlighting general differences:

TimerBit WidthPrescalerTypical Uses
Timer08-bit1:2, 1:4, ...Basic timing, delays, overflow interrupts
Timer116-bit1:1, 1:2, ...Time measurement, RTC, capture/compare
Timer28-bit1:1, 1:4, ...PWM generation, periodic interrupts with adjustable period

Example: Timer0 for Delay

Below is a simple code snippet using Timer0 interrupt for a periodic LED toggleMastering Digital I/O on PIC MCUs with Practical ExamplesMastering Digital I/O on PIC MCUs with Practical ExamplesLearn hands-on techniques for configuring and using digital I/O pins on PIC microcontrollers to control LEDs, sensors, and more in practical projects.:

#include <xc.h>
void __interrupt() timer_isr(void) {
    if (INTCONbits.T0IF) {
        // Toggle an LED on PORTB
        LATBbits.LATB0 = ~LATBbits.LATB0;
        // Clear Timer0 interrupt flag
        INTCONbits.T0IF = 0;
        // (Optional) Reload TMR0 if needed
        // TMR0 = 6; // for example, to adjust timing
    }
}
void main(void) {
    // Configure RB0 as output
    TRISB = 0x00;
    LATB = 0x00;
    // Timer0 Setup (8-bit, internal clock, prescaler = 1:256)
    OPTION_REGbits.T0CS = 0;   // Internal instruction cycle
    OPTION_REGbits.PSA  = 0;   // Prescaler assigned to Timer0
    OPTION_REGbits.PS   = 0b111; // 1:256 prescaler
    // Enable Timer0 Interrupt
    INTCONbits.T0IE = 1;  // Enable TMR0 Interrupt
    INTCONbits.GIE  = 1;  // Enable Global Interrupts
    while (1) {
        // Main loop can do other tasks
    }
}
Note: The actual register names (like OPTION_REG, T0IE) may vary depending on PIC family. Always refer to your specific datasheet.

Harnessing Interrupts on PIC🔗

InterruptsImplementing Interrupt-Driven Systems for Real-Time ApplicationsImplementing Interrupt-Driven Systems for Real-Time ApplicationsLearn to configure and optimize PIC microcontroller interrupts for real-time performance. Enhance responsiveness and efficiency using best practices. let the PIC respond to asynchronous events without polling. When an interrupt condition is met-such as a timer overflow, pin change, or ADC completion-the processor halts normal execution and jumps to the interrupt service routineImplementing Interrupt-Driven Systems for Real-Time ApplicationsImplementing Interrupt-Driven Systems for Real-Time ApplicationsLearn to configure and optimize PIC microcontroller interrupts for real-time performance. Enhance responsiveness and efficiency using best practices. (ISR).

The Interrupt Vector

Most baseline and mid-range PICs have a single interruptImplementing Interrupt-Driven Systems for Real-Time ApplicationsImplementing Interrupt-Driven Systems for Real-Time ApplicationsLearn to configure and optimize PIC microcontroller interrupts for real-time performance. Enhance responsiveness and efficiency using best practices. vector at address 0x0004. When an interrupt triggers, the program counter jumps to this address, and you handle all interrupt sources in one ISR. More advanced PICs have vectored interruptsImplementing Interrupt-Driven Systems for Real-Time ApplicationsImplementing Interrupt-Driven Systems for Real-Time ApplicationsLearn to configure and optimize PIC microcontroller interrupts for real-time performance. Enhance responsiveness and efficiency using best practices., but the concept remains similar.

Configuring Interrupts

1. Enable each peripheral interruptImplementing Interrupt-Driven Systems for Real-Time ApplicationsImplementing Interrupt-Driven Systems for Real-Time ApplicationsLearn to configure and optimize PIC microcontroller interrupts for real-time performance. Enhance responsiveness and efficiency using best practices. in its control register (e.g., T0IE for Timer0, INT0IE for external interruptsImplementing Interrupt-Driven Systems for Real-Time ApplicationsImplementing Interrupt-Driven Systems for Real-Time ApplicationsLearn to configure and optimize PIC microcontroller interrupts for real-time performance. Enhance responsiveness and efficiency using best practices., etc.).

2. Enable Global InterruptImplementing Interrupt-Driven Systems for Real-Time ApplicationsImplementing Interrupt-Driven Systems for Real-Time ApplicationsLearn to configure and optimize PIC microcontroller interrupts for real-time performance. Enhance responsiveness and efficiency using best practices. (GIE) in the INTCON register.

3. Within your ISRImplementing Interrupt-Driven Systems for Real-Time ApplicationsImplementing Interrupt-Driven Systems for Real-Time ApplicationsLearn to configure and optimize PIC microcontroller interrupts for real-time performance. Enhance responsiveness and efficiency using best practices., check the flag that caused the interrupt (e.g., T0IF), handle it, and clear the flag before returning.

Interrupt vs. Polling

Putting It All Together🔗

By combining I/O, timers, and interruptsImplementing Interrupt-Driven Systems for Real-Time ApplicationsImplementing Interrupt-Driven Systems for Real-Time ApplicationsLearn to configure and optimize PIC microcontroller interrupts for real-time performance. Enhance responsiveness and efficiency using best practices., you can implement reactive systems with precise timing. For example, you could:

These three peripherals form the core of most embedded applications on PIC devices, allowing you to read external signals, measure time intervals, and respond to events with minimal CPU overhead.

Conclusion🔗

In this tutorial, we explored:

Mastering these PIC peripherals is an essential step toward creating efficient and robust embedded applications. By using the flexibility of I/O, the precise timing of hardware timers, and the immediacy of interruptsImplementing Interrupt-Driven Systems for Real-Time ApplicationsImplementing Interrupt-Driven Systems for Real-Time ApplicationsLearn to configure and optimize PIC microcontroller interrupts for real-time performance. Enhance responsiveness and efficiency using best practices., you can implement a broad range of designs-from simple blinking LEDs to time-critical control systems.

Feel free to experiment with different prescalers, interruptImplementing Interrupt-Driven Systems for Real-Time ApplicationsImplementing Interrupt-Driven Systems for Real-Time ApplicationsLearn to configure and optimize PIC microcontroller interrupts for real-time performance. Enhance responsiveness and efficiency using best practices. methods, and code structures. This hands-on practice will bolster your grasp on the fundamentals and prepare you for even more advanced PIC applications!

Author: Marcelo V. Souza - Engenheiro de Sistemas e Entusiasta em IoT e Desenvolvimento de Software, com foco em inovação tecnológica.

References🔗

  • Microchip - Documentação oficial e guias de referência para PIC Microcontrollers: www.microchip.com
  • Peatman, John B. - 'Design with PIC Microcontrollers': www.pearson.com

Share article