1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Program to flash memory

Discussion in 'Microcontrollers' started by elecLear78, Jul 22, 2017.

  1. elecLear78

    elecLear78 Member

    Joined:
    Jul 16, 2013
    Messages:
    227
    Likes:
    1
    Location:
    india
    I have written the following program to program flash memory with data. I have tried to simulate the code before actually flashing the code but i am not seeing the data as expected. Please help.

    Code (text):

    #include<xc.h>
    /*
     * Program to flash double word
     * family:dspic33evgm256
     */
    _FOSCSEL(FNOSC_FRC & IESO_OFF);
    _FOSC(FCKSM_CSECMD & OSCIOFNC_OFF & POSCMD_XT);
    _FWDT(FWDTEN_OFF)
    _FICD(ICS_PGD2)

    #define SET                             (1u)
    #define PM_ROW   __attribute__((space(prog), aligned(2)))
    const PM_ROW data_in_flash[10];
    unsigned int nvmAdru;
    unsigned int nvmAdr;
    unsigned int nvmAdrPageAligned;
    unsigned int tableOffset;
    unsigned int data[10]={0x1111,0x2222, 0x3333,0x4444, 0x5555, 0x6666, 0x7777, 0x8888, 0x9999, 0xaaaaa};
    unsigned int *pdata=data;
    int main(int argc , char *argv[])
    {
       
     PLLFBD=38;              // M=40
     CLKDIVbits.PLLPOST=1;   // N2=4
     CLKDIVbits.PLLPRE=0;    // N1=2

    // Initiate Clock Switch to Primary Oscillator with PLL (NOSC=0b011)
    __builtin_write_OSCCONH(0x03);
    __builtin_write_OSCCONL(OSCCON | 0x01);
    // Wait for Clock switch to occur
    while (OSCCONbits.COSC!= 0b011);
    // Wait for PLL to locke
    while (OSCCONbits.LOCK!= 1);
        /*step1  ---> Let the destination address to be loaded*/
        nvmAdru=__builtin_tblpage(&data_in_flash[0]);
       nvmAdr=__builtin_tbloffset(&data_in_flash[0]);
       NVMADRU = nvmAdru;
        NVMADR = nvmAdr;
    /*step2: Load the two words into latches*/  
        TBLPAG = 0xFA;
        tableOffset= 0x0000;
        __builtin_tblwtl(tableOffset,*pdata);
        __builtin_tblwth(tableOffset,0x00);
        tableOffset +=2;
        pdata++;
        __builtin_tblwtl(tableOffset,*pdata);
        __builtin_tblwth(tableOffset,0x00);
        /* Setup NVMCON for word programming */
         NVMCON = 0x4001;  
        /*step3: with interrupts disabled write the key sequence */
        __builtin_write_NVM();
        /* set the WR bit it will start programming operation */
        NVMCONbits.WR = SET;
        Nop();
        Nop();
        while(1);
        return 0;
    }
     
    When i simulated i got the following details in the variables section

    upload_2017-7-22_19-0-27.png

    So i am assuming that the variable data_in_flash is placed at the address 0x0103B4. Am i correct? Then i opened the program memory window and i see the following information
    upload_2017-7-22_19-5-0.png
    the data seem to match but not in proper order with what ever i am trying to write, but does not seem to be writing in the address i am expecting. Please help.
     

Share This Page