Adafruit is open and shipping! Let's build back better, together!
0

ATtiny2313 "Hello, world!" question
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

ATtiny2313 "Hello, world!" question

by A. Square on Tue Feb 05, 2008 10:36 pm

My chips finally arrived today, so I am now able to play with my USBtinyISP and try programming chips directly (ie, not an Arduino) for the first time.

I built a minimalist target board for the ATtiny2313 (a la Evil Mad Scientist Labs); read (but didn't write) the fuses successfully as a test; wrote, compiled, and flashed the chip with a simple blinking LED program; and, finally, connected Vcc and /RESET to 5V, connected GND to ground, put an LED and series resistor on the right pin, and watched it blink. Hooray! It worked, first time 'round, how often does that happen? I played with the blink delay a bit, watched it change, changed the pin around to convince myself I actually was causing this to happen. All's good, except for two very small things.

The first is that, when the LED blinks "off," it's not all the way off: it's still dimly on. Why would that be happening, and how can I stop it?

The other is, when I connect the LED's anode to 5V and cathode to the uC pin, it blinks fine. When I switch it around, so the cathode is connected to ground and the anode is connected to the pin, nothing happens. As I understand it (which is clearly a misconception), it should blink either way; what am I misunderstanding here?
A. Square
 
Posts: 41
Joined: Mon Dec 17, 2007 12:51 pm

by adafruit on Wed Feb 06, 2008 1:48 am

you dont have the direction of the pin set correctly, i bet. use DDR, etc

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

by A. Square on Wed Feb 06, 2008 11:15 am

I may be wrong, but I'm pretty sure I do have the direction of the pin set correctly. Here's the code:


Code: Select all | TOGGLE FULL SIZE
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
    DDRD = (1 << 4);           /* make the LED pin an output */
   for(;;){
        char i;
        for(i = 0; i < 50; i++){
            _delay_ms(20);  /* max is 262.14 ms / F_CPU in MHz */
        }
        PORTD ^= (1 << 4);    /* toggle the LED */
    }
    return 0;               /* never reached */
}
A. Square
 
Posts: 41
Joined: Mon Dec 17, 2007 12:51 pm

by A. Square on Wed Feb 06, 2008 12:13 pm

This might have been obvious to others, but I just realized why, given the fact that the LED doesn't turn all the way off, only one polarity can work.

What must be happening is that the pin is going all the way down to 0V (or thereabouts) when it's low, such that the voltage difference across the LED/resistor circuit is 5V, and the LED lights up bright. However, in order for it to be lighting up at all when the pin is high, the pin must be at a voltage significantly less than 5V. Since the forward voltage of the LED is around 2V, the high pin state must be less than 3V to see any light at all. So, when I flip the polarity of the LED around, the LED is off when the pin is low, because both sides of the circuit are at 0V, and still off when the LED pin is high, because the high state isn't high enough to supply the forward voltage of the LED. (If I was at home, I would check all of these numbers with the multimeter, but for now it's just speculation.)

So the question is, why the heck is my uC pin high state so low?
A. Square
 
Posts: 41
Joined: Mon Dec 17, 2007 12:51 pm

by A. Square on Wed Feb 06, 2008 6:46 pm

OK, found and fixed the problem. It was a result of pure stupidity on my part, and what's worse, it's a mistake I've made several times before. However, in the interest of perhaps helping someone else, I'll swallow my pride and admit it: my Vcc was connected to a bus strip, which was not connected to anything.

I'm always surprised by how well some ICs will work with a power or ground pin floating... I wish it just didn't do anything at all, which would make it much easier to debug!
A. Square
 
Posts: 41
Joined: Mon Dec 17, 2007 12:51 pm

Please be positive and constructive with your questions and comments.