| View previous topic :: View next topic |
| Author |
Message |
Eddy71ua
Joined: 23 Sep 2009 Posts: 61 Location: Ukraine
|
| Old PIC16F506 Reason for reset from STATUS register |
Posted: Tue Jan 13, 2026 4:25 am |
|
|
I am reading the datasheet for this controller, and it describes how to read the STATUS register assembler commands.
State of the TO and PD bits to understand what woke the controller from sleep mode.
What simple C commands can be used to do this? |
|
 |
dyeatman
Joined: 06 Sep 2003 Posts: 1977 Location: Norman, OK
|
|
Posted: Tue Jan 13, 2026 6:24 am |
|
|
Googling for: "ccs picc get reset reason" you get the following:
Determine the reset cause using the built-in function restart_cause().
Usage: Call restart_cause() as early as possible in your main() function, as subsequent operations can change the returned status bits.
Returned Values: The function returns a value indicating the reason for the restart. Check the CCS C user manual for the specific defines for your PIC model, but common reasons include:
WDT_FROM_SLEEP or WDT_TIMEOUT: Watchdog timer expired.
MCLR_FROM_RUN or NORMAL_POWER_UP: External Master Clear (MCLR) pin reset or a normal power-on event.
BROWNOUT_RESE: A brownout (power supply voltage dip) occurred.
STACK_UNDERFLOW or STACK_OVERFLOW: A software issue related to the program stack.
CODE_GUARD_RESE: A security violation reset
Example Code:
#include <16F877A.h> // Include appropriate header for your PIC
#fuses NOWDT, HS, NOBROWNOUT, NOLVP // Example fuses
#use delay(clock=20000000)
void main() {
int reason = restart_cause();
// Use the value of 'reason' to log or handle the specific reset event
if (reason == WDT_TIMEOUT) {
// Handle WDT reset
}
// ... other checks
} _________________ Google and Forum Search are some of your best tools |
|
 |
Eddy71ua
Joined: 23 Sep 2009 Posts: 61 Location: Ukraine
|
|
Posted: Tue Jan 13, 2026 7:11 am |
|
|
Thank you.
But I have another question: when the controller wakes up from pressing the button, how should the flags be set in the STATUS register? |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20017
|
|
Posted: Wed Jan 14, 2026 1:57 am |
|
|
What button?????.
Depends what this does.
If this is not a 'restart', but a wake using an interrupt for example,
the status won't be affected. |
|
 |
Eddy71ua
Joined: 23 Sep 2009 Posts: 61 Location: Ukraine
|
|
Posted: Wed Jan 14, 2026 2:18 am |
|
|
I put the controller to sleep and allow it to wake up when the status of port B lines changes.
I have already read in the datasheet that when the controller wakes up due to a change in the status of the lines, the RBWUF bit will be set to 1 in the STATUS register.
It remains to be understood how to check this without assembler inserts in the code...
 |
|
 |
dyeatman
Joined: 06 Sep 2003 Posts: 1977 Location: Norman, OK
|
|
Posted: Wed Jan 14, 2026 5:22 am |
|
|
What I would do is print out the variable reason after the wake-up to
see what the code is. Then, if not already defined in the header file, define it
myself as a constant just like the other reason codes. _________________ Google and Forum Search are some of your best tools |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20017
|
|
Posted: Wed Jan 14, 2026 5:36 am |
|
|
OK.
If you have an RBWUF bit, you have to be talking a really old baseline
chip like the 12F508. Why?. The more modern chips are a lot easier to
use and usually cheaper.
If so you will have to enable wake from sleep with the setup_wdt
call, and have suitable pullups on the pin.
Just setup a #byte to register 3, then test bit 7 of this.
You will run out of space really quickly if you are on a chip like the 508.
| Code: |
#byte STATUS=getenv("SFR:STATUS")
void main(void)
{
if (bit_test(STATUS,7))
{
//Here code for wake up on B change
}
//Other code
|
On most chips, you could use:
#bit RBWUF=getenv("BIT:RBWUF")
and just test this. However on your chip this isn't supported.
You can use:
| Code: |
#byte STATUS=getenv("SFR:STATUS")
#bit RBWUF=STATUS.7
//
//Then
if (RBWUF)
{
//Here code for wake up on B change
}
|
|
|
 |
Eddy71ua
Joined: 23 Sep 2009 Posts: 61 Location: Ukraine
|
|
Posted: Thu Jan 15, 2026 2:08 am |
|
|
In the forum thread title, I mentioned that I am using an old PIC16F506. I have a ready-made device, which is very simple. However, a friend asked me to add a power button to turn the device on and off. That is what I am trying to do now, without replacing the microcontroller.
 |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20017
|
|
Posted: Thu Jan 15, 2026 6:18 am |
|
|
It is a foul little chip, and is now so easy to replace with once that works
much better, that is it honestly worth wasting the time to do this?.
The basic design is over 25 years old!.... The original versions were 'C'
(so OTP), but the 'F' versions only changed the memory used, and kept
all the problems. These appeared about 15 years ago.
Problem is that the memory space is so small that you may well find
yourself running out of ROM. Your approach will have to be to read restart_cause, and the top bt as I show, then in the main code you will
have to add the setups to configure the CONFIG register to support the
wake up from pin change (this is done by setup_wdt).
You will also have to change all the configuration of the registers so
they are left uninitialised at boot, and add this into the main code so
it doesn't happen on the reboot. I can see just adding this adding perhaps
a total of about 50 instructions to the code, hence the worry about the
ROM size..... |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9614 Location: Greensville,Ontario
|
|
Posted: Thu Jan 15, 2026 8:34 pm |
|
|
great Mr. T make me feel old ! Pretty sure I have 1/2 tube of 508s in the same drawer as the 16C71s and 16C84s. Pretty chips, quartz windows on top, 15 minutes to erase, LOTS of 'coffee breaks'..... good old dayze !!!
agree there has to be a drop in replacement, heck do they still sell 508s ?? |
|
 |
|