Go Down

Topic: Direct access digital IO pins - not working as expected (Read 913 times) previous topic - next topic

falcon74

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: [Select]

// 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 ?

Coding Badly


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

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


MarkT

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

falcon74



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

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




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


Go Up