PIC Microcontroller Behavior vs. Simulator – Discrepancies Explained

mplabpic

I was encountering some quite strange differences in the behaviours of my PIC16F628a microcontroller compared to the MPLAP built-in simulator.

After a long search, it turned out to be different due to the free cells' default values.

In the simulator, when accessing a memory location that has not been initialized, the W register is loaded with 0x00, whereas in the actual device, some other value may be loaded (I don't remember if it was 0xFF or 0x7F).

Is there a way to either make all unused bytes 0x00 on the device or to change the default value of the simulator to the PIC's uninitialized default value?

(It might also be that my question shows that I didn't understand some fundamental principles about PICs… In this case I would be grateful if someone would teach me. 😉 )

Best Answer

My standard 16F PIC-family startup code specifically initializes all lower-bank data memory to 0x00. This is a hangover from when I was first learning to program PIC microcontrollers but I consider this to be good programming practice and the code stays in all of my projects.

The code required to do this zeroing of the data memory is different for each PIC variant. But it's a simple loop and occupies very little code space.

The newer 16F PICs have much more data memory than the legacy parts that I still work with. I zero-out only the first one or two lowest banks of data memory.

Add a comment if you would like me to post the code that I use for the various PICs that I use.

All that said: I consider it to be bad programming practice to make a decision based upon a reading a data location that has not been initialized to some specific value by your program. This is a common cause of programming errors.