CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Old PIC16F506 Reason for reset from STATUS register

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Eddy71ua



Joined: 23 Sep 2009
Posts: 61
Location: Ukraine

View user's profile Send private message Visit poster's website

Old PIC16F506 Reason for reset from STATUS register
PostPosted: Tue Jan 13, 2026 4:25 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jan 13, 2026 6:24 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue Jan 13, 2026 7:11 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jan 14, 2026 1:57 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Wed Jan 14, 2026 2:18 am     Reply with quote

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...

Rolling Eyes
dyeatman



Joined: 06 Sep 2003
Posts: 1977
Location: Norman, OK

View user's profile Send private message

PostPosted: Wed Jan 14, 2026 5:22 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jan 14, 2026 5:36 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Thu Jan 15, 2026 2:08 am     Reply with quote

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.

Confused
Ttelmah



Joined: 11 Mar 2010
Posts: 20017

View user's profile Send private message

PostPosted: Thu Jan 15, 2026 6:18 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jan 15, 2026 8:34 pm     Reply with quote

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 ??
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group