keep 2 bytes data on reset()

Discussion in 'Embedded Systems and Microcontrollers' started by bug13, Jul 20, 2016.

  1. bug13

    Thread Starter Well-Known Member

    Feb 13, 2012
    1,208
    38
    Hi guys

    I am using pic18f, are there some special register than I can use to store 2 bytes data and the values won't change after reset instruction? I really don't want to use an eeprom.

    Thanks guys!
     
  2. NorthGuy

    Active Member

    Jun 28, 2014
    604
    121
    RAM will not change during reset unless you power-cycle the PIC.
     
    atferrari and AlbertHall like this.
  3. Papabravo

    Expert

    Feb 24, 2006
    10,157
    1,795
    There should also be bits to tell you what kind of RESET there has been. You are interested in the difference between POWER ON RESET, MANUAL RESET, and WATCHDOG RESET.
     
    bug13 likes this.
  4. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    764
    And then make sure that you retrieve the data prior any initialization routines starts. Honestly have no idea if RAM, in commercial applications, is initialized in any way.
     
  5. blocco a spirale

    AAC Fanatic!

    Jun 18, 2008
    1,440
    368
    Isn't there some user EEPROM in the PIC18F....?
     
  6. dannyf

    Well-Known Member

    Sep 13, 2015
    1,828
    365
    You can write it to flash. You can write to outboard storage, ...
     
  7. Picbuster

    Member

    Dec 2, 2013
    376
    50
    simple do, using mplab xc8
    int Adress[2];
    int Data[2];
    int n=0; // read zero or 1
    // write before reset
    eeprom_write(Address[n],Data[n]);
    // read it back
    value[n]=eeprom_read(address[n]);
     
  8. bug13

    Thread Starter Well-Known Member

    Feb 13, 2012
    1,208
    38
    Not in my one, but I can use program space tho if I really want to.
     
  9. ian field

    Distinguished Member

    Oct 27, 2012
    4,415
    784
    For 2 bytes; you can use a couple of bus-wide D latch packages or shift it into a shift register.

    A supercapacitor could sustain that for a day or 2 with the power removed.
     
  10. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    Which PIC18?
    What language tool?
    Warm or cold reset?

    What you are asking is not hard to do but the method varies depending on the PIC and which assembler/compiler you are using AND whether you want to maintain the RAM during warm resets (by instruction or MCLR/) or power-on (cold) resets in which case, you have to either keep the RAM alive or keep the data in some sort of non-volatile storage.

    With warm resets:
    Assembler is easy, just don't initialize that RAM until you've examined the reset source. C is a little more involved - many compilers initialize RAM to 00h (to conform to the language requirement that uninitialized variables are 00) others recognize that that takes code and is not necessarily desirable in an embedded system so the compiler will provide an option to initialize or not. Some compilers provide a way to initialize some variables and not others.

    With hard reset from power off condition.
    The RAM will be corrupted.
    You'll have to detect dropping power and try to store what you want or better, always keep a current value in some nonvolatile storage (EEPROM, flashROM etc.) OR keep the RAM alive with some backup supply.

    What are you using?
    Good luck.
     
    Last edited: Jul 21, 2016
  11. bug13

    Thread Starter Well-Known Member

    Feb 13, 2012
    1,208
    38
    Isn't it XC8 automatically initially all variable to 0 when declare?

    • PIC18F46J50
    • XC8 - Free mode
    • RESET by RESET instruction (warm?)
    I believe XC8 initial variable to 0 when declare, but I haven't found how not to do it yet XC8. Mainly I don't know where to look. (Still in the process of reading the xc8 user guild to find what I need)
     
  12. Papabravo

    Expert

    Feb 24, 2006
    10,157
    1,795
    You have to customize the C startup file. This is usually an assembly language file that calls _main()
     
  13. NorthGuy

    Active Member

    Jun 28, 2014
    604
    121
    If you use C then all the variables are initialized. But the memory outside the C variables isn't.

    Also, if I remember correctly you can declare a variable as "persistent" to avoid initialization.
     
  14. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    True. Non-initialized variables declared as 'persistent' are not affected by the startup code in XC8. XC8 also provides facilities to save the STATUS register to be able to inspect PD and TO bits for processors that have them in the STATUS reg. See 'MAIN, RUNTIME STARTUP AND RESET' in the XC8 Compiler User's Guide (DS520053). It's section 5.10 in my old one and describes how all variables are treated at startup.

    And yes, executing the 'reset' instruction would be considered a 'warm' restart, the distinction being that at a warm restart, the RAM is not changed by the reset action. Contrast that with a cold start i.e. from a power off condition. In that case, the contents of the RAM are indeterminate. Either way, it is the job of the initialization code to set up the RAM and start the program (by jumping to main() ) with any explicitly initialized variables loaded and the rest zeroed (according to C convections) unless you explicitly tell it to leave some variables unchanged.
     
    Last edited: Jul 22, 2016
  15. Picbuster

    Member

    Dec 2, 2013
    376
    50
    The PIC18F46Jxx series do not have eeprom on board however; you can easy steal two bytes from program flash. go to microchip applications and software support. (An example should be available)
    Picbuster.
     
  16. ian field

    Distinguished Member

    Oct 27, 2012
    4,415
    784
    Some parts have EEPROM scratch pad memory which would be exactly right for this use.

    Program memory is usually flash and has a finite number of write cycles.
     
  17. Picbuster

    Member

    Dec 2, 2013
    376
    50
    You are correct about this program memory spec is100K times according to microchip.


    Picbuster
     
  18. NorthGuy

    Active Member

    Jun 28, 2014
    604
    121
    Data EEPROM is typically 100K. Flash is usually between 10K and 20K, but on some PIC18s may be as low as 100 (sic!).
     
  19. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    764
    In the newest ones?
     
  20. NorthGuy

    Active Member

    Jun 28, 2014
    604
    121
    Not in the newest ones - from some 7-8 years ago - such as PIC18F87J10.
     
    atferrari likes this.
Loading...