How to Check the State of an Input Pin of a PIC Microcontroller in C

PIC18F4525 microcontroller

In this article, we show how to check the state of an input pin of a PIC microcontroller in the C programming language.

By state, we mean how to check or monitor whether the input is a logic '0' (0v) or a logic '1' (3.3V-5V).

It is important to know how to montior the logic state of a pin of a microcontroller because we may need to do certain actions based on the input of the pin.

Many electronic devices require user input and based on this input, the device does some programmed task.

A very basic example, probably the most basic, is a switch connected to a pin that is programmed to be an input. Once the switch gets turned on, which means connected to a logic '1' (which is 3.3V-5V), then we can program some event based on the switch. But you have to know how to code this in into your program.

So that is what we do in the prorgram.

So let's imagine that we have a very simple circuit, with the pin, RA0, connected to a switch. This pin functions as an input.

Then we also have an LED on pin, RB0. This pin functions as an output.

We will make a program that when the switch goes to a logic '1' or high on pin RA0, then we turn the LED on pin RB0.

This is shown in the code below.

We will now explain this code.

So every C program has a main() function, which is where the program starts.

We have to initialize the pins, meaning set which will be inputs or outputs, as well as whether they will be analaog or digital.

We set pin RA0 of PORTA as an input.

We set all pins of PORTB as outputs.

We turn the ADC off, since we are not setting any pins as analog.

We then set all pins in PORTA and PORTB as digital.

We then set the internal oscillator.

After initializing necessary values, we then move onto to our while loo.

So what we want to create is an infinite while loop, or a forever loop.

The reason we want to create an forever loop in our code is because we want to monitor the input forever.

Inputs can change repeatedly over and over, so it's not a one-time event. We need to monitor continuously, forever, in order to see what if there are any changes, so that we can respond accordingly (which is to turn on the LED if the input is a logic high or off if the input is a logic low).

So anything placed in between while(1) { } forms an infinte while loop, which is what we desire.

Within the while loop, we place a while loop. This while loop is not infinite. It is only infinite as long as the input at pin RA0 is a logic '0' or low; while this is the case, the bit at RB0 remains at a state of logic '0'. When the input turns into a logic '1' or high, then this while loop is exited and the pin RB0 is set to a logic '1' or high.

You may ask, why an infinite while loop is needed and not just the while loop within the infinite while loop?

Without the infinite while loop, the program will exit the regular while loop and loop through the code which initializes the program setup. This is not desired, as initialization is only needed to be done once. With an infinite while loop, or forever loop, the while loop is always true, so it never gets exited. This ensures that the program does not reinitialize over and over again.

If you simulate this program using the MPLab X IDE simulator, you will see that there is a 0-0 or 1-1 relationship between the input and the output. What this means is that when the input, in this case, pin RA0 is a logic '0', the output at pin RB0 will be at a state of logic '0'. When the input RA0 is at a logic '1', the output RB0 will be at a logic '1'.

And this is how we can check the state of an input pin of a PIC microcontroller in C.

Related Resources

How to Set the Ports of a PIC Microcontroller in C

HTML Comment Box is loading comments...