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 support@ccsinfo.com

Sleep Mode in PIC16F877

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



Joined: 26 Sep 2014
Posts: 11

View user's profile Send private message

Sleep Mode in PIC16F877
PostPosted: Fri Sep 26, 2014 7:10 am     Reply with quote

Hello everyone!
I have written subroutine for putting the PIC16F877 into sleep and the wake. I have few confusions in this program. Kindly help me
I have implemented the sleep mode in PIC16F877 can anyone tell me that how can i enable/Disable the watch dog timer in software. I have added comments where i require little help of you.
Code:

void plzsleep()
{
//output_high(penll);
//delay_ms(100);
//output_low(penll);
//output_low(penll);
//delay_ms(50);
//output_low(penll);
disable_interrupts(INT_TIMER0);
setup_wdt(wdt_ON); ///////////// kindly tell me that what is the equivalent statement for PIC16F877 for watch dog timer enable because i want to enable the watch dog timer at programming level not with fuse bits
//setup_wdt(WDT_ON);


//delay_ms(100);
while(i<1)
{
//output_low(penll);
//output_high(penll);
output_A(0x00);
output_B(0x00);
output_C(0x00);
output_D(0x00);
output_E(0x00);
delay_ms(100);
//delay_ms(1000);
//restart_wdt();
sleep();
restart_wdt();
//sleep();
//output_e(0x00);

//output_low(penll);


i++;
}
setup_wdt(WDT_OFF);//////////// Also tell me that what will be equivalent statement for disable the Watch dog timer in PIC16F877 because i want to disable the watch dog timer in programming level not with fuse bits.
//i=0;
output_low(penll);
count=0;
enable_interrupts(INT_TIMER0);
enable_interrupts(Global);
//count=800;

//output_high(penll);


}
drh



Joined: 12 Jul 2004
Posts: 192
Location: Hemet, California USA

View user's profile Send private message

PostPosted: Fri Sep 26, 2014 7:48 am     Reply with quote

Section 14.5 of the data sheet

REGISTER 14-3: WDTCON: WATCHDOG TIMER CONTROL REGISTER
U-0 U-0 U-0 R/W-0 R/W-1 R/W-0 R/W-0 R/W-0
— — — WDTPS3 WDTPS2 WDTPS1 WDTPS0 SWDTEN(1)
bit 7 bit 0
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
bit 7-5 Unimplemented: Read as ‘0’
bit 4-1 WDTPS<3:0>: Watchdog Timer Period Select bits
Bit Value = Prescale Rate
0000 = 1:32
0001 = 1:64
0010 = 1:128
0011 = 1:256
0100 = 1:512 (Reset value)
0101 = 1:1024
0110 = 1:2048
0111 = 1:4096
1000 = 1:8192
1001 = 1:16384
1010 = 1:32768
1011 = 1:65536
1100 = reserved
1101 = reserved
1110 = reserved
1111 = reserved
bit 0 SWDTEN: Software Enable or Disable the Watchdog Timer(1)
1 = WDT is turned on
0 = WDT is turned off (Reset value)
Note 1: If WDTE Configuration bit = 1, then WDT is always enabled, irrespective of this control bit. If WDTE
Configuration bit = 0, then it is possible to turn WDT on/off with this control bit.

Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Value on
POR, BOR
Value on
all other
Resets
CONFIG1(1) CPD CP MCLRE PWRTE WDTE FOSC2 FOSC1 FOSC0 — —
OPTION_REG RBPU INTEDG T0CS T0SE PSA PS2 PS1 PS0 1111 1111 1111 1111
WDTCON — — — WDTPS3 WDTPS2 WSTPS1 WDTPS0 SWDTEN ---0 1000 ---0 1000
Legend: Shaded cells are not used by the Watchdog Timer.
Note 1: See Register 14-1 for operation of all Configuration Word Register 1 bits.
_________________
David
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Sep 26, 2014 9:20 am     Reply with quote

drh wrote:
Section 14.5 of the data sheet
@drh: You didn't mention for which PIC this datasheet is, but it isn't the PIC16F877. Seems to me like the PIC16F887.

@adnan: The PIC16F877 is an older type of PIC which doesn't have the hardware feature to switch the watchdog on/off from software.
So, short answer, it is impossible to do what you want to do with this PIC.
kx9



