Search code examples
picxc8

PIC18F XC8 compiler - objects not initialized?


I have to use a Microchip PIC for a new project (needed high pin count on a TQFP60 package with 5V operation).

I have a huge problem, I might miss something (sorry for that in advance). IDE: MPLAB X 3.51 Compiler: XC8 1.41

The issue is that if I initialize an object to anything other than 0, it will not be initialized, and always be zero when I reach the main(); In simulator it works, and the object value is the proper one.

Simple example:

#include <xc.h>
static int x= 0x78;
void main(void) {
   while(x){
     x++;
   }
   return;
}

In simulator the x is 0x78 and the while(x) is true. BUT when I load the code to the PIC18F67K40 using PICKIT3, the x is 0.

This happening even if I do a simple sprintf, and it does nothing as the formatting text string (char array) is full of zeros. sprintf(buf,"Number is %u",x")

I can not initialize any object apart to be zero. What is going on? Any help appreciated!


Solution

  • Found the problem, The chip has an errata issues, and I got the one which is effected, strange, Farnell sells it. More strange that the compiler is not prepared for that, does not even give a warning to say to be careful!

    Errata note:

    Module: PIC18 Core

    3.1 TBLRD requires NVMREG value to point to appropriate memory The affected silicon revisions of the PIC18FXXK40 devices improperly require the NVMREG<1:0> bits in the NVMCON register to be set for TBLRD access of the various memory regions. The issue is most apparent in compiled C programs when the user defines a const type and the compiler uses TBLRD instructions to retrieve the data from program Flash memory (PFM). The issue is also apparent when the user defines an array in RAM for which the complier creates start-up code, executed before main(), that uses TBLRD instructions to initialize RAM from PFM.