Skip to content

Engineering Xpert

We Simplify Engineering

Menu
  • Home
  • About
  • Contact
  • MATLAB
  • PIC Microcontroller
Menu
How to Check an Input Pin of PIC Microcontroller

How to Check an Input Pin of PIC Microcontroller

Posted on May 22, 2022May 24, 2022 by Aamir Javed

This posts describes how to check the state of a single input pin of PIC microcontroller in C as well as assembly language.

Note: For this tutorial I am using MPLAB X v5.0 but you can use any version upto v5.35. Version 5.35 is last version that included MPASM i.e. the assembler we require to compile the assembly code used in this tutorial. Code given here will not work for later versions.

How to Check a Single Input Pin of PIC Microcontroller in Assembly

The status of a single bit can be checked by using bit-oriented instructions BTFSS and BTFSC. I have already discussed how to use these instructions in a post titled “BTFSS and BTFSC Instructions for PIC Microcontroller“.

BTFSS and BTFSC Instructions for PIC Microcontroller

Lets look at few examples of using BTFSS and BTFSC to monitor an input pins and perform different actions.

Example: Check the status of a pin and perform an action if it is ‘0’

The following code uses BTFSC to monitor Pin 0 of PORTC continuously until it becomes ‘0‘. 0x55 is sent to PORTD when it becomes ‘0‘.

    BSF		TRISC, 0	; Make Pin 0 of PORTC an input Port
    CLRF	PORTD		; Make PORTD an output Port
    
CHECK
    BTFSC	PORTC, 0	; Check Pin 0 of PORTC, Skip next instruction if it is '0'
    BRA		CHECK		; Code comes here if Pin 0 of PORTC is '1'. Check it again.    
    MOVLW	0x55		; Copy 0x55 to WREG if Pin 0 of PORTC is '0'
    MOVWF	PORTD		; Copy WREG (0x55) to PORTD

Example: Check the status of a pin and copy its value to a selected bit of a file register

The following code uses BTFSS to monitor pin 5 of PORTC continuously and copies its value to bit ‘0‘ of RAM location 0x01.

    BSF		TRISC, 5	; Make Pin 5 of PORTC an input Port
    
CHECK
    BTFSS	PORTC, 5	; Check Pin 5 of PORTC, Skip next instruction if it is '1'
    BCF		0x01, 0		; Code comes here if Pin 5 of PORTC is '0'. Clear Bit 0 of RAM location 0x01.    
    BSF		0x01, 0		; Code comes here if Pin 5 of PORTC is '1'. Set Bit 0 of RAM location 0x01.

Example: Check the status of a pin and send its value to another pin

The following code uses BTFSS to monitor a switch connected to pin 5 of PORTC continuously and sends its status to an LED connected to pin ‘3‘ of PORTD.

    BSF		TRISC, 5	; Make Pin 5 of PORTC an input Port
    BCF		TRISD, 3	; Make Pin 3 of PORTD an output Port
    
CHECK
    BTFSS	PORTC, 5	; Check Pin 5 of PORTC, Skip next instruction if it is '1'
    BRA	  LED_OFF		; Code comes here if Pin 5 of PORTC is '0'. Jump to label LED_OFF
    BSF		PORTD, 3	; Code comes here if Pin 5 of PORTC is '1'. Set Pin 3 of PORTD
    BRA		CHECK		; Check again
LED_OFF
    BCF		PORTD, 3	; Clear Pin 3 of PORTD  
    BRA		CHECK		; Check again

How to Check a Single Input Pin of PIC Microcontroller in C

Individual bit x of TRISy can be accessed by writing TRISybits.TRISyx. To access pin Ryx, you need to write PORTybits.Ryx. Using this syntax and comparison operators in C, we can check the status of a single pin and perform desired actions based on the status.

Example: Check the status of a pin and perform an action if it is ‘0’

The following code monitors Pin 0 of PORTC continuously until it becomes ‘0‘. 0x55 is sent to PORTD when it becomes ‘0‘.

#include <xc.h>

void main(void)
{    
    TRISCbits.TRISC0 = 1;       // Make RC0 an input port
    TRISD = 0x00;               // Make PORTD an output port 
    
    while (PORTCbits.RC0 == 1); // Monitor RC0
    PORTD = 0x55;               // Send 0x55 to PORTD when RC0 becomes 0
    
    while (1);
}

Example: Check the status of a pin and send its value to another pin

The following code monitors a switch connected to pin 5 of PORTC continuously and sends its status to an LED connected to pin ‘3‘ of PORTD.

#include <xc.h>

void main(void)
{    
    TRISCbits.TRISC5 = 1;       // Make RC5 an input port
    TRISDbits.TRISD3 = 0;       // Make RD3 an output port 
    
    while(1)
    {
        if (PORTCbits.RC5 == 1)
        {
            PORTDbits.RD3 = 1;      // Set RD3, if RC5 = 1
        }

        else
        {
            PORTDbits.RD3 = 0;      // Clear RD3, if RC5 = 0
        }
    }
}

Latest Posts

  • MOVF and MOVFF Instructions for PIC Microcontroller
  • MOVLW and MOVWF Instructions for PIC Microcontroller
  • CALL RCALL and RETURN Instructions for PIC Microcontroller
  • BRA and GOTO Instructions for PIC Microcontroller
  • INCFSZ and INFSNZ Instructions for PIC Microcontroller

1 thought on “How to Check an Input Pin of PIC Microcontroller”

  1. Pingback: PIC Bit Test Instructions with Examples - Engineering Xpert

Leave a Reply Cancel reply

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

Recent Posts

  • MOVF and MOVFF Instructions for PIC Microcontroller
  • MOVLW and MOVWF Instructions for PIC Microcontroller
  • CALL RCALL and RETURN Instructions for PIC Microcontroller
  • BRA and GOTO Instructions for PIC Microcontroller
  • INCFSZ and INFSNZ Instructions for PIC Microcontroller

Categories

  • Electric Circuits
  • How To
  • MATLAB
  • MPLAB X
  • PIC18
  • PIC18 Assembly
  • PIC18 C
  • Proteus
  • STM32
  • STM32Cube IDE
  • STM32F3 Discovery Kit

About

We post tutorials and guides related to a variety of topics like MATLAB, PIC18 Microcontrollers, Electric Circuits, Digital Logic Design, Arduino, Raspberry Pi, STM32

© 2023 Engineering Xpert | Powered by Superbs Personal Blog theme