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

Discussion in 'Embedded Systems and Microcontrollers' started by janice_k, Jul 16, 2013.

  1. janice_k

    Thread Starter New Member

    Jul 16, 2013
    6
    0
    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
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,362
    Since you suspect the power supply, try using a different power source.
    Use a 9VDC wall adapter with a 5V regulator.
     
  3. janice_k

    Thread Starter New Member

    Jul 16, 2013
    6
    0
    Thank you, I'll try that.
     
  4. Shagas

    Active Member

    May 13, 2013
    802
    74
    Link the exact code that you have programmed
     
  5. janice_k

    Thread Starter New Member

    Jul 16, 2013
    6
    0
    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:
    Code ( (Unknown Language)):
    1.  
    2. // define what pins the LEDs are connected to
    3. #define LED PD4
    4.  
    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:
    Code ( (Unknown Language)):
    1.  
    2.     while (1) {
    3.         delay_ms(200);
    4.  
    5.         // turn on the LED for 200ms
    6.         output_high(PORTD, LED);
    7.  
    8.         delay_ms(200);
    9.  
    10.         // now turn off the LED for another 200ms
    11.         output_low(PORTD, LED);
    12.   }
    13.  
    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:
    Code ( (Unknown Language)):
    1.  
    2.     while (1) {
    3.   for(i=0; i<10; i++) {
    4.     delay_ms(200);
    5.           }
    6.  
    7.         // turn on the LED for 200ms
    8.         output_high(PORTD, LED);
    9.  
    10.         delay_ms(200);
    11.  
    12.         // now turn off the LED for longer
    13.         output_low(PORTD, LED);
    14.   }
    15.  
    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:
    Code ( (Unknown Language)):
    1.  
    2. -mmcu=attiny2313 -s -O2 -dF_CPU=8000000UL
    3.  
    and generating .hex file with:
    Code ( (Unknown Language)):
    1.  
    2. cmd /c avr-objcopy -O ihex bin\Release\AVR.exe bin\Release\AVR.hex
    3.  
    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?
     
  6. Shagas

    Active Member

    May 13, 2013
    802
    74
    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:


    Code ( (Unknown Language)):
    1.  
    2. #include <avr/io.h>
    3. #include <avr/delay.h>
    4.  
    5.  
    6. int main(void)
    7. {
    8.     DDRD = 0b11111111;
    9.    
    10.    
    11.     while(1)
    12.     {
    13.           PORTD = 0b00000001;
    14.          
    15.          _delay_ms(300);
    16.          
    17.           PORTD = 0b00000000;
    18.          
    19.           _delay_ms(300);
    20.          
    21.            
    22.     }
    23.        
    24. }
    25.  
    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
     
  7. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,362
    A 7VDC adapter will probably output about 10V with no load.
    Try and get a LDO (low dropout) 5V regulator.
     
  8. Shagas

    Active Member

    May 13, 2013
    802
    74
    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?
     
  9. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,362
    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.
     
  10. Shagas

    Active Member

    May 13, 2013
    802
    74
    Yes I suspected it would be something like that ,
    Thanks for the answer
     
  11. janice_k

    Thread Starter New Member

    Jul 16, 2013
    6
    0
    Yes, I defined both as:
    Code ( (Unknown Language)):
    1.  
    2. #define output_low(port,pin) port &= ~(1<<pin)
    3. #define output_high(port,pin) port |= (1<<pin)
    4.  
    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.

    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?
     
  12. Shagas

    Active Member

    May 13, 2013
    802
    74
    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
     
  13. janice_k

    Thread Starter New Member

    Jul 16, 2013
    6
    0
    Wonderful, thank you so much, it makes sense.
     
  14. janice_k

    Thread Starter New Member

    Jul 16, 2013
    6
    0
    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 :).
     
Loading...