Port Reading , what i make wrong?

i wont read PB4, PB5, PB6 and PB7 for high or low
i try this its only not working
what its wrong?
iam a complet dummy in programming, the last program what i right its 20 years ago.
thanks for help

Dirk

void setup()
{
DDRB = DDRB | B11110000; // sets PB Ports to input (1) (not tuching ports markt whit 0)

PORTB &= B00001111; // set PC Ports to 0 not tuching ports markt whit 1
}

void loop() {

if (PINB & (1<<_BV(4)))
// (PINB == B00010000)
{
star();
}
if (PINB & (1<<_BV(5)))
// (PINB == B00100000)

{
moon();
}

i try whit a variable like this
and now difference

char my_var = 1;
my_var = (PINB & (1 << _BV(4))); // (PINB == B00010000)
if (my_var == 1)
{
star();
}

thanks again

You may need to read the Atmel datasheet and the Arduino pin mapping more carefully.

PB6 and PB7 are the pins for the 16MHz oscillator.

DDRx should be set to 0 for input and 1 for output.

...R

i use a A-STAR 32U4 Micro and i set DDRB = DDRB | B11110000; // sets PB Ports to input (1) (not tuching ports markt whit 0) in this case all 1 go to 0 and all 0 have the status untouched.

mexicodirk: i use a A-STAR 32U4 Micro and i set DDRB = DDRB | B11110000; // sets PB Ports to input (1) (not tuching ports markt whit 0) in this case all 1 go to 0 and all 0 have the status untouched.

This is a quote from Section 10.2.1 of the 32U4 datasheet

If DDxn is written logic one, Pxn is configured as an output pin. If DDxn is written logic zero, Pxn is configured as an input pin.

I had previously assumed you were using an Uno. (The DDxn stuff is the same for both)

...R

Hi ,what are you using as an IDE and programmer cable? Can you post us a picture of your programming setup.

Thanks.. Tom........ :)

my_var = (PINB & (1 << _BV(4)));  //          (PINB == B00010000)
   if (my_var == 1)
                 {                 
                  star();
                  }

The condition in that if statement can NEVER be true. 1<< 4 is 16. So if PINB had that bit set then my_var will be equal to 16. If the bit was not set then my_var will be equal to zero. But it will never ever equal 1.

You can do this:

if(PINB & (1<< 4)

Or you could use what you have and test that my_var is greater than 0.

But it can never ever equal 1.

Actually, with the _BV in there you aren't shifting by 4. You are shifting by 16. _BV(4) == 16.

Since my_var is only char, it only has 8 places. So if you shift a 1 off 16 places in an 8 bit variable you'll get 0 every time.

yes Robin2, i done say what i use, my fault :-(

hay Tom, normal USB A to Micro-B cable for this little Board http://www.pololu.com/product/3101/pictures

Thanks Delta_G this i need work out a little bit moor or understanding shifting and all this. I need to use 4 Inputs PB4 to PB7 and only 1 of this its High, the rest its Low. i now i can use digitalread, only this maks the same only in moor time and moor "script". reading direct from the Bit (Pin) i think its faster, the same like writing. for writing the Pin High and Low i use this:

float varstar = 467470; long previousMicros = 0; ... ... unsigned long currentMicros = micros(); if(currentMicros - previousMicros > varstar) { previousMicros = currentMicros; PINC = _BV(6); //B01000000 PINC = _BV(6); } .... and this its faster then DigitalWrite and moor accurate. works also good whit millis. varstar its only a number (time between 2 events)

Thanks all i will play tonight moor whit this bits and bytes :-)

Dirk

Thanks Delta_

your are the hero

if(PINB & (1<< 5))

simple and works

Thanks