Sunday, 17 July 2011

Bit Set - or not

I've been playing with a simple PWM application on PICs...

The final application will use an 8-pin PIC (the 12F675) but I chose to start code development on the 16F676, partly because I had some in the junk bin and mainly because I also had the special 20-pin PIC16F676-ICD device which allowed me to do debugging (I'm too cheap to buy the PIC12F675-ICD).

All was working FB until I tried to translate the code to the 12F675 - I had one of my two PWM signals running, but couldn't get the second to appear on the appropriate I/O line, no matter what I tried.

Here's what turned out to be the offending lines of code...


It is part of the interrupt service routine, which establishes the two (phase-opposed) PWM signals on GPIO pins 1 and 2. The signal on GPIO2 was fine, but there was nothing but a glitch on GPIO1.

Can you see anything wrong with the code? I couldn't - despite staring at it for an hour!

I assumed that I'd made a mess of setting up the GPIO port and spent ages staring at different pieces of the program and making changes just out of desperation. Everything appeared logically consistent and I just couldn't see where I'd gone wrong.

Eventually, I searched for anybody else who'd suffered the indignity of PICs behaving badly.

Fortunately some Kiwis have trodden this path before me and written it up .

It seems you can't just perform bit-wise operations (like bit-set or bit-clear) on the port of the 12F675 without errors (perhaps I should say without "unexpected results"), despite the fact that you can on other PICs - like the 16F676 on which I did my code development!

The fix is simple - don't use bit-wise manipulations...


I feel a little better that I couldn't see a logical error in the code (there wasn't one) but the fact that the 12F675 "doesn't obey its own instructions" still galls!

...-.- de m0xpd

No comments:

Post a Comment