[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [avr-gcc-list] Stoooopid Noooobie question...

From: Jack Valmadre
Subject: RE: [avr-gcc-list] Stoooopid Noooobie question...
Date: Tue, 2 Dec 2003 16:48:45 +1000

To work with a few bits without modifying the others, use |=, &= and ~.
If you know logic, & means AND, | means OR, ~ means invert.

1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0

1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0

1 ~ 0
0 ~ 1

To set PB3 high (5V) while still leaving the rest of the pins alone:
PORTB |= (1<<PB3);

To set PB3 and PB5:
PORTB |= (1<<PB3) | (1<<PB5);

To clear PB3 (GND):
PORTB &= ~(1<<PB3);

To clear PB3 and PB5:
PORTB &= ~((1<<PB3) | (1<<PB5));

To check if PB2 is high (5V):
if(PINB & (1<<PB2))

To check if PB2 and PB4 are high:
if(PINB & ((1<<PB2) | (1<<PB4)))

To check if PB2 is low (GND):
if(~PINB & (1<<PB2))

To check if PB2 and PB4 are low:
if(~PINB & ((1<<PB2) | (1<<PB4)))

All this will be easier to understand if you write all the bits out for
each byte you're reading/writing and have a look what the logic command

-----Original Message-----
From: address@hidden
[mailto:address@hidden On Behalf Of Robert Ussery
Sent: Tuesday, 2 December 2003 1:29 PM
To: address@hidden
Subject: [avr-gcc-list] Stoooopid Noooobie question...

Hi, all.

I've got a very stupid Nubie question, that may turn out to be a case of
RTFM. Anyway, here's my question. How can I address specific pins or
bits of
I/O and settings registers? For instance, I want to set only PB3 as an
output after I've set the rest of Port B to be inputs. How can I do this
without modifying all the bits of the DDRB register?  Another example
be reading the state of, say PB2, while ignoring the rest of the
(port). I imagine there's some way to do this with elaborate XORing or
something, but I'm convinced there's some obvious, considerably easier
that I simply haven't run across.

I'm very new to avr-gcc, and I don't have any C programming experience,
this is a case of learning as I go along. I've been through the ritual
blinking programs, but now that I'm trying some more advanced stuff I'm
finding my knowledge level very limiting. Thanx for your help.


- Robert

avr-gcc-list mailing list

reply via email to

[Prev in Thread] Current Thread [Next in Thread]