• AVR Freaks

Hot!I need an example on how to write/read from Flash Memory using XC8 with PIC16F1614

Author
castanog
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2016/04/06 11:30:01
  • Location: 0
  • Status: offline
2020/06/05 15:37:16 (permalink)
0

I need an example on how to write/read from Flash Memory using XC8 with PIC16F1614

THe data sheet has some examples in Assembly, but I have not been able to make them work.
does someone have an example using XC8 that I could use as reference?
Thank you.
#1

11 Replies Related Threads

    ric
    Super Member
    • Total Posts : 27630
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: I need an example on how to write/read from Flash Memory using XC8 with PIC16F1614 2020/06/05 15:44:51 (permalink)
    +1 (1)
    Why not post what you tried, and reveal how you tested if it worked?
     
    Many get confused thinking the memory view in the debugger is a live view. It is not, you must read back the flash from the device to see any changes made by running code, which terminates the debugging session.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    castanog
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2016/04/06 11:30:01
    • Location: 0
    • Status: offline
    Re: I need an example on how to write/read from Flash Memory using XC8 with PIC16F1614 2020/06/05 16:12:51 (permalink)
    0
    Sorry, I do not use a debugger, Just write and test on the hardware.
    My real trouble is I don't know how to properlly port the ASM code on the datasheet to XC8.

    I was hoping that I could define a global constant that I would assume would be in flash and then be able to write to it from the program to store user parameter data as if it was non volatile memory kept even in case of power failure.

    My original try was:

    unsigned int const TV_TYPE = 0xA55A; /// defined as a global constant for the project.
    #define flash_address PMADR
    #define flash_addressL PMADRL
    #define flash_addressH PMADRH

    #define flash_CFGS PMCON1bits.CFGS
    #define flash_RD PMCON1bits.RD
    #define flash_WR PMCON1bits.WR
    #define flash_FREE PMCON1bits.FREE
    #define flash_WREN PMCON1bits.WREN
    #define flash_LWLO PMCON1bits.LWLO

    #define flash_data PMDAT
    #define flash_dataL PMDATL
    #define flash_dataH PMDATH
    #define flash_PMCON2 PMCON2
    #define flash_FSR0 FSR0
     
     
    void write_tv_stb_type(unsigned short v){

      /// Note: for some reason the only bits being writen are the 14 LSB of the word
      INTERRUPT_GlobalInterruptDisable();
      //// erase flash

      flash_address = &TV_TYPE;
      flash_CFGS = 0;
      flash_FREE = 1;
      flash_WREN =1;

      flash_PMCON2= 0x55;
      flash_PMCON2= 0xAA;
      flash_WR = 1;

      asm("NOP");
      asm("NOP");

      flash_WREN =0;
    /////////////

    // write data
      flash_address = &TV_TYPE;
      flash_FSR0 = &v;
      flash_CFGS = 0;
      flash_WREN =1;
      flash_LWLO =1;

      flash_data = v;


      flash_PMCON2= 0x55;
      flash_PMCON2= 0xAA;
      flash_WR = 1;

      asm("NOP");
      asm("NOP");

      flash_LWLO =0;

      flash_PMCON2= 0x55;
      flash_PMCON2= 0xAA;
      flash_WR = 1;

      asm("NOP");
      asm("NOP");

      INTERRUPT_GlobalInterruptEnable();
    }
     
     

     
    #3
    ric
    Super Member
    • Total Posts : 27630
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: I need an example on how to write/read from Flash Memory using XC8 with PIC16F1614 2020/06/05 16:30:25 (permalink)
    +2 (2)
    castanog
    Sorry, I do not use a debugger, Just write and test on the hardware.

    Well you never explained HOW you tested, so I was trying to guess what you were doing.
     
     

    I was hoping that I could define a global constant that I would assume would be in flash and then be able to write to it from the program to store user parameter data as if it was non volatile memory kept even in case of power failure.

    Indeed you can, and the compiler will do all the work for you.
    See "5.4.5.1 EEPROM Variables" in MPLAB_XC8_C_Compiler_User_Guide_for_PIC.pdf
     
    In your code, you have a comment "Note: for some reason the only bits being writen are the 14 LSB of the word"
    So does this mean it DID write, but only 14 bits?
    If so, that is exactly what you should expect.
    Each word in a PIC16F device is only 14 bits, which is dociumented any many places in the datasheet.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    castanog
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2016/04/06 11:30:01
    • Location: 0
    • Status: offline
    Re: I need an example on how to write/read from Flash Memory using XC8 with PIC16F1614 2020/06/05 16:44:51 (permalink)
    +1 (1)
    Thank you for pointing me to the "5.4.5.1 EEPROM Variables", I'll review it right away.
    and also thank you for clarifying the issue on the 14 LSB of the word. 


    THank you for the help. Hopefully that will be it.
     
    #5
    castanog
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2016/04/06 11:30:01
    • Location: 0
    • Status: offline
    Re: I need an example on how to write/read from Flash Memory using XC8 with PIC16F1614 2020/06/22 11:41:55 (permalink)
    0
    Sorry, but I'm still having issues trying to save a variable to Flash memory.


    Does the Pic16F1614 supports Variables in EEPROM.
    If I make a declaration such as:
        eeprom unsigned int  TV_TYPE; 

    I get the error:
    MPIHTVE.c:56: error: (1250) could not find space (2 bytes) for variable _TV_TYPE

    The experiments I did before don't seem to be working, and If I use the code I tried before, now my program gets obliterated.
    So I'm trying to get back to basics. and see if I can re-write the whole "saving to flash" process.
    #6
    ric
    Super Member
    • Total Posts : 27630
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: I need an example on how to write/read from Flash Memory using XC8 with PIC16F1614 2020/06/22 13:19:38 (permalink)
    +2 (2)
    castanog
    Sorry, but I'm still having issues trying to save a variable to Flash memory.

    Sorry, I thnk I lead you astray earlier.
    The PIC16F1614 does not have any EEPROM memory, so you can't use the "eeprom" qualifier.
     
    You have no choice but to implement the read and write routines using the FLASH access hardware documented in chapter 10 of the datasheet.
     
    post edited by ric - 2020/06/22 13:23:44

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    castanog
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2016/04/06 11:30:01
    • Location: 0
    • Status: offline
    Re: I need an example on how to write/read from Flash Memory using XC8 with PIC16F1614 2020/06/22 13:35:32 (permalink)
    0
    Thank you for the clarification.
    One last favor, I'm not very familiar on the Assembly, could you please let me know the proper way to pass the address of the Constant where I plan to hold the data. For example:


    Considering the Constant:
     unsigned int const TV_TYPE;
     
    I was hoping that to read I could:
     
    flash_address = &TV_TYPE ; // #define flash_address  PMADR
    flash_CFGS = 0; // #define flash_CFGS PMCON1bits.CFGS
    flash_RD = 1; // #define flash_RD PMCON1bits.RD
    asm("NOP");
    asm("NOP");

    int result = flash_data; // #define flash_data  PMDAT
     
    But I get the warning:
     warning: (358) illegal conversion of pointer to integer
     
    And although the program does compile, It crashes after I use the write routine, Maybe it is writing where it is not supposed to.
     
    Thank you for your patience. I truly appreciate your time an expertise.
     
    #8
    ric
    Super Member
    • Total Posts : 27630
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: I need an example on how to write/read from Flash Memory using XC8 with PIC16F1614 2020/06/22 13:42:59 (permalink)
    +1 (1)
    You can't just place the constant randomly like that.
    It needs to be in a unique "page" of FLASH memory that can be safely erased without affecting the rest of your program.
    The top of FLASH memory is best for that. The "HEF" (High endurance FLASH) can be written and erased more often than normal FLASH. You need to change some compiler options to stop the compiler using all the memory.
    Sorry, can't explain further right now, can do in a few hours.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #9
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11882
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: I need an example on how to write/read from Flash Memory using XC8 with PIC16F1614 2020/06/22 13:51:49 (permalink)
    +1 (1)
    The warning is because flash_address (PMADR?) isn't a pointer to const unsigned int.


    #10
    ric
    Super Member
    • Total Posts : 27630
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: I need an example on how to write/read from Flash Memory using XC8 with PIC16F1614 2020/06/22 16:32:35 (permalink)
    +3 (3)
    I think we need to mention some of the basics of writing to FLASH.
    The erased state of a FLASH cell is "1" (not "0"), so an erased word in code memory is 0x3FFF (14 bits).
    Individual bits can be written from "1" to "0" as required, so 0x3FFF can be overwritten as any 14 bit value, but you can NOT write a "0" bit back to "1". The only way to get the bits back to "1" is to erase the FLASH, which erases an entire "row", which in your PIC is 32 words. (See section "11.3 Flash Program Memory Overview" in your datasheet).
    So, you can't just declare a variable as "const" and allow the compiler to place it in the middle of your code memory, as erasing that row will also erase other variables or part of your code!
    That's why I said it's better to reserve a block of FLASH t the top for data storage, and tell the compiler not to use it.
    For more details, see: https://microchipdeveloper.com/tip:22
     
    Another complication is, when XC8 stores variables in code memory, it only uses the lower 8 bits of each 14 bit word.
    So, an "int" variable (16 bits) is spread over two words.
    If you do need a 16 bit variable, you can do the same, spreading it over two words.
    You can tell XC8 to read it like a standard variable, by stating its address using "@" in C90 mode, or "__at" in C99 mode. Alternatively, you can just use a PM read routine to read it directly using the PM registers.
     
    Have a read of the above, then come back with any details that are still unclear.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #11
    ric
    Super Member
    • Total Posts : 27630
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: I need an example on how to write/read from Flash Memory using XC8 with PIC16F1614 2020/06/22 18:03:15 (permalink)

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #12
    Jump to:
    © 2020 APG vNext Commercial Version 4.5