• AVR Freaks

Hot!PIC16f1789 EEPROM ASM-->XC8

Page: 12 > Showing page 1 of 2
Author
irmak
Starting Member
  • Total Posts : 34
  • Reward points : 0
  • Joined: 2019/07/05 23:50:12
  • Location: 0
  • Status: offline
2019/11/04 23:28:08 (permalink)
0

PIC16f1789 EEPROM ASM-->XC8

In datasheet of PIC16f1789 EEPROM read write functions are defined in page 117-118 of datasheet;
 
My question is: What is miising in XC8 code below;
 
 
void EEPROM_Byte_Write(unsigned char eepromAddress, unsigned char eepromData)
{
    unsigned char gie_Status;
    
    while(WR); // check the WR bit to see if a previous Write operation is in progress
    EEADR=eepromAddress; // Write the address to EEADR.
    EEDATA=eepromData; // load the 8-bit data value to be written in the EEDATA register.
    //
    WREN=1; // Set the WREN bit to enable eeprom operation.
    //
    gie_Status = GIE; // Copy the current Interrupt state
    GIE = 0; // Disable the interrupts
    //
    EECON2=0x55; // Execute the special instruction sequence
    EECON2=0xaa; // Refer the datasheet for more info
    //
    WR=1; // Set the WR bit to trigger the eeprom write operation.
    GIE = gie_Status; // Restore the interrupts
    WREN=0; // Disable the EepromWrite
}


unsigned char EEPROM_Byte_Read(unsigned char eepromAddress)
{
    while(RD || WR); // check the WR&RD bit to see if a RD/WR is in progress
    EEADR=eepromAddress; // Write the address to EEADR.
    RD = 1; // Set the RD bit to trigger the eeprom read operation.
    return(EEDATA); // Return the data read form eeprom.
}

 
 
post edited by irmak - 2019/11/04 23:31:38

Attached Image(s)

#1

