Pages: [1]   Go Down
Author Topic: Direct access digital IO pins - not working as expected  (Read 788 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 117
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Need some help to understand why my program isn't working as expected.

Here's the breadboard view of my circuit.

Basically a ATtiny85 with PB1 hooked to a 4-legged PCB-mount push-button switch, with a 10K pull-down resistor to GND, s.t. when button is pressed PB1 is pulled HIGH, else LOW. Also PB4 is hooked to an LED via a 470R. Only other thing connected to the IC is Vcc and GND.

The current behavior is that the LED is always on, while it was supposed to be normally off, and turn-on only when button is pressed. Here is my code. What is it that I am doing wrong --

Code:
// PB1 is of type INPUT
#define XPB1    B00000010
#define XPB1_N  B11111101
// PB4 is of type OUTPUT
#define XPB4    B00010000
#define XPB4_N  B11101111

void setup()
{
  DDRB = DDRB | XPB4;           // set PB4 to OUTPUT
  PORTB = PORTB & XPB4_N;   // turn off LED
}

void loop()
{
  if (PINB & XPB1_N) {
    PORTB = PORTB | XPB4;
  }
  else {
    PORTB = PORTB & XPB4_N;
  }
}


Is it possible that the conditional expression (PINB & XPB1_N) always evaluates to true ?
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 206
Posts: 12849
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Yes.  The other pins are floating and could easily be HIGH.  Try this...

Code:
  if ( (PINB & _BV(PINB1)) != 0 ) {
Logged

0
Offline Offline
Shannon Member
****
Karma: 206
Posts: 12062
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or
Code:
  if (PINB & XPB1)
Logged

[ I won't respond to messages, use the forum please ]

0
Offline Offline
Full Member
***
Karma: 0
Posts: 117
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Yes.  The other pins are floating and could easily be HIGH.  Try this...

Code:
  if ( (PINB & _BV(PINB1)) != 0 ) {


Argh... my, so bad ! Deserve a hard kick... yes indeed.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 117
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or
Code:
  if (PINB & XPB1)

Precisely. Thanks @MarkT.
Logged

Pages: [1]   Go Up
Jump to: