1
\$\begingroup\$

I have made PORTC.0,PORTC.1,PORTC.2,PORTC.3 as input and set it at high. The remaining pins are set as output and high. Thus all pins of PORTC are high.

Now what I want to check that whenever any pin of PORTC gets low (0), then my function "setDirection()"should be called. For that I have written

if(~PINC)setDirection();

but ~PINC condition comes out to be always true, irrespective of any pin of PORTC is low or not. Then I tried this code

if(!PINC)setDirection();

this condition never turns out to be true, either PORTC PIN is high or low.

\$\endgroup\$
  • \$\begingroup\$ While it has been answered how to check pin state correctly, its not clear what are you trying to achieve. What does setDirection() do exactly? \$\endgroup\$ – Rev1.0 Jul 1 '13 at 7:10
  • \$\begingroup\$ @Rev i was trying to detect that if any of the pin of port c gets low function "setDirection()" should be called which then checks which pin of port c is low and sets direction accordingly... \$\endgroup\$ – shafeeq Jul 4 '13 at 9:54
3
\$\begingroup\$
if ((PINC & 0x0F) != 0x0F) setDirection();

This tests if any of the 4 bits are not high. The tests you tried require that ALL of the bits be low.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.