Overview

In this essay, we will study PIC Microcontroller push button interfacing. The GPIO (General Purpose input-output) pins are also provided by PIC, just like other microcontrollers. We can connect input-output devices to GPIO pins, such as LEDs, switches, adc power supplies, sensors, etc.

Here, we’ll see how to operate an SPST (single pole, single throw) switch to control a led. The simplest example of an input and output device is a led and pushbutton. Go here for more information about DSPIC33FJ64GS606.

Components Required Table

S.NoComponentsQuantitylink
1DSPIC33FJ64GS6061https://www.mouser.in/ProductDetail/Microchip-Technology-Atmel/dsPIC33FJ64GS606
2PICkit4 Programmer1https://www.amazon.in/MPLAB-PICkit4-in-Circuit-Debugger
3LED’S1https://www.amazon.in/Traffic-Lights-Signal-Module-Digital
4Push Button1https://robu.in/product/momentary-tactile-push-button
5Breadboard – 21https://www.amazon.in/Robotbanao-Solderless-MB102-Breadboard1
6Jumper wires1(set)https://www.amazon.in/YUVS-Jumper-Wires

Software Requirements

  • MPLAB X IDE
  • MPLAB X X16 Compailer

Circuit Diagram

Parallel I/O (PIO) Ports

Generally, a parallel I/O port that shares a pin with a peripheral is subservient to the peripheral. The peripheral’s output buffer data and control signals are provided to a pair of multiplexers. The multiplexers select whether the peripheral or the associated port owns the output data and control signals of the I/O pin. The logic also prevents “loop through”, in which a port’s digital output can drive the input of a peripheral that shares the same pin.

When a peripheral is enabled and the peripheral is actively driving an associated pin, the use of the pin as a general-purpose output pin is disabled. The I/O pin can be read, but the output driver for the parallel port bit is disabled. If a peripheral is enabled, but the peripheral is not actively driving a pin, that pin can be driven by a port.

All port pins have three registers directly associated with their operation as digital I/O. The Data Direction register (TRISx) determines whether the pin is an input or an output. If the data direction bit is ‘1’, the pin is an input. All port pins are defined as inputs after a Reset. Reads from the latch (LATx) read the latch. Writes to the latch write the latch. Reads from the port (PORTx) read the port pins, while writing to the port pins write the latch. Any bit and it’s associated data and control registers that are not valid for a particular device will be disabled. That means the corresponding LATx and TRISx registers and the port pin will read as zeros.

PIC I/O Register Configurations

RegisterDescription
TRISxUsed to configure the respective PORT as output/input
PORTxUsed to Read/Write the data from/to the Port pins
LATxUsed to Read/Write the data from/to the Port pins

Source code

Input and output selection

TRISGbits.TRISG3 = 0; // 0 as a output 1 as a input Led
TRISGbits.TRISG2 = 1; // 0 as a output 1 as a input Pushbutton

Read the input button status

PORTGbits.RG2;

Final Source Code

/*
 * File:   main.c
 * Author: Admin
 *
 * Created on  March, 2023,
 */


// DSPIC33FJ64GS606 Configuration Bit Settings

// 'C' source line config statements

// FBS
#pragma config BWRP = WRPROTECT_OFF     // Boot Segment Write Protect (Boot Segment may be written)
#pragma config BSS = NO_FLASH           // Boot Segment Program Flash Code Protection (No Boot program Flash segment)

// FGS
#pragma config GWRP = OFF               // General Code Segment Write Protect (General Segment may be written)
#pragma config GSS = OFF                // General Segment Code Protection (General Segment Code protect is disabled)

// FOSCSEL
#pragma config FNOSC = FRCDIVN          // Oscillator Source Selection (Internal Fast RC (FRC) Oscillator with postscaler)
#pragma config IESO = ON                // Internal External Switch Over Mode (Start up device with FRC, then switch to user-selected oscillator source)

// FOSC
#pragma config POSCMD = NONE            // Primary Oscillator Source (Primary Oscillator disabled)
#pragma config OSCIOFNC = OFF           // OSC2 Pin Function (OSC2 is clock output)
#pragma config FCKSM = CSDCMD           // Clock Switching Mode bits (Both Clock switching and Fail-safe Clock Monitor are disabled)

// FWDT
#pragma config WDTPOST = PS32768        // Watchdog Timer Postscaler (1:32,768)
#pragma config WDTPRE = PR128           // WDT Prescaler (1:128)
#pragma config WINDIS = OFF             // Watchdog Timer Window (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF             // Watchdog Timer Enable (Watchdog timer enabled/disabled by user software)

// FPOR
#pragma config FPWRT = PWR128           // POR Timer Value (128ms)
#pragma config ALTSS1 = ON              // Enable Alternate SS1 pin bit (SS1A is selected as the I/O pin for SPI1)
#pragma config ALTQIO = OFF             // Enable Alternate QEI1 pin bit (QEA1, QEB1, INDX1 are selected as inputs to QEI1)

// FICD
#pragma config ICS = PGD1               // Comm Channel Select (Communicate on PGC1/EMUC1 and PGD1/EMUD1)
#pragma config JTAGEN = OFF             // JTAG Port Enable (JTAG is disabled)

// FCMP
#pragma config HYST0 = HYST45           // Even Comparator Hysteresis Select (45 mV Hysteresis)
#pragma config CMPPOL0 = POL_FALL       // Comparator Hysteresis Polarity (for even numbered comparators) (Hysteresis is applied to falling edge)
#pragma config HYST1 = HYST45           // Odd Comparator Hysteresis Select (45 mV Hysteresis)
#pragma config CMPPOL1 = POL_FALL       // Comparator Hysteresis Polarity (for odd numbered comparators) (Hysteresis is applied to falling edge)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include "p33FJ64GS606.h"
#include "libpic30.h"
#include "dsp.h"

uint16_t ButtonStatus = 0;

int main(void) {
    TRISGbits.TRISG3 = 0; // 0 as a output 1 as a input
    TRISDbits.TRISD2 = 0; // 0 as a output 1 as a input
    TRISGbits.TRISG2 = 1; // 0 as a output 1 as a input
    while(1)
    {
        ButtonStatus = PORTGbits.RG2;
        if(ButtonStatus == 1)
        {
            LATGbits.LATG3 = 1;// trun on led
            LATDbits.LATD2 = 1; // trun on led
        }
        
        else
        {
            LATGbits.LATG3 = 0;// trun off led
            LATDbits.LATD2 = 0; // trun off led 
        }
    }
}

By Devilal

Leave a Reply

Your email address will not be published. Required fields are marked *