20 Replies Related Threads

    ric
    Super Member
    • Total Posts : 24202
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/04 23:46:27 (permalink)
    0
    Are you using XC8 in FREE mode?
    If yes, it's not guaranteed to produce exactly the sequence required to unlock the EEPROM.
    What are you putting in the EEPROM?
    XC8 has direct support to place variables into EEPROM for you on PIC16F devices.
     

    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
    irmak
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2019/07/05 23:50:12
    • Location: 0
    • Status: offline
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/05 00:55:07 (permalink)
    0
    Dear Ric
     
    Yes, I am using MPLAB XC8 in free mode. 
     
    I just write a single integer/char value;
     
    =====================================
    int EEPROM_Adress=0x00;
    EEPROM_Byte_Write(EEPROM_Adress,'X');
    //
    value=EEPROM_Byte_Read(EEPROM_Adress);
    =====================================
     
    I have also look at samples at
    https://microchipdeveloper.com/faq:36
     
    but I could not succeed to over come some problems on writing and reading which sometimes occurs.

    Do you have any suggestion ?
    Is there a wirte verification method
     
     
     
    post edited by irmak - 2019/11/05 00:58:28
    #3
    ric
    Super Member
    • Total Posts : 24202
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/05 01:13:44 (permalink)
    +2 (2)
    There's a much easier way.
    Just declare a variable is in EEPROM like this:
    unsigned char __eeprom var_name;

     
    (You can use just "eeprom" in XC8 v1.xx, but you need "__eeprom" in v2.xx)
    You can then just treat it like a normal variable, and the compiler handles all the low level access. You don't need to do ANY of that extra code to read or write it.
     
     

    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
    ric
    Super Member
    • Total Posts : 24202
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/05 01:18:20 (permalink)
    +2 (2)
    FYI, the problem in FREE mode is that there are often superfluous bank select instructions in this sequence, so it does not exactly match the required assembly sequence.
        EECON2=0x55; // Execute the special instruction sequence
        EECON2=0xaa; // Refer the datasheet for more info
        //
        WR=1; // Set the WR bit to trigger the eeprom write operation.


    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!
    #5
    irmak
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2019/07/05 23:50:12
    • Location: 0
    • Status: offline
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/05 01:29:13 (permalink)
    0
    Dear Ric
     
    Many Thanks for your detailed information. Everything is clear after your explanations.
    ========================================================
    More details: XC8 Compilier Guide Page 33 
    ========================================================
     
     
     
    #6
    irmak
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2019/07/05 23:50:12
    • Location: 0
    • Status: offline
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/05 09:32:31 (permalink)
    0
    Dea RIC
     
    Is it possible to read the address of __eeprom variable ?
    #7
    1and0
    Access is Denied
    • Total Posts : 9863
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/05 10:16:34 (permalink)
    +1 (1)
    irmak
    Is it possible to read the address of __eeprom variable ?

    Don't be afraid to try it and see for yourself; e.g.
    unsigned char __eeprom var_name;
    unsigned char addr;
     
    addr = (unsigned char) &var_name;

    #8
    ric
    Super Member
    • Total Posts : 24202
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/05 12:34:02 (permalink)
    +2 (2)
    As 1and0 suggested, just try it.
    In order to give the best advise, it would be useful to know WHY you want the address. i.e. what are you going to do with it?
     

    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
    irmak
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2019/07/05 23:50:12
    • Location: 0
    • Status: offline
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/05 12:53:38 (permalink)
    0
    Many Thnaks 1ad0
     
    Dear RIC
     
    In current project I do not need address information.
     

    unsigned char __eeprom variable_set[10]

     
     works for writing array of char/int to EEPROM.
     
    But I just wonder !!!
     
    I know EEPROM is very slow but if I need something like circular memory buffer to store last session data of any encoder measurements. If is it possible to do it by using addrees information.
     
     
     
    post edited by irmak - 2019/11/05 12:57:22
    #10
    ric
    Super Member
    • Total Posts : 24202
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/05 13:21:19 (permalink)
    +2 (2)
    Yes, EEPROM writes are slow, can be 1 to 5 milliseconds per byte.
    It's pointless trying to work via address though. The write operation will take precisely the same amount of time, regardless of how you do it.
    Do you really need to store every measurement into EEPROM?
    Couldn't you keep the circular buffer in RAM, and just copy to EEPROM every now and then?
    These are engineering decisions that we can't make for you with the limited information you have supplied.
     

    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
    irmak
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2019/07/05 23:50:12
    • Location: 0
    • Status: offline
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/05 13:29:43 (permalink)
    0
    Dear Ric
     
    Many Thanks. 
     
    I just wonder, currently I do not have a concrete project for this situation. I work on biomedical signals with MATLAB. As you understand from my questions I am just a beginner of PIC MCU. When I will improve my skills I plan to design applications with DSPICS that can aquire and process biomedical signals that I interest. But this is a very long road. I feel luckly and thank to Good for your guidance.
     
    https://microchipdeveloper.com/projects:emg-signal-processing-for-embedded-applications
     
    post edited by irmak - 2019/11/05 13:31:38
    #12
    ric
    Super Member
    • Total Posts : 24202
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/05 14:36:59 (permalink)
    0
    The more you understand about the capabilities and limitations of the underlying hardware, the better you will be at using them optimally.
     
    Application libraries like MCC and Harmony can help you write correct code, but they cannot substitute for reading the PIC datasheet and understanding what you are working with.
     

    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!
    #13
    pcbbc
    Super Member
    • Total Posts : 1373
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/06 03:59:58 (permalink)
    +2 (2)
    ric
    Yes, EEPROM writes are slow, can be 1 to 5 milliseconds per byte.
    Do you really need to store every measurement into EEPROM?
    Couldn't you keep the circular buffer in RAM, and just copy to EEPROM every now and then?

    More to the point, EEPROM has a limited number of write cycles.
    So the "slowness" shouldn't really be an issue - if you are writing often enough to notice a delay, you are probably using it wrong (and will very quickly end up with faulty EEPROM due to exceeding the erase/write cycle counts).
    #14
    crosland
    Super Member
    • Total Posts : 1665
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/06 05:00:48 (permalink)
    0
    ric
    Are you using XC8 in FREE mode?
    If yes, it's not guaranteed to produce exactly the sequence required to unlock the EEPROM.
    What are you putting in the EEPROM?
     

     
    Quite reasonable, Any compiler is free to reorder instructions, etc.
     
    How do you use EEPROM on PIC18? [I've tried to post a new thread for PIC18 in the XC8 forum but keep getting the access denied error]
     
    My old legacy code that always worked in C18 is potentially unsuitable.
     
    The __eeprom qualifier is only supported for my baseline and mid-range parts. Why???
     
    The XC8 eeprom_write() function and EEPROM_WRITE macro is only supported on baseline and mid-range parts. Why???
     
    We are recommended, by the XC8 User Guide for PIC, to use MCC, which generates the same code I would always use, i.e., not guaranteed to work.
     
    So, either you are wrong about XC8 free mode or Microchip have really dropped the ball on EEPROM access and there is no safe way in PIC18/XC8 in free mode.
     
     
    #15
    pcbbc
    Super Member
    • Total Posts : 1373
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/06 05:10:11 (permalink)
    +1 (1)
    croslandQuite reasonable, Any compiler is free to reorder instructions, etc.

    At least in C99 it cannot re-order volatile variables accesses across a sequence point.
    A sequence point is defined (amongst a few other places) as a statement (terminated by a semicolon).
     
    TBH it's a fairly basic requirement of any compiler targeting an embedded system.  Order of register accesses are fairly fundamental to the operation of a lot of hardware.
     
    Still no guarantee any particular instruction sequence (i.e. no intervening register writes) will be generated though.
    #16
    1and0
    Access is Denied
    • Total Posts : 9863
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/06 09:12:00 (permalink)
    +1 (1)
    pcbbc
    croslandQuite reasonable, Any compiler is free to reorder instructions, etc.

    At least in C99 it cannot re-order volatile variables accesses across a sequence point.
    A sequence point is defined (amongst a few other places) as a statement (terminated by a semicolon).

    The problem is not the access order of the volatile register, but the insertion of banking instruction at the wrong place messing up the 4-cycle magic sequence.
    #17
    crosland
    Super Member
    • Total Posts : 1665
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/06 09:59:58 (permalink)
    0
    1and0
    The problem is not the access order of the volatile register, but the insertion of banking instruction at the wrong place messing up the 4-cycle magic sequence.

    What has become apparent is that the NVMCON registers are in the access bank and there should never be any need for superfluous bank changing code.
     
    Maybe the C code is OK after all.
    #18
    1and0
    Access is Denied
    • Total Posts : 9863
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/06 10:35:40 (permalink)
    0
    crosland
    What has become apparent is that the NVMCON registers are in the access bank and there should never be any need for superfluous bank changing code.
     
    Maybe the C code is OK after all.

    Let's move to your other thread: https://www.microchip.com/forums/FindPost/1117732
    #19
    mlp
    boots too small
    • Total Posts : 813
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: previously Microchip XC8 team
    • Status: offline
    Re: PIC16f1789 EEPROM ASM-->XC8 2019/11/07 08:41:09 (permalink)
    0
    crosland
    The __eeprom qualifier is only supported for my baseline and mid-range parts. Why???
     
    The XC8 eeprom_write() function and EEPROM_WRITE macro is only supported on baseline and mid-range parts. Why???

    Because under the hood XC8 is still separate compilers for baseline/mid-range (PICC) and high-end (PICC-18), and the eeprom qualifier was never implemented in PICC-18.
     
    The function and macro were in PICC-18 back in HI-TECH days so I can't answer your second question but it's likely "a directive came from Management (demanding something impossible) that meant PIC18 EEPROM support was sacrificed".

    Mark (this opinion available for hire)
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5