Saturday, May 7, 2011

Trip me once shame on you. Trip me twice shame on me.

I don't believe it. I just realized I got bogged down by the same bug twice. In both cases it took me hours to hunt down the cause. First time around it was with the PIC12F1822 last March while developing the toy traffic light.  And now with the PIC16F1827 for the kitchen timer. In both cases I was having a problem getting the MCU to sleep. And in both cases it was because I was clearing the interrupt-on-change interrupt flag bit IOCIF (which is read-only) in the INTCON register when what should be cleared is IOCxF register (where x = A or B depending on the PORT where IOC is present).

From the PIC12F1822 datasheet: "The IOCIF Flag bit is read-only and cleared when all the Interrupt-on-Change flags in the IOCAF register have been cleared by software." (DS41413B-page 91)

And from the PIC16F1827 datasheet: "The IOCIF Flag bit is read only and cleared when all the interrupt-on-change flags in the IOCBF register have been cleared by software." (DS41391D-page 86)

Because the IOCxF register remained non-zero, INTCON.IOCIF remained set and the MCU kept waking up immediately after the sleep command was executed.

This is just unbelievable. I actually completely forgot about the 12F1822 debugging nightmare I had just two months ago. And repeated the very same mistake and had to grope around for the cause all over again. My brain needs a tune up badly.

No comments:

Post a Comment