Baby-O: reading input code question and debugging question

I wanted to check to see if this is the proper way to read the inputs off of the PB0,3,4,5 pins.
PORTB=(1<< PB0)|(1<< PB3)|(1<<PB4)|(1<<PB5);
DDRB=(0<< PB0)|(0<< PB3)|(0<<PB4)|(0<<PB5);

N=PINB5;
S=PINB4;
W=PINB3;
E=PINB0;

and my other question is I would like to watch the values that are being written on the chip and Im using the newest version of AVR studio and have my baby-o attached using the programmer from pololu. Ive tried every setting and it says that it cant find it when I try to run the program any ideas.

Thanks,

MichaelS

Hello.

The expression (0 << x) will always evaluate to 0 no matter what x is, so your line

DDRB = (0 << PB0) | … | (0 << PB5);

is just equivalent to

DDRB = 0 | 0 | 0 | 0;

In general, if you want to clear a specific bit (set it to zero) in a byte, you should use the following type of instruction:

DDRB &= ~(1 << PB0) & ~(1 << PB3) & ~(1 << PB4) & ~(1 << PB5);

This line clears bits PB0, PB3, PB4, and PB5 while leaving the rest of DDRB unchanged. If you want to see how this works, you can consider the simple case of

DDRB &= ~(1 << 5);

1 << 5 = 00100000
~(1 << 5) = not 00100000 = 11011111
Performing a bitwise and of this byte with DDRB will clear DDRB bit 5 (since it’s being anded with a zero) while leaving all other DDRB bits the same (since they’re being anded with ones).

You only need to set the values of PORTB if you want to enable pull-ups on those specific inputs. If that is your goal, I suggest you set the PORTB bits after you use DDRB to configure those pins as inputs (and once again, I recommend you use the |= operator rather than the = operator so as to leave the other pins of PORTB unchanged by the operation):

DDRB &= ~(1 << PB0) & …
PORTB |= (1 << PB0) | …

PINB5 is just a constant (it’s defined as 5 in iomx8.h), so this will definitely not get you the results you want. To read a pin value, you want to use an instruction like

N = PINB & (1 << PB5); // (1 << PB5) if PB5 is high, 0 if PB5 is low
S = PINB & (1 << PB4);

Note that if PINB has bit PB5 set, N ends up with the value (1 << PB5), which equals 1 << 5 = 2^5 = 32. If PINB has bit PB5 cleared, N ends up with the value 0. If you want to normalize things so that each of your four variables is 1 when itsassociated bit is set, you can do something like:

N = ((PINB & (1 << PB5)) == (1 << PB5)); // 1 if PB5 is high, 0 if PB5 is low
S = ((PINB & (1 << PB4)) == (1 << PB4));

I don’t understand what you’re trying to do here. Are you talking about doing some sort of real-time in-circuit debugging? This is not possible with a standard AVR ISP programmer such as our Orangutan USB programmer; you would need to use something like a JTAG In-Circuit Emulator. AVR Studio can run your program in a simulator, though, which lets you step through your program line by line tracking variables and virtual register values. I find this is often more than sufficient for my debugging needs.

-Ben