How to take input from the joystick on the AVR butterfly development board

avrmicrocontroller

I am currently programming an AVR butterfly dev board. I want to take input from the joystick and my program to do certain things whenever the joystick is puled down. To do this I have the following pieces of code. (There are more but this is what you need to answer the question.)

#define false 0

#define true 1

DDRB = (0<<DDB7);

PORTB = (1<<PB7);

....

while (PINB==1)

....

while (PINB==0)

....

Right now the program avt as PINB is never 0 neither is it 1. I think my problem is that I never even read if the joystick is moved down or not.

Best Answer

Checking

while (PINB == 1)  {...}

is probably not what you want. PINB refers to the 8 bit register for the whole port B. If you want to check the state of a specific pin you have to mask it like this:

while (PINB & (1<<PB0)) {...}

That condition is true as long as pin 0 on port B is high.