Monday, 26 May 2014

Killing bugs

Whilst not being able to build up the linear amplifier as I had planned, I decided to do a little update to the firmware. Whilst the frequency control and display were all working nicely as designed, I had yet to write the code to allow the set to remember the last frequency before turning off. This is a little annoying since the default is set to 14.000MHz requiring a good tune up the band to get to the SSB segments.

The PIC16f877 is a very capable device and incorporates non-volitile memory. I had yet to acquire the necessary knowledge to write to the memory area of this device. The first part of getting the feature to work though should have been straight forward.

Rule number one - Nothing is straight forward.

Having decided on the method that would be used to save the frequency, by simply detecting a change of the frequency and then waiting for a couple of seconds of no buttons being pressed or knobs turned, I wrote code to toggle the LED back light to represent what would later be the NV RAM write. With that done, I located the necessary areas that would hold off the write - namely the routines that dtetect button presses and knob turns, and inserted code just to reset the countdown counter. The countdown being triggered by the change of frequency. This would ensure that the write to NV RAM wouldn't interfere with the fluid graphics.

Once done a quick test of the rest of the functions and I would be ready to move on to learning the skills to write to NV RAM.

The first hurdle
It seems that somehow, my PIC would not allow itself to be programmed. For some reason, each time I tried, it would throw an error when verifying the rewrite. At £3 each, it can get expensive, I had one do the same to me previously and I managed to pop one during the "de-demonising" stage. Nothing for it, I had to insert a new one.

The second hurdle
All programmed up and the back light flashing nicely after turning the dial and waiting a couple of seconds. But wait, why wouldn't the volume control work? Something wrong with my new code? Hmmm

After a lengthy read through the associated code, I could see nothing I might have done that would stop the volume control from working! Nothing! I decided to flash the chip with the previous version that I knew to work and see if there might be something wrong with the new chip. Backing out of a change is always a good idea if you experience issues. With the new chip flashed with the previous version of the code the volume control still didn't want to work! Hmmm a faulty digipot perhaps?  Easily tested by putting the previous chip back in ( although it wouldn't accept a reprogramming, it still worked with the previous code). It worked!!?????

It took me a full day of debugging, sniffing and scoping the outputs to the digipot until I finaly found the fault. If you ever do any programming, always always always, declare your variables and then initialise them!!!! Most languages force you to declare a variable and unless you initialise it to something else, default the value to zero or null. Not so the PIC. I program in assembler, it allows me greater control of the hardware. The problem in this world of binary and hex is that you get exactly what you ask for but If you don't ask, you get what the PIC offers and that can be anything that was left in there by either your program or when it was manufactured. In my case a particular variable I had thought was at 0x00 was in fact at 0x07, simply because I hadn't initialised it. In the previous chip it had worked simply because that location had been written to by a zero in the past. A simple single line of code to correct it and I can now move on. A setback of a whole day, just because I didn't follow a rule I was taught in programming 101. Builder beware.

No comments:

Post a Comment