Joined: 05 Nov 2013
Posts: 3
Location: portland OR

View user's profile Send private message

PostPosted: Fri Sep 26, 2014 12:25 pm     Reply with quote

I went and read the data sheet.
The 16F877 and 16F877A does state this PIC has a watchdog timer.
Always go back to the data sheet, it is your friend. Very Happy

Yes, this pic is going/is obsolete.
drh



Joined: 12 Jul 2004
Posts: 192
Location: Hemet, California USA

View user's profile Send private message

PostPosted: Fri Sep 26, 2014 12:36 pm     Reply with quote

ckielstra wrote:
drh wrote:
Section 14.5 of the data sheet
@drh: You didn't mention for which PIC this datasheet is, but it isn't the PIC16F877. Seems to me like the PIC16F887.

@adnan: The PIC16F877 is an older type of PIC which doesn't have the hardware feature to switch the watchdog on/off from software.
So, short answer, it is impossible to do what you want to do with this PIC.


Right you are. Must be getting old, bad eyesight, too early in the morning, or "something else". Wink

Time for the OP to upgrade to the 887.
_________________
David
Ttelmah



Joined: 11 Mar 2010
Posts: 19238

View user's profile Send private message

PostPosted: Fri Sep 26, 2014 2:15 pm     Reply with quote

You can though if you must 'simulate' a disable.

Just have a tick interrupt that runs slightly faster than your watchdog period, then code as:
Code:

int1 Wdog_On=FALSE;

#INT_TIMERx //whatever timer you use
void tick(void)
{
   if (Wdog_On==FALSE)
       restart_wdt();
}


When you want to enable the watchdog, set Wdog_On=TRUE, while to disable it set Wdog_On=FALSE.

When Wdog_On is FALSE, the watchdog will never trigger, since the tick keeps it reset.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sat Sep 27, 2014 3:21 pm     Reply with quote

Ttelmah wrote:
You can though if you must 'simulate' a disable.
Nice trick, but:
- Ttelmah's workaround is just that; it introduces extra complexity and requires an extra Timer resource.
- The PIC16F877 is old and not recommended for new designs. Sales price is around $7. The replacement PIC16F887 has the software watchdog enable and other features and is with $2.45 a lot cheaper.
- Rethink if the project really needs a watchdog, only about 1% of the projects on this forum require a watchdog. Is this such a project? In my experience I see the value of a watchdog being overrated. The way it is often implemented it doesn't solve the actual problem and either the watchdog is being triggered too often which misses the whole point, or a crucial trigger is forgotten somewhere and causes an unwanted reset.
Ttelmah



Joined: 11 Mar 2010
Posts: 19238

View user's profile Send private message

PostPosted: Sun Sep 28, 2014 12:46 am     Reply with quote

Lets take a step back here.

He is not using a watchdog!..... Smile

He is talking about using the watchdog timer, as a way of waking the chip from sleep.

This is a critical 'distinction'.

Actually using a watchdog, for it's primary purpose, is much more complex, than most people think. If (for instance), you scatter 'restart_wdt' calls through the code, you might as well not be using the watchdog at all....
The key point is that the only places where the restart should be reached, are carefully trapped locations in the code, setup, so they are _only_ reached, when everything _is_ working correctly. This is the key point Ckielstra is making....

There are lots of other caveats as well. Any use of the watchdog timer (either for waking, or as a genuine watchdog), needs to be 'aware' of just how inaccurate this is on these older chips. The nominal '18mSec' watchdog, has a tolerance, of something like 7mSec to 21+mSec....

I agree about being much better to use a more modern chip. Generally these are cheaper, in many cases have much more accurate timers, and will usually offer the ability to enable/disable in software.

Now, the 'workaround', is generally acceptable, for the 'sleep' application, since in most code, I'd expect there to be at least one timer already in use. Does anyone program without at least one 'tick' timer running!.... In fact the posted code, has a timer interrupt running, right at the point where he wants to disable the watchdog. Smile
If he 'must' use this chip, it is a way to 'solve' the requirement. Not a good way, but a useable solution to the problem as originally posed. Especially since for the 'sleep' type application, I'd expect the watchdog timeout to be set to quite a high value.

Whatever solution he uses here, I'd suggest searching to forum, for threads about power consumption during sleep. Unless the hardware is carefully setup, the consumption of other things on the board may be many times that of the PIC.
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