NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Is there an "If" statement that can tell if a microcontroller pin is connected to +5 volts?

June 07, 2011
by rboggs10
rboggs10's Avatar

For example:

if ("PC4 is connected to 5 volts") {
    //Code
}

How may this be accomplished?

June 07, 2011
by bretm
bretm's Avatar

The state of the pins is reflected in the PINx registers assuming the pin is configured for input (DDRx bit is zero). I'm pretty sure the Nerdkit guide goes over this, so I would review that. There is also an example in the library.

June 07, 2011
by delinthe
delinthe's Avatar

an example from a project of mine where i assign a pins state to a variable. at which point you can just check to see whether or not the variable is reading 1 or 0. when the pin is receiving vcc(5v) it will read 1 when it is not it will read 0.

button_up = (PINC & (1<<PC5)) >> PC5
if (button_up == 1){}

alternatively you could simply do something like this if you don't need to use the button state more than once per program loop.

if (((PINC & (1<<PC5)) >> PC5) == 1){}

June 07, 2011
by rboggs10
rboggs10's Avatar

Thanks delinthe that should be very useful for me. Also thanks to bretm for helping too.

June 08, 2011
by bretm
bretm's Avatar

Or just

if (PINC & (1 << PC5))
June 21, 2011
by lnino
lnino's Avatar

I would be prefer the solution of bretm. As I remember this is exaclty the solution described in the nerdkits guide.

Post a Reply

Please log in to post a reply.

Did you know that SPDT stands for "Single Pole, Double Throw"? Learn more...