C Minus Minus

Note to self: When writing programs in C that need to read ports on PIC microcontrollers, copy the port data to a local variable without trying anything fancy.

In normal, standard C, a trick like the following should work nicely…

if (PORTC & 0x08 == 0x08){ foo();}

In Hi-Tech C, however, you don’t actually seem to get the current value of PORTC by doing something like that. I’m not sure *what* you get, but it certainly isn’t the current input value. Instead, you need to use the C equivalent of speaking slowly and using small, simple words:

temp = PORTC;
temp = temp & 0x08;
if (temp == 0x08){foo();}

This is annoying, but wouldn’t be an undue burden if properly documented. What *is* annoying is having to waste half a day tracking down why the code wasn’t working correctly!

This entry was posted in C, Coding, Digital, HOW-TO, PIC Microcontrollers. Bookmark the permalink.

Leave a Reply