quick question about I/O

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm a little confused about how I/O works so I made this little example but it doesn't seem to be working.

I'm connecting the anode of an LED to port B0 and the cathode to port D0 - using an ATTiny2313 and avr-gcc. From there I attempt to enable output on B0 and input on D0 - will this let current flow through?

Here is my code:

#include 

int main(void){
    while(1) {
        // Enable output on port B0
        DDRB = (1 << PB0);
        PORTB = (1 << PB0);

        // Enable input on port D0
        DDRD = ~(1 << PD0);
        PORTD = (1 << PD0);
    }

    return(0);
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No. Except it looks like pd0 pullup is enabled by setting pd0 while pd0 is an input, so you could source 'some' current if pb0 was low (very little current). You would want pd0 an output, with it 'outputting' low. Although usually you just connect the anode to +, or the cathode to -, and then use 1 port pin to turn off/on the led (also a current limiting resistor in there). If you had bicolor led's, your method would be one way to do it, though.

So, forget the output->input thinking in this case, as there is usually no current flowing on inputs (not quite true, but true enough). If you want to sink or source current, output is needed.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ahhh! I had to read it twice but that makes perfect sense =]. Thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

uidzer0 wrote:
Ahhh! I had to read it twice but that makes perfect sense =]. Thanks!

Well I had this working when using 2 LEDs in a complimentary pair. But when I tried to take this a bit further via charlieplexing I got mixed results.

I'm trying to turn on 6 LEDs with 3 ports:

(replace "Pin C" with PB0, "Pin B" with PB1, and "Pin A" with PB2)

#include 

#define LED_1 DDRB = (1 << PB1) | (1 << PB2); PORTB = (1 << PB2)
#define LED_2 DDRB = (1 << PB1) | (1 << PB2); PORTB = (1 << PB1)
#define LED_3 DDRB = (1 << PB0) | (1 << PB1); PORTB = (1 << PB1)
#define LED_4 DDRB = (1 << PB0) | (1 << PB1); PORTB = (1 << PB0)
#define LED_5 DDRB = (1 << PB0) | (1 << PB2); PORTB = (1 << PB2)
#define LED_6 DDRB = (1 << PB0) | (1 << PB2); PORTB = (1 << PB0)

int main(void){
    while(1) {
        LED_1;
        LED_2;
        LED_3;
        LED_4;
        LED_5;
        LED_6;
    }

    return(0);
}

Only LED 1 and LED 4 are turning on... any ideas?

Edit: Figured it out.. I had forgotten to set the pin not in use to be an input.

#define LED_1 DDRB = (1 << PB1) | (1 << PB2); PORTB = (1 << PB2) | (1 << PB0)
#define LED_2 DDRB = (1 << PB1) | (1 << PB2); PORTB = (1 << PB1) | (1 << PB0)
#define LED_3 DDRB = (1 << PB0) | (1 << PB1); PORTB = (1 << PB1) | (1 << PB2)
#define LED_4 DDRB = (1 << PB0) | (1 << PB1); PORTB = (1 << PB0) | (1 << PB2)
#define LED_5 DDRB = (1 << PB0) | (1 << PB2); PORTB = (1 << PB2) | (1 << PB1)
#define LED_6 DDRB = (1 << PB0) | (1 << PB2); PORTB = (1 << PB0) | (1 << PB1)