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 » Checking a Pins Status

February 04, 2011
by Twarter369
Twarter369's Avatar

What is a simple way I an check the status of a pin in my code?

For instance

if(PC3==1) PORTC &= (1<<PC3) else PORTC |= (1<<PC3)

Something like that to create a toggle effect would be great!

I am trying to send a message from my GUI (c++ for the desktop and PHP for the web)to get the state of individual pins so I can find out if something is already on before I try to turn it on again.

February 04, 2011
by bretm
bretm's Avatar

PC3 is just a constant, so doing "if (PC3==1)" won't work.

To check if pin PC3 is set, you can do

if (PINC & (1<<PC3))
    doSomething();

To read the value of PC3 as a 0 or 1 bit:

char pinValue = (PINC >> PC3) & 1;

To toggle pin PC3:

PINC |= 1<<PC3;
February 04, 2011
by Twarter369
Twarter369's Avatar

Okay thanks. I will try those snippets, Thanks! Another problem I am running into is a two step Write/Read. I am sending a character from my GUI (t). On the chip is a line that fires a method when it receives that character. Part of that method is to write back to the serial port the temp. In the c++ I have

chipIO->WriteLine("t") 
chipIO->ReadLine()

where chipIO is my SerialPort^ connection. I have verified the writing is working fine. When I go to read the line back I get an "Unathorized Access" Error?

Post a Reply

Please log in to post a reply.

Did you know that you can impress a loved one with a digitally-controlled Valentine's Day Card with randomly twinkling LEDs? Learn more...