Help me create code

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
brianmay27
 
Posts: 11
Joined: Thu Dec 03, 2009 9:53 pm

Help me create code

Post by brianmay27 »

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

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Re: Help me create code

Post by mtbf0 »

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.

Code: Select all

  DDRB = 0x1c;
the syntax preferred in avr-gcc, though, is

Code: Select all

  DDRB = (1 << PB2) | (1 << PB3) | (1 << PB4);
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

Code: Select all

  DDRB = 0b00011100;

brianmay27
 
Posts: 11
Joined: Thu Dec 03, 2009 9:53 pm

Re: Help me create code

Post by brianmay27 »

mtbf0 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.

Code: Select all

  DDRB = 0x1c;
the syntax preferred in avr-gcc, though, is

Code: Select all

  DDRB = (1 << PB2) | (1 << PB3) | (1 << PB4);
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

Code: Select all

  DDRB = 0b00011100;
Ahh beautiful! thanks! and im using the standard ATtiny2313V-10PU chip

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

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Re: Help me create code

Post by mtbf0 »

PORTB is used for output. PINB is used for input. so...

Code: Select all

  if (PINB & (1 << PB0))
there are a couple of problems with your approach, even if you were to use the correct port name.

Code: Select all

  if (PINB = 1)
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

Code: Select all

  if (1)
which will evaluate to true, since any non-zero value is considered true in a conditional.

had you used

Code: Select all

  if (PORTB = 1)
you would have set the internal pullup on pin 0 of PINB and the conditional would evaluate true.

had you used

Code: Select all

  if (PINB == 1)
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,

Code: Select all

  if (PINB & 0b00000001)
is fine, too.

clear as mud? suggestions for further research. pull down. debouncing.

brianmay27
 
Posts: 11
Joined: Thu Dec 03, 2009 9:53 pm

Re: Help me create code

Post by brianmay27 »

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....

brianmay27
 
Posts: 11
Joined: Thu Dec 03, 2009 9:53 pm

Re: Help me create code

Post by brianmay27 »

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.

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Re: Help me create code

Post by mtbf0 »

Code: Select all

if (!(PINB & (1 << PB0)))

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”