Ok so i have a very interesting project i want to use this for. Its basicly a device to turn on and off a light based on whos in the room.
So my question is how do i set some ports to be input and some to be output. I want it to be like port 1 and 2 to be input and port 3,4,5 to be output and 6,7,8 to be input. I know you set it with DDRB but would it be something like DDRB = 0b11000111 or what? Thanks in advance
Help me create code
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
-
- Posts: 1645
- Joined: Sat Nov 10, 2007 12:59 am
Re: Help me create code
what chip are you using?
a couple of matters of semantics.
PORTB is the port. it is eight bits wide.
those things you refer to as ports 1-8 are pins 0-7.
to set pins 0, 1, 5, 6 & 7 to be inputs their corresponding bits in DDRB should be cleared. the corresponding bits for pins 2, 3 & 4 should be set to make them outputs.
the syntax preferred in avr-gcc, though, is
i have what is possibly an irrational hatred of binary literals. i find them very difficult to read, which may be why i failed to notice at first that you have the bits in the reverse order. pin 0 is the low order bit of the i/o port. think of it as 2^^0. pin 7 is the high order bit and is 2^^7. these things work just like decimal numbers.
the proper way to set DDRB as you have described with a binary literal is
a couple of matters of semantics.
PORTB is the port. it is eight bits wide.
those things you refer to as ports 1-8 are pins 0-7.
to set pins 0, 1, 5, 6 & 7 to be inputs their corresponding bits in DDRB should be cleared. the corresponding bits for pins 2, 3 & 4 should be set to make them outputs.
Code: Select all
DDRB = 0x1c;
Code: Select all
DDRB = (1 << PB2) | (1 << PB3) | (1 << PB4);
the proper way to set DDRB as you have described with a binary literal is
Code: Select all
DDRB = 0b00011100;
-
- Posts: 11
- Joined: Thu Dec 03, 2009 9:53 pm
Re: Help me create code
Ahh beautiful! thanks! and im using the standard ATtiny2313V-10PU chipmtbf0 wrote:what chip are you using?
a couple of matters of semantics.
PORTB is the port. it is eight bits wide.
those things you refer to as ports 1-8 are pins 0-7.
to set pins 0, 1, 5, 6 & 7 to be inputs their corresponding bits in DDRB should be cleared. the corresponding bits for pins 2, 3 & 4 should be set to make them outputs.
the syntax preferred in avr-gcc, though, isCode: Select all
DDRB = 0x1c;
i have what is possibly an irrational hatred of binary literals. i find them very difficult to read, which may be why i failed to notice at first that you have the bits in the reverse order. pin 0 is the low order bit of the i/o port. think of it as 2^^0. pin 7 is the high order bit and is 2^^7. these things work just like decimal numbers.Code: Select all
DDRB = (1 << PB2) | (1 << PB3) | (1 << PB4);
the proper way to set DDRB as you have described with a binary literal is
Code: Select all
DDRB = 0b00011100;
So for the input, if i wanted to do a if statement that was if pin 0 = closed would i use the same structure? so
if (PORTB = 0b00000001) or do you use something else
-
- Posts: 1645
- Joined: Sat Nov 10, 2007 12:59 am
Re: Help me create code
PORTB is used for output. PINB is used for input. so...
there are a couple of problems with your approach, even if you were to use the correct port name.
the effect of this statement is to assign the value 1 to PINB, (== is the comparison operator). an assignment statement in c evaluates to the value assigned, so this is equivalent to
which will evaluate to true, since any non-zero value is considered true in a conditional.
had you used
you would have set the internal pullup on pin 0 of PINB and the conditional would evaluate true.
had you used
you would still have problems because this statement will only evaluate true if pin 0 is high and all of the other pins are low because if, say pin 7 and pin 0 are both high, (PINB == 129).
this is why we use (PINB & (1 << PB0)). it masks the unwanted bits of the register and returns a non-zero value only if pin 0 is set.
of course,
is fine, too.
clear as mud? suggestions for further research. pull down. debouncing.
Code: Select all
if (PINB & (1 << PB0))
Code: Select all
if (PINB = 1)
Code: Select all
if (1)
had you used
Code: Select all
if (PORTB = 1)
had you used
Code: Select all
if (PINB == 1)
this is why we use (PINB & (1 << PB0)). it masks the unwanted bits of the register and returns a non-zero value only if pin 0 is set.
of course,
Code: Select all
if (PINB & 0b00000001)
clear as mud? suggestions for further research. pull down. debouncing.
-
- Posts: 11
- Joined: Thu Dec 03, 2009 9:53 pm
Re: Help me create code
Ok, well most of my code is compleat but im having problems with my delay functions. Instead i want to use the internal timer of the chip. does anyone know how i can use this? I want to be able to reset it and have a while command saying if timer is greater then or equal to like 100 milliseconds....
-
- Posts: 11
- Joined: Thu Dec 03, 2009 9:53 pm
Re: Help me create code
Ok so one least question
So the PinB & 1 << PB0 works, what is the opposite of that? so if the pin is open, not having a positive charge? Ive tried 0 << PB0 but that doesnt seem to work.
So the PinB & 1 << PB0 works, what is the opposite of that? so if the pin is open, not having a positive charge? Ive tried 0 << PB0 but that doesnt seem to work.
-
- Posts: 1645
- Joined: Sat Nov 10, 2007 12:59 am
Re: Help me create code
Code: Select all
if (!(PINB & (1 << PB0)))
Please be positive and constructive with your questions and comments.