Tuesday, May 17, 2011

A battle of epic proportions

I spent 3 days trying to figure out why the pickit3 debugger wasn't stopping at simple breakpoints and why a seemingly correct code worked sometimes and didn't most of the times. Things looked to have a probabilistic flavor and I was almost sure I would have to find the expected number of times I need to burn the same code, though that wouldn't guarantee anything! It was an epic battle. First I found some module variables weren't getting initialized to right value. Ruslan and I forced it to work in this case by manually setting those variables to required values. Then I included the 'finding the teammate' part of my code and things went as wrong as they can go only in hell- the code simply stopped executing at a particular fixed line. Certainly I had no clue and neither did the great Ed! Then I turned towards the simple Eric Wei and his amazing insights and asked him to use his brain's awesome computing power and fix it for me. We commented out the line where the code got stuck and tried to stop at the breakpt. And it worked! Then I uncommented that line and  it still worked! I promised him a treat, but Ed wasn't happy because now the problem was 'lost' and could not be reproduced. Being a follower of procedures, I did as Ed had told me- restarted the entire workstation devices, including the computer. Then I checked if the problem still persisted, and it did! I had given up and just decided to initialize my set of variables inside my Init function instead of initializing them outside during declaration. It seemed to make the debugger stop at the breakpts I set but it didn't stop at breakpts set later than a particular line, as if it were 'offset'. Then I decided to check what worked in my old code (the one with the old timer) and new code (new timer).I had always thought the new timer was causing the problems, but I realized that I had fewer state machines in the old code.  Just on a whim, I decide to remove one of the 3 parallel state machines I was using and instead code it up in an interrupt routine like I had in old code. I also ditched the debugger for the seemingly more reliable picstart plus programmer. This I tried in the checkpt2 code and it worked. I held my breath and tried it with the complete code, and it worked! Fittingly, it was an ePIC Win! The only explanation I can think of is that the compiler is not able to handle long codes even if it wasn't using its complete program memory. So eliminating one of the state machines and coding it up as an interrupt response routine reduced the number of lines of code to some acceptable value for the compiler. Bottomline, don't trust the Lite Mode Hitech Compiler for C for PICs.

~Preyas.

P.S. I still feel 'violated' as a manner of speaking because the stupid compiler was at fault and wasted precious 3 days that I could have used for something else. And boasted about in the spdl. Damn all the Murphys of the world.

Next: Battle of the Xbee, Jackie

No comments:

Post a Comment