Data direction set to output PB2 doesn't work attiny2313

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

Hello, I have been scouring the datasheet and searching the forums, but haven't found the answer. Maybe someone can help.

DDRB |= 1<<2;
PORTB |= 1<<2;

This just turns on the pull-up resistor, it won't light an LED through a 1k resistor. It's as if the data direction bit setting is ignored.

DDRD |= 1<<6;
PORTD |= 1<<6;

works fine for PD6, lights an LED no problem.

I'm guessing an alternate function of this pin is overriding the DDR, but I having trouble finding the answer.

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

Quote:
I'm guessing an alternate function of this pin is overriding the DDR

Only if you are using Timer0 with the COM0Ax bits set.

Regards,
Steve A.

The Board helps those that help themselves.

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

Quote:
alternate function of this pin is overriding the DDR,
...or you have a dead pin...

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I started out writing code to make PWM output on OC0A. After I failed to get PWM output to show on PB2, I ended up making just the tiny bit of code above for diagnostic purposes, nothing in the timer0 control registers set.

I did try another, new micro, and same result. Both from same batch, so both might be bad?
For now, crude redirect solution, that works:

	if (TCNT0 < OCR0A){PORTD |= (1<<6);} //poll timer
	if (TCNT0 > OCR0A){PORTD &=~ (1<<6);}

I'm just wondering if I'm missing a small detail, like on the atmega16, trying to use certain pins as inputs without disabling the jtag with the fuse bit.

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

From your initial post

Quote:
This just turns on the pull-up resistor, it won't light an LED through a 1k resistor.

I presume you have an external pull up resistor. By definition a pullup resistor will have one end connected to Vdd and when you write a 1 the output will still be 1. If you want it to go low[\b] write a [b]0.

Similarly , if the LED(& limiting resistor) connected to Vdd, it requires an active LO to turn the LED on.

I apologize if I am telling you how to suck eggs, but you have not clarified exactly what you expected to happen to the pullup resistor or specified exactly how the LED is wired up.

If the resistor you mentioned is connected to Vss(ground) then it is a pull-down resistor

I suggest you toggle the pin

    DDRB |= 1<<2; 
    while(1)
    { 
        PORTB |= 1<<2;
        PORTB &=~(1<<2);
    } 

and check with a logic probe or CRO before writing your chips off!

BTW ATTiny2313 does not support JTAG!

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

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

Quote:

I suggest you toggle the pin
Code:

DDRB |= 1<<2;
while(1)
{
PORTB |= 1<<2;
PORTB &=~(1<<2);
}

and check with a logic probe or CRO before writing your chips off!

It only shows pulse on the logic probe with an external pulldown (10k I used) In other words it acts like an input: toggling 2 states, low Z or internal pullup.

Quote:

BTW ATTiny2313 does not support JTAG!

Yes, I know, that was an example of something from another instance/micro that can be a pitfall if you are not aware.

I'm ordering more chips, will test those, and I'll post the results.

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

1. There is no need for a external resistor on the as the lowimpedance output will drive the pin to Vdd or Vss. Pull-up & pull-down resistors really only apply to inputs. Either lecturers don't explain this or students just don't grasp it.
When there is a resistor on an output it is really a load resistor. This is required on some devices that have an open collector/open drain output stage.

In the case of PB2 no such resistor is required. Get rid of it!

2

Quote:
it acts like an input: toggling 2 states, low Z or internal pullup.

How can an input toggle between two states with no external input? Surely the output driver is doing that!

What you suggest, indicates that it is working fine!

3. You have not explained your LED wiring.

Ballast resistor should be about 180 Ohm.
You can have active high or active low output, just make sure the LED is wired the right way. LED's run without a ballast resistor do not work for very long.

4. I very much doubt if your chips are crook! But if they are, then you did something horribly wrong. Getting more chips & plugging them would probably blow them up as well and give you false result for your test.

5. Purchase a copy of "The Art of Electronic Design" by Horowitz & Hill.

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

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

Quote:

When there is a resistor on an output it is really a load resistor.
Yes, I meant load resistor.

Quote:

In the case of PB2 no such resistor is required. Get rid of it!
I'd like to, but then no pulse is detected bu the logic probe on PB2

Quote:

How can an input toggle between two states with no external input? Surely the output driver is doing that!


From page 50 of the datasheet:
Quote:
If PORTxn is written logic one when the pin is configured as an input pin, the pull-up resistor is activated.

