ATTiny 2313A-PU seems to execute only a few instructions

Thread Starter

janice_k

Joined Jul 16, 2013
6
Hi! I'm a programmer (x86/64 C/C++ etc) professionally, but as a hobby I've started dabbling with electronics. I have successfully assembled and interfaced USBTinyISP from Adafruit (http://learn.adafruit.com/usbtinyisp) with my computer and flashed an ATTiny 2313 controller with small LED flashing program with AVRDude.

The circuit is the simple LED flasher (http://learn.adafruit.com/usbtinyisp) consisting of 330 ohm resistor and a LED attached to pin corresponding to PD6 register. The program is also taken from the same page. I tried out different modifications of the program and it appears that changing the output pin and setting it to high or low works at the start of the program, but it seems to only happen once. IE, I can delay for 200ms and turn the LED on after that, but it won't turn off again.

My only guess is that the power-supply is faulty and provides unstable voltage. I am using a power-supply salvaged from old 90 MHz Pentium computer. Using multi-meter the power-supply seems to fluctuate at around 5.1-5.25 volts. It also makes suspicious sound each second or so and the brightness of enabled LED seems to fluctuate at the same frequency. Could this be the problem and should I try to find another power-source or is there something else that might be amiss that I should try first? I would try swapping the power-supply at once, but I don't have another one at hand and it might be a while until I can replace it...

Thank you in advance,
Janice
 

MrChips

Joined Oct 2, 2009
30,701
Since you suspect the power supply, try using a different power source.
Use a 9VDC wall adapter with a 5V regulator.
 

Thread Starter

janice_k

Joined Jul 16, 2013
6
Sorry, I managed to accidentally omit the direct link from my post, because I remembered wrongly it was on the same page. The original code that I tried was from here: http://www.ladyada.net/learn/proj1/blinky.html

It worked insofar as to turn on PD6 immediately and it was left turned on. I figured maybe I had done something wrong, so I tried to change the pin into PD4 like this:
Rich (BB code):
// define what pins the LEDs are connected to
#define LED PD4
It worked such that PD6 didn't light up the LED any more. Attaching to PD4 the LED lit it up. I noticed a slight pulsing of the LED, so I tried 990 ohm resistor for LED just to see if maybe that would do something, but it just pulsed dimmer. I'm not sure if that was a sensible idea anyway, so I put back the 330 ohm resistor.

Then I tried moving the delay to the top of while loop. And the LED seemed to turn on after around 200ms, but it still didn't turn off again:
Rich (BB code):
    while (1) {
        delay_ms(200);

        // turn on the LED for 200ms
        output_high(PORTD, LED);

        delay_ms(200);

        // now turn off the LED for another 200ms
        output_low(PORTD, LED);
  }
Then, I tried different variations for timing with both 50ms and 250ms, but the result was still the same. LED turns on after a short time, but I wasn't sure if the time was shorter or longer with different values.

Lastly I tried to see if I can delay for longer before turning the LED on and increased the delay with an extra for loop like this to avoid uint8_t overflow with delay_ms function argument:
Rich (BB code):
    while (1) {
  for(i=0; i<10; i++) {
    delay_ms(200);
          }

        // turn on the LED for 200ms
        output_high(PORTD, LED);

        delay_ms(200);

        // now turn off the LED for longer
        output_low(PORTD, LED);
  }
The LED no longer turned on even after waiting for 10 seconds.

I forgot to mention that I'm using gcc-avr of WinAVR v20100110 in combination with Code::Blocks IDE. Compilation switches I used are:
Rich (BB code):
-mmcu=attiny2313 -s -O2 -dF_CPU=8000000UL
and generating .hex file with:
Rich (BB code):
cmd /c avr-objcopy -O ihex bin\Release\AVR.exe bin\Release\AVR.hex
I found an old 7VDC wall adapter, I'm guessing that would work with a 5V voltage regulator (going to store to get it today) as well or would 9VDC adapter be a better choice?
 

Shagas

Joined May 13, 2013
804
did you define :

#define output_high(port,pin) port |= (1<<pin) and the output low?

I've only been playing with MCU's for a few days so i'm a newbie , but try see if this works:


Rich (BB code):
#include <avr/io.h>
#include <avr/delay.h>


int main(void)
{
   	DDRD = 0b11111111;
   	
   	
   	while(1)
   	{
		  PORTD = 0b00000001;
		  
		 _delay_ms(300);
		 
		  PORTD = 0b00000000; 
		  
		  _delay_ms(300);
		  
		   
    }
	   	
}
No macros , no nothing just plain and simple.

Yes you definately want a 5v regulator . Problem is that it has a 2volt dropout so your source will need to be at least 7 volts for you to get 5v out of it . I'd probably go with the 9volt DC adapter
 

Shagas

Joined May 13, 2013
804
A 7VDC adapter will probably output about 10V with no load.
Try and get a LDO (low dropout) 5V regulator.
Yes, from practice I've noticed that Wall-warts output a 30-50% higher voltage than rated ( which always pisses me off) .
Does anyone know why it is so??
Is it done on purpose or is there some other reason?
 

MrChips

Joined Oct 2, 2009
30,701
Owing to the high internal resistance of the transformer the output voltage will fall as the output current increases. The stated output voltage is supposed to be rated at full load. Hence they have to take this into account by starting out with a higher voltage with no load.
 

Shagas

Joined May 13, 2013
804
Owing to the high internal resistance of the transformer the output voltage will fall as the output current increases. The stated output voltage is supposed to be rated at full load. Hence they have to take this into account by starting out with a higher voltage with no load.
Yes I suspected it would be something like that ,
Thanks for the answer
 

Thread Starter

janice_k

Joined Jul 16, 2013
6
did you define :

#define output_high(port,pin) port |= (1<<pin) and the output low?
Yes, I defined both as:
Rich (BB code):
#define output_low(port,pin) port &= ~(1<<pin) 
#define output_high(port,pin) port |= (1<<pin)
To be honest for first test I simply copied the code from ladyada's tutorial, but it looked valid. Your code looks more bare-bones, so I'll try out your code as soon as I get home just in case. Thanks.

MrChips said:
Try and get a LDO (low dropout) 5V regulator.
Thanks, I'll try it. I wonder if my understanding is correct that the dropout voltage should be below 2V in my case and primary input voltage can be as high as it is as long as it is above 10V? The one I found matching these criteria while being in a decent price range and suitable for breadboard was lp2950-50 (http://www.ti.com/product/lp2950-50) with 380mV dropout voltage, primary input voltage 30V and fixed output voltage 5V. Would it work or am I about to do something silly?
 

Shagas

Joined May 13, 2013
804
If it is a 5 volt regulator with a dropout of 2 volts then that means that you need at least 7 volts Input voltage to get 5 volts . Anything less and the regulator would not 'regulate' properly.
With the regulator that you linked , you can use anything from 5,5 volts intput to 30 volts input . Ofc you aren't going to use 30 volts .
In fact , for regulators the lower the input voltage then the less heat it has to dissipate .
Basically if you are getting that low dropout voltage regulator then the best thing would be to use a say .... 6 volt input voltage . But it would be perfectly ok to use 7v or 10 v or 15 volts or whatever . Just don't go too high because when your MCU will be pulling current from it then the regulator will start getting very hot
 

Thread Starter

janice_k

Joined Jul 16, 2013
6
No macros , no nothing just plain and simple.
To sum the thread up, yesterday I tried the code you provided and it worked, but unfortunately just like other versions I've tried. PD0 went high and then stayed high while pulsing slightly.

So today I got a voltage regulator to replace my power supply with a wall adapter. Everything worked exactly as it should and the LED blinked nicely with every version of the code! Thanks again for the help everyone! Now off to see if I can make another circuit and program with the ATTiny to drive a stepper motor :).
 
Top