View previous topic :: View next topic |
Author |
Message |
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
Bad Code Causes Reset |
Posted: Thu Nov 10, 2005 4:40 pm |
|
|
Hello All,
I've been chasing my tail for hours. I've narrowed down my problem to a #defined value that is causing problems. I won't include the code at this point because it's quite spaghetti'ish...
But I do know that the end result of my problem is a reset. My question is: What are the errors in code that could cause a reset? I don't have WDT enabled. I'm not doing any memory manipulation. The only thing I am doing that I can imagine causing the problem is working with an array that I remove a value from and repack.
Suggestions?
Thanks,
John |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 10, 2005 4:45 pm |
|
|
Quote: | My question is: What are the errors in code that could cause a reset? |
1. You could be writing beyond the limits of the array, and
trashing other variables that are used by the compiler.
2. If you're using #separate directive, the compiler won't automatically
limit you to the 8 stack locations of the 16F series PICs, and you
could be using 9 or more locations. This will cause a crash. |
|
|
Ttelmah Guest
|
|
Posted: Thu Nov 10, 2005 4:51 pm |
|
|
Stack overflow.
An array index that is too large, on a constant array in a 12/16 chip (on these, constant arrays are accessed by adding the value to the program counter, and jumping to a 'RETLW' instruction). Jumping too far, could result in a jump anywhere...
'Repacking' an array, implies a RAM array, and using either the indirect register, or table read/write. Obviously this has the potential to damage other RAM memory entries if the values are incorrect, which could then lead to unexpected code flows.
An interrupt, without protection on the table registers (using #int_global or #fast), resulting in the array pointer becoming corrupted.
There are some "erratas" that apply to table read accesses on specific chips.
Remember also that #define can result in 'unexpected' results, if the value is included in arithmetic. Bracketing round the value, ensures that the macro expansion should be what is expected.
I'd start by checking the erratas for your chip, and adding tests for the values used to access the array.
Best Wishes |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Thu Nov 10, 2005 6:39 pm |
|
|
Well,
I found one problem:
Code: | array_index = rand() - 1; |
I brain-farted and forgot that rand() can return 0.
Still, after that, I couldn't track down the offending code. Will try something else for now.
Thanks for those pointers. That was exactly what I was looking for.
John
EDIT:
No arrays, no problems! The KISS method works again. |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Fri Nov 11, 2005 11:24 am |
|
|
I thought I had it fixed....
It must be a stack problem. As I increase a value past a point the problem appears.
Is there a way in software to monitor what's happening on the stack in software? i.e. can I read the memory locations and print them out to see if that is really the problem?
Thanks,
John |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Fri Nov 11, 2005 1:51 pm |
|
|
Aaaaarrrrrrrrrgggggghhhhhhhhh!
I SHOULD HAVE KNOWN!
The code I was worried about was used to control some LED controllers. As the number of LEDs being illuminated got too high, when I enabled them it caused a droop in Vcc.... And when would that cause a reset?
I had BOR28 on a 3.3V chip....
Only 2 days wasted on that one!
Thanks again for the pointers.
John |
|
|
Ttelmah Guest
|
|
Posted: Fri Nov 11, 2005 3:33 pm |
|
|
Not exactly a 'code' error, but a bit of a 'classic'. The phrase 'Aaargh'!, seems to cover it... :-)
Best Wishes |
|
|
|