Hot!Array Initialization issue with PIC18F47K40

Author
shivanandP
New Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2016/12/20 06:02:09
  • Location: 0
  • Status: offline
2016/12/25 23:27:08 (permalink)
0

Array Initialization issue with PIC18F47K40

I am facing this array initialization issue with PIC18F47k40. When i am initializing array with assigning values like
unsigned int array[] = {0,1,10,100,1000}; //All the values of array elements taking 0x0000 I saw with debug in watch and variable window.
If i assign like
unsigned int array[0] = 0;
unsigned int array[1] = 1;
unsigned int array[2] = 10;
unsigned int array[3] = 100;
unsigned int array[4] = 1000;
its working fine, but i need the array with initialization because i am using pointer to character array in my application.
But same code is working with PIC18f46k80, PIC18F4620 and PIC18f46k22 i tried.
I am attaching my code please if any help possible do the needfull.

I tried with global, static and const also but no luck.
#1

9 Replies Related Threads

    timijk
    Super Member
    • Total Posts : 1215
    • Reward points : 0
    • Joined: 2007/11/26 00:30:07
    • Location: Taiwan
    • Status: online
    Re: Array Initialization issue with PIC18F47K40 2016/12/26 00:31:55 (permalink)
    +1 (1)
    what's your MPLAB X IDE version?  I just tried it on v3.50, it looks OK.
    #2
    qhb
    Superb Member
    • Total Posts : 6262
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Array Initialization issue with PIC18F47K40 2016/12/26 02:21:04 (permalink)
    0 (2)
    shivanandP
    I am facing this array initialization issue with PIC18F47k40. When i am initializing array with assigning values like
    unsigned int array[] = {0,1,10,100,1000}; //All the values of array elements taking 0x0000 I saw with debug in watch and variable window.

    This proves nothing if your code doesn't actually USE the data yet.
    Optimising compilers are free to throw away any code which doesn't do anything useful.
     
    #3
    shivanandP
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2016/12/20 06:02:09
    • Location: 0
    • Status: offline
    Re: Array Initialization issue with PIC18F47K40 2016/12/26 02:32:43 (permalink)
    0
    I am also using same MPLabX v3.50. Is my same code is working or any changes you have done? like header files or different initialisation?
    #4
    timijk
    Super Member
    • Total Posts : 1215
    • Reward points : 0
    • Joined: 2007/11/26 00:30:07
    • Location: Taiwan
    • Status: online
    Re: Array Initialization issue with PIC18F47K40 2016/12/26 03:21:07 (permalink)
    +1 (1)
    just very simple,
    #include <xc.h>
    unsigned int array[] = {0,1,10,100,1000};
    void main()
    {
       array[0]=1;
       while(1){}
    }

    compiler is XC8 1.38.
    #5
    qhb
    Superb Member
    • Total Posts : 6262
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Array Initialization issue with PIC18F47K40 2016/12/26 03:42:02 (permalink)
    +1 (1)
    Try this
    #include <xc.h>
    unsigned int array[] = {0,1,10,100,1000};

    void main()
    {
        array[0]=1;
        {
            volatile int dummy;
            unsigned char idx;
            for (idx=0; idx < 5; idx++)
                dummy = array[idx];    //this will always be done because dummy is "volatile"
        }
       while(1){}
    }

     
    #6
    davekw7x
    Entropy++
    • Total Posts : 1391
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Left Coast, USA
    • Status: offline
    Re: Array Initialization issue with PIC18F47K40 2016/12/26 10:01:27 (permalink)
    +5 (5)
    There is an important erratum for all chips in the 'K40 family.
     
    With XC8 version 1.38, you need the workaround shown in Section 3.1 of the Errata document DS80000712B
     
    That is:
     
    Create a new file, powerup.as, in your project.  (But: See edited information below.)
     
    Here's what I put in that file:
        ;
        ;
        ; From Errata document DS80000712B for the PIC168F27K40
        ;
        ; From davekw7x: This workaround is still necessary
        ; as of XC8 version 1.40  (December 14, 2016)
        ;
        #include <xc.inc>
        
        global    powerup,start
        psect    powerup,class=CODE,delta=1,reloc=2
    powerup:
        bsf NVMCON1, 7;  From errata document DS80000712B
        goto    start
        end

     
    Without this fix, global initialization won't work, and a lot of other stuff won't work.  (Anything that accesses data in program memory, like printf(), for example)
     
     
    Note:
     [/Begin Edit]
    Release notes for XC8 version 1.40 indicate that the following should fix it, but it didn't work for me:
     
    Add the following command-line option to the project Properties->XC8 Global Options->XC8 compiler->Additional Options line
     
    --ERRATA=NVMREG
     
    So I am still using the powerup.as  fix as shown in the Errata, even for XC8 version 1.40.
     
    The proper way to do it with XC8 version 1.40 and later is the following:
    • Enter +nvmreg in the Project Properties XC8 global options->XC8 Linker->Additional Options->Errata box
    That is, it's a Linker option, not a Compiler option.
     
    Bottom line: With recent compiler versions you don't need  powerup.as  any more.  (Huzzah!)
    [/End Edit]
     
    Finally,
    The Errata document indicates that is not necessary for silicon revision A4, but I don't have any such chips to test.
     
    Bottom line:
    Tested on my MPLABX version 3.50 PIC18F27K40 project with XC8 version 1.38 and version 1.40.  Both in "Free" mode.  Note that I did not test with simulator or debugger or anything other than bare-metal hardware and software: Real chip running with production release at full speed,  using printf() to verify memory contents and other functionality.
     
     
    Regards,
     
    Dave
    post edited by davekw7x - 2017/06/26 07:49:48

    Sometimes I just can't help myself...
    #7
    1and0
    Access is Denied
    • Total Posts : 7836
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Array Initialization issue with PIC18F47K40 2016/12/26 10:33:09 (permalink)
    +1 (1)
    davekw7x
    There is an important erratum for all chips in the 'K40 family.

    That comes up recently: http://www.microchip.com/forums/FindPost/964223
    #8
    shivanandP
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2016/12/20 06:02:09
    • Location: 0
    • Status: offline
    Re: Array Initialization issue with PIC18F47K40 2017/01/03 06:05:57 (permalink)
    0
    Please can you share your source code for initialization of PIC18F47K40.
    #9
    shivanandP
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2016/12/20 06:02:09
    • Location: 0
    • Status: offline
    Re: Array Initialization issue with PIC18F47K40 2017/06/26 03:46:29 (permalink)
    +1 (1)
    Finally Array initialization is fixed..
     
    #10
    Jump to:
    © 2018 APG vNext Commercial Version 4.5