PIN IF Statment

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I was making a program, that is a basic button program were you if Pin B1 is high then it makes Pin B0 high. But the code does not work but it does compiles. Can someone help me find out what is wrong?

Code:

DDRB = 0b11111101;
	
	
	if ((PIND, 00000010)==0)
	{
		PORTB =  00000001;
	}

Life Is Like A Bucket Of Chicken.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

well for starters you don't have any base on your numbers except the initial DDRB line, as such the base will be taken to be Octal, because of the leading 0, I'm assuming you wanted binary.

Secondly you have no operation in the "IF" statement. You need to combine the mask with the PIN value using AND.

DDRB = 0b11111101;
   
   
   if ((PIND & 0b00000010)==0)
   {
      PORTB =  0b00000001;
   } 

I'm still amazed that did not generate an error.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

glitch wrote:
I'm still amazed that did not generate an error.
The original code was valid C, but obviously did not do what the OP wanted.
if ((PIND, 00000010)==0)

The C comma operator is valid syntax. The original code simplifies to:

if (00000010 == 0)

Since that is always false, I'd imagine if you look at the output of the compiler, the entire if statement is optimized away.