Society of Robots - Robot Forum

Electronics => Electronics => Topic started by: cyberfish on March 11, 2010, 11:26:17 PM

Title: ATmega328P working intermittently
Post by: cyberfish on March 11, 2010, 11:26:17 PM
I am really lost now. Have been trying to solve this problem for a few days.

I built my own arduino-compatible microcontroller board (atmega328P + 16MHz crystal), and it works, sometimes.

When it's working, everything is fine. I can upload sketches (code), run code, get the right output, etc. But if I just leave it running, it will suddenly automagically stop responding completely after about 2-3 hours. Nothing on UART output, output ports become floating (I THINK, didn't try putting a load to it), doesn't respond in any way. Disconnecting and reconnecting power doesn't help, but apparently leaving it off for a few hours before reconnecting does (but it will die in another few hours).

To isolate the problem, I have stripped the hardware to the bare minimum, and wrote some simple LED blinking test code.

It blinks for about 2 hours, and just stops. I didn't even touch it.

The code:
Code: [Select]
void setup() {
    pinMode(2, OUTPUT);
}

void loop() {
  int x = 0;
  while (true) {
      digitalWrite(2, x ^= 1);
      delay(1000);
  }
}

My board setup -
RESET pulled up to 5V by 10k resistor (reset input disconnected)
AVcc = AVref = 5V (bypassed by the same 0.1uF capacitor)
AGND = 0V
Vcc = 5V (bypassed by another 0.1uF)
Rx disconnected
XTAL1 and XTAL2 to a 16MHz crystal, both legs connected to ground through 2 22pF ceramic caps

For testing, digital pin 2 is connected to an LED with a 1K resistor in series to 5V

Power supply is a wall transformer (~15V DC) to a bunch of diodes (drop the voltage to ~9V), to LM7805. Electrolytic cap on input, ceramic on output. Voltage measures 5.01V, but I have no oscilloscope so cannot check transients.

Any suggestions? Has anyone faced the same problem before?

Thanks!

EDIT: tried powering it with a battery (NiMH regulated down to 5V), no change.
Title: Re: ATmega328P working intermittently
Post by: little-c on March 12, 2010, 02:03:08 AM
heat? i.e is the micro hot? is the regulator (very)hot?

Title: Re: ATmega328P working intermittently
Post by: cyberfish on March 12, 2010, 02:16:43 AM
The MCU is cold, so is the regulator.
Title: Re: ATmega328P working intermittently
Post by: Razor Concepts on March 12, 2010, 04:39:48 AM
Just wondering, what does ^ mean? I am familiar with += and *= etc, but I have never seen ^.
Title: Re: ATmega328P working intermittently
Post by: chelmi on March 12, 2010, 08:30:37 AM
Just wondering, what does ^ mean? I am familiar with += and *= etc, but I have never seen ^.

^ is xor. He is toggling the output on and off.
Title: Re: ATmega328P working intermittently
Post by: Razor Concepts on March 12, 2010, 04:14:13 PM
Just wondering, what does ^ mean? I am familiar with += and *= etc, but I have never seen ^.

^ is xor. He is toggling the output on and off.

Are there any sites that show these different symbols and what they do? Google fails at searching for those symbols.
Title: Re: ATmega328P working intermittently
Post by: little-c on March 12, 2010, 05:06:15 PM
search "(programing language) arithmatic operators"

should find a tutorial with a massive list of operators.

alternativly if you have visual studios, F1 will bring up help, and you should be able to search for operators.

Title: Re: ATmega328P working intermittently
Post by: waltr on March 12, 2010, 07:56:28 PM
Just wondering, what does ^ mean? I am familiar with += and *= etc, but I have never seen ^.

^ is xor. He is toggling the output on and off.

Are there any sites that show these different symbols and what they do? Google fails at searching for those symbols.

The C Programming Language by Kernigham & Ritchie. The original C programming book by the guys who invented C.
Title: Re: ATmega328P working intermittently
Post by: Soeren on March 12, 2010, 11:04:07 PM
Hi,

[...] suddenly automagically stop responding completely after about 2-3 hours.
[...]Disconnecting and reconnecting power doesn't help, but apparently leaving it off for a few hours before reconnecting does (but it will die in another few hours).
EDIT: tried powering it with a battery (NiMH regulated down to 5V), no change.
My first thought was Brown Outs, but running on a battery should rule that out (as long as the batt. has power).
Is the 2..3 hours the same interval each time or random?

My second guess would be Latch Up. Try shorting the caps completely after disconnecting and then power up again

Third target, do you have an oil burner a fridge or similar that starts at the time it dies?  (Defect contacts can radiate electrical noise of quite some power). Or perhaps a transmitter in the vicinity (intermittent broadcasts, cell phone, ham radio, walkie talkie or similar).
Title: Re: ATmega328P working intermittently
Post by: cyberfish on March 13, 2010, 12:14:07 AM
Thanks for the advices!

Most of the times it runs for 2-3 hours, but I didn't actually time it, so I'm not sure. More recently, though, sometimes it would just run for a few minutes.

I first thought it was brown out, too, that's why I tested with a battery. But even if it was a brown out, shouldn't it just reset instead of freeze completely?

As for the power supply, sorry I didn't make it clear, but it's actually not part of the circuit (the regulator and 2 caps). I use it as the power supply for 2-3 of my projects on my desk (only powering this one at the moment, though). It's never turned off. I just unplugged the microcontroller circuit from the power supply.

It was 2am at night, so nothing turning on and off.

Thanks again for the suggestions!

I think the problem is with the oscillator. When it's working, I see about 0.5VAC on both legs of the crystal, and when it's not working, I see 0V and 5VDC. Any idea why?

Oh and the exact same circuit works perfectly on a breadboard (tested over 24 hours).

EDIT:
Crystal:
http://search.digikey.com/scripts/DkSearch/dksus.dll?lang=en&site=CA&WT.z_homepage_link=hp_go_button&KeyWords=300-8499&x=0&y=0 (http://search.digikey.com/scripts/DkSearch/dksus.dll?lang=en&site=CA&WT.z_homepage_link=hp_go_button&KeyWords=300-8499&x=0&y=0)

Caps:
http://parts.digikey.com/1/parts/76971-cap-22pf-50v-ceramic-c0g-5-k220j15c0gf5tl2.html (http://parts.digikey.com/1/parts/76971-cap-22pf-50v-ceramic-c0g-5-k220j15c0gf5tl2.html)

each leg of the oscillator has a cap to ground.

EDIT2:
And it's behaving very funny now. Runs for a few seconds/minutes, freezes for a few seconds/minutes, repeat... I didn't touched it at all.
Title: Re: ATmega328P working intermittently
Post by: little-c on March 13, 2010, 03:06:32 AM
change the crystal?
Title: Re: ATmega328P working intermittently
Post by: cyberfish on March 13, 2010, 03:07:16 AM
Thanks. Tried that already. Replaced the crystal and both caps.
Title: Re: ATmega328P working intermittently
Post by: cyberfish on March 14, 2010, 04:11:13 PM
I built the exact same circuit again with all new components (except the MCU), and arranged things better (no stacking components on top of each other etc).

It's working fine so far, but it has only been 3 hours. Will run it overnight and see what happens.

Watching a green LED blink at 0.5Hz has never been so exciting...