0
\$\begingroup\$

I'd like to turn on the board's LED (PB5) when I press its button (PB7).

DDRB = 0xFF; // set all B-ports as output
DDRB &= ~(1<<7); // change PB7 to input
while (1)
{
    if (PINB & (1<<7))
        PORTB |= (1<<5); // when button is pressed, turn on the LED
    else
        PORTB &= ~(1<<5); // when button is not pressed, turn off the LED
}

However, PINB & (1<<7) returns 1 when I except 0, and 0 when I press the button. I could add a not operator and it'd be fine, but I'd like to know what I missunderstand. Why is the 8th bit of PINB read as 0 when I press the button?

\$\endgroup\$
5
  • 2
    \$\begingroup\$ Post a schematic that shows how the button is connected up. Some circuits have the button pull down the signal pin of the MCU when the button is pressed. \$\endgroup\$ – Michael Karas May 21 '16 at 17:12
  • \$\begingroup\$ Simple: !(PINB & (1<<7)). Now you get a 1 when the button is pressed and a 0 when released. \$\endgroup\$ – Tom Carpenter May 21 '16 at 17:24
  • \$\begingroup\$ Almost every single button I have ever put in a schematic was "active low" meaning that one terminal was connected to ground, and the other one was connected to both a pullup resistor and a GPIO. Of course it is possible to do it the other way, too. But it appears that in your case, it is an active low signal (low = button pressed). \$\endgroup\$ – mkeith May 21 '16 at 19:25
  • \$\begingroup\$ PINB & (1<<7) might evaluate to true but it cannot evaluate to 1 without additional operations. \$\endgroup\$ – Chris Stratton May 21 '16 at 20:51
  • \$\begingroup\$ @ChrisStratton That's true, by 1 I meant non-zero which equals to true in an if, but right now this isn't important. \$\endgroup\$ – klenium May 21 '16 at 20:54
1
\$\begingroup\$

What PINB & (1<<7) will return depend on how the button is wired. It will return true (not 0) value if the voltage on the pin is in the range of the high logic level and false (0) if it is in the range of the low logic level.

In the first case, the GPIO pin is pulled up to VCC (logic high) by default, so if the button is not pressed PINB & (1<<7) will return true. If you press the button, the voltage at the pin will be GND (logic low), so your statement will return false (0).

In the second case the configuration is reversed. The pin is pulled down to GND by default and when the button is pressed the voltage will be VCC. So the input will be true on button presses.

schematic

simulate this circuit – Schematic created using CircuitLab

On your board, you probably have the first case.

\$\endgroup\$
3
  • \$\begingroup\$ I found this in the board's user guide: i.stack.imgur.com/Gji5D.png I'm a bit new in electronics, and for me (as programmer) default was=0, in use=1. \$\endgroup\$ – klenium May 21 '16 at 20:50
  • \$\begingroup\$ You are preserving an error made by the poster. PINB & (1<<7) might evaluate to true but it cannot evaluate to 1 \$\endgroup\$ – Chris Stratton May 21 '16 at 20:52
  • \$\begingroup\$ @Chris Stratton Indeed, thank you for pointing out, corrected this issue. \$\endgroup\$ – Bence Kaulics May 21 '16 at 21:18

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.