keep 2 bytes data on reset()

Thread Starter

bug13

Joined Feb 13, 2012
2,002
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!
 

Papabravo

Joined Feb 24, 2006
21,225
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.
 

Picbuster

Joined Dec 2, 2013
1,047
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!
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]);
 

JohnInTX

Joined Jun 26, 2012
4,787
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:

Thread Starter

bug13

Joined Feb 13, 2012
2,002
RAM will not change during reset unless you power-cycle the PIC.
Isn't it XC8 automatically initially all variable to 0 when declare?

Which PIC18?
What language tool?
Warm or cold reset?
  • 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)
 

Papabravo

Joined Feb 24, 2006
21,225
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)
You have to customize the C startup file. This is usually an assembly language file that calls _main()
 

NorthGuy

Joined Jun 28, 2014
611
Isn't it XC8 automatically initially all variable to 0 when declare?
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.
 

JohnInTX

Joined Jun 26, 2012
4,787
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.
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:

Picbuster

Joined Dec 2, 2013
1,047
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.
 

ian field

Joined Oct 27, 2012
6,536
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.
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.
 
Top