I think even though the pin should be an output ( DDRB |= 1<<2; ),in this case the internal pull-up is just being toggled, and the output driver is disconnected or broken.
Quote:
3. You have not explained your LED wiring.
PB2---------LED------1k resistor-----gnd.
(LED cathode connected to resistor.)
This setup works fine on other pins: example:
DDRD |= 1<<6;
PORTD |= 1<<6; 

turns the led, connected as above, but connected to PD6.
I chose 1k ballast resistor to keep the LED not too bright, it's rated 5000mcd @20 ma, so it's just the right brightness for my eyes as an indicator with a 1k resistor.

Quote:
4. I very much doubt if your chips are crook! But if they are, then you did something horribly wrong. Getting more chips & plugging them would probably blow them up as well and give you false result for your test.

Could give a false result, but if a new chip works, then question answered, and if not, only out $2 attiny2313's are very affordable.

Quote:
5. Purchase a copy of "The Art of Electronic Design" by Horowitz & Hill.

OK.

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

Quote:
From page 50 of the datasheet:
Quote:
If PORTxn is written logic one when the pin is configured as an input pin, the pull-up resistor is activated.

Where the critical statement is when the pin is configured as an input pin,
You have configured it as an output pin by writing 1 to DDB2, so writing to PORTB activates the pin directly without the need of any load resistor(or internal pull resistor) In fact Fig 22 & Table 22 of the data sheet suggests that the internal pullup is actually disabled(same as writing 0 to PUDxn).

Anyway, try the other pins on PORTB and make really ..really sure that no initialization of any PWM /Timer0 registers remain especially affecting TCCR0A. Surely all of PORTB can't be faulty.

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

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

well.....

I would say start from scratch to make sure that nothing went soure....

#include 
int main(void)
{
 ddrb |= (1<<pb2);
 portb |= (1<<pb2);
 for(;;);
}

this should make the led go on and stay on if it is connected as said.

so basically go back to scratch. If that works then there is something wrong with the controller otherwise you have to blame the software. I also assume that no other electronics is connected to the controller...

regards

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

Your previous code for PWM might configure the pin for output compare output. Make sure it does not.

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

I agree with MESLOMP. Here is the code that I have tested and it works fine on PB2 on a ETT-jrAVRTiny2313 board. I have made PB2 toggle at 1 second, interval so that you can check it with a logic probe or your LED.
NOTE. There are no external resistors on PB2 (other then the LED ballast resistor of course)

//test PB2 output on ETT jrAVr-Tiny2313 board
//8Mhz. Xtal
#include 
#include  
int main(void) 
{ 
 	DDRB |= (1<<PB2); 
 	for(;;)
 	{
		PORTB |= (1<<PB2);
 		_delay_ms(500);
		PORTB &= ~(1<<PB2);
		_delay_ms(500); 
 	} 
}

Using the toggle function when writing to PINB, the following code will do the same.

//test PB2 output on ETT jrAVr-Tiny2313 board
//8Mhz. Xtal
#include 
#include  
int main(void) 
{ 
 	DDRB |= (1<<PB2); 
 
 	while(1)
 	{
		PINB=(1<<PB2);  //toggle PB2
		_delay_ms(500); 
 	} 
}

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

Last Edited: Mon. Sep 14, 2009 - 11:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, I'm going to do that, totally fresh code, and no extra electronics, exactly as Lee has posted I found some at90s2313's I can try too, until some more tiny2313's arrive. for this test, should work the same, yes? Will post results.
Thanks much for all the help.

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

Quote:
for this test, should work the same, yes?
Remember that there is no internal oscillator with the older chips so you need to provide a clock.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

n81641 wrote:
Ok, I'm going to do that, totally fresh code, and no extra electronics, exactly as Lee has posted I found some at90s2313's I can try too, until some more tiny2313's arrive. for this test, should work the same, yes? Will post results.
Thanks much for all the help.
`

you should try it with the tiny2313.... not with an older/other chip. You want to make sure that your tiny works and not that the other chip is working.
If you write this program to flash and make sure that the fuse bits are as you want them to be then this program should work.

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

sorry for long delay, I was out of town.

Yes, new attiny2313's work fine as outputs on PB2. seems the other 2 had bad pins. They did have the same date code, so they may have been damaged together. I'm not even sure they were new, they have been in my supply for several years.