NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Weird behavior: Skipping loop or being stuck in it - Software or hardware?

May 12, 2010
by Frozenlock
Frozenlock's Avatar

Some weird things are happening to my MCU...

I was playing with some arrays, experimenting with int, float, char, small array, very big multidimensional .... Then some of my loops started to be skipped. I reverted my program to what it was before (with ctrl-z), and now the loop goes on and on. I can see it, because in the loop there's a command to print the loop's iteration on the LCD. Instead of stopping at 54, as it always did, it now goes beyond 300... Sometimes it even completes the loop, only to come back in it with 700000... I loaded an even older version of the program and the same problem occurs.

Does this sound familiar? A symptom of a poor connection? An overused MCU perhaps? Or might it be a software problem?

I should mention that I'm flirting with the max memory space, with a program of 13822 bytes.

Thanks!

May 13, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Frozenlock,

It is hard to believe you could have caused permanent issues to your chip via programming a wonky program. Have you tried writing a new simple program with a simple loop, see if that works correctly. My guess is there is a subtle error in your code that happened when you ctrl-z to revert to the old version of the code.

It is possible you are hitting the limits of the RAM space if you are allocating large arrays, could that be the problem? Since global arrays are held on the heap, it is possible to clobber your local stack variables which would cause strange behaviours in loops.

Humberto

May 13, 2010
by Frozenlock
Frozenlock's Avatar

Thank you as always Humberto,

If I understand correctly, the compiler doesn't check if the RAM will run out while the program is running?

It must definitively be that, I have some very large arrays, thousands of rows in length! I'll try to downsize a bit and give it another try.

May 13, 2010
by Frozenlock
Frozenlock's Avatar

AAAARG!

I think I've found it!

The program was still acting strange even when reduced by half. It's only when I isolated the problem's source to a particular array that I had my "Eureka" moment.

One of my arrays is filled with PRE-DETERMINED data. Which is to say I manually entered some numbers. Now, this isn't the first time this problem shows itself to me (Eadogm LCD).

The array is initialized all right, but none of my inputs are kept! So when I make some comparisons or mathematic operations, it's with an empty array! Of course I will have some weird behavior!

So now I'm trying to keep the data when I transfer to the MCU. I've yet to succeed, but I believe I'm on the right way.

static float trend[] PROGMEM={10.85,7.448,6.023......
May 13, 2010
by wayward
wayward's Avatar

Looks like another issue caused by the dropped .data segment. Perhaps there can be a little red box in the NerdKits guide warning people that non-PROGMEM static initializers don't work?

Or maybe there is one already, I haven't opened it recently. In that case, beg pardon. :)

Post a Reply

Please log in to post a reply.

Did you know that hobby servos are controlled through a particular kind of PWM (Pulse Width Modulation) signal? Learn more...