Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC18F and watchdog timer?

Status
Not open for further replies.

M3GAPL3X

Junior Member level 3
Joined
Jul 29, 2009
Messages
30
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,521
pic18f wdt

Hello guys,

I am a beginner with microcontrollers and need some advice on how to implment this power saving feature.

I need to put the 18F series PIC device to sleep and wake it up ever 20 ms to check the state of various inputs. The main reason for this is to save power. After some research, it seems the best way to implement this would be with a Watchdog Timer?

Is this correct? Any guidance in the right direction would be greatly appreciated!

I will be using the C18 MPLab compiler.
 

pic watchdog

Yes, the watchdog timer (WDT) is the way to go.
1 - Set configuration bits to have WDT fire every 20 mS
2 - When you want to go to low power mode, enable the WDT and execute the Sleep instruction. Make sure you disable any peripheral modules you don't need to have active during power down, or you won't redcuce the power consumption much.
3 - After 20 mS the DWT will fire and wake-up the PIC. The code will start executing at the instruction just after the Sleep Instruction
4 - Disable the WDT, enable any peripheral modules you need to use, and then run your application - repeat from step 2
 

    M3GAPL3X

    Points: 2
    Helpful Answer Positive Rating
watchdog timer in pic18f

don't forget! if you enable the watchdog by default (WDTEN config bit as far as I remember...) and if you are awake (well, when the pic is awake) for more than 20ms, it will reset due the watchdog... so "clear the watchdog" if you need to be alive for more than 20ms... I made this error once! (but maybe the Idea of having the watchdog enable by software, as GSMMan suggest, is more suitable...)

Also! any interrupt enabled will wake-up the pic! Instead of the WDT!!! if GIE is active, it will also enter the Interrup-routine, if not, it will just wake-up!!! (I used the INT0 to wake up on certain conditions..., dissabling the WDT or clearing it, will do the work...)
 

    M3GAPL3X

    Points: 2
    Helpful Answer Positive Rating
pic18 sleep mode

Hi guys,

I appreciate the replies! I learned something new today.

Kurenai_ryu, that was actually my original design to have an interrupt wake up the PIC; however, it was determined that checking every 20ms for any changes and incrementing a counter every 20ms until about 4 seconds is reached in the counter would best to save power. I am going to look at the WDT a little more today and might be back for some more help!

GSM Man, I am going to implement that routine today and see how it goes.

Thank you both for the reply!

Added after 5 hours 49 minutes:

I was not going to do the watchdog approach but use Timer0 for sleeping and waking up with interrupts; however, looking at the data sheet, it states that during sleep mode, Timer0 turns off and can't wake up the PIC even with the Timer0 interrupt flag as high.

Does this also occur if pic is in idle mode??
 

pic18f watchdog timer

Which Device are you using?
In general, when in sleep mode all clocks are disabled, while in Idle mode CPU clock is off but certain peripheral clocks are enabled - which ones depends on the device and the Idle mode settings.

One thing to keep in mind is that Sleep mode has the lowest current draw of any mode. Unless you need a peripheral running - such as a Real Time Clock - it's generally better to put the device to sleep rather than idle it.
 

    M3GAPL3X

    Points: 2
    Helpful Answer Positive Rating
pic18 watchdog

Hi GSM,

I will be using a PIC 18F14K50.

I was too worried that timers would be disabled during sleep modes, but the spec sheet says that only Timer0 is disabled and Timer1 can be used during sleep mode. So basically I will be using Timer1's interrupt to wake the PIC up after a certain amount of time.

Basically my design will be as follows:

After 10 ms of sleep, I will be checking various ports for certain voltages. Since this device will protect against voltages in the range of >4.5 V, I will turn off power to output if it exceeds that voltage. The voltage will be checked every 10 ms.

Also, do you think a 1 Mhz clock is suitable for this task? I can go even lower?

Thanks!
 

pic18f watchdog

well, i didn't met this pic in person :) but many PIC microcontrollers disables all clocks and peripherals while sleeping EXCEPT those slaves or asynchronous... so maybe the Timer 1 keeps working if the asynchronous oscillator is enable.... please check this issue...

also! many PICs could work with a secondary clock! (just like this asynchronous timer1 stuff) so, it could work at lower frequencies while doing boring jobs. but can get speedy when it's needed! the Timer1 asynchronous oscillator commonly is used with a 32.768 kHz crystal (so you would get a one-second count with 15 bits ) but it's said it could work up to 200KHz... maybe it could be of help...


lastly I have seen a PIC (pic18f46j50) which have an internal RTC which will not be turned off while the pic is sleeping... maybe yours have something like that, which could be useful...

hope it helps, sorry for my bad english...
 

    M3GAPL3X

    Points: 2
    Helpful Answer Positive Rating
watchdog timer pic

M3GAPL3X said:
... but the spec sheet says that only Timer0 is disabled and Timer1 can be used during sleep mode.
I'm not sure where you see that in the datasheet - it appears to me that in the Sleep state all clocks are disabled (TABLE 19-1: POWER-MANAGED MODES) - this is typical for PIC's.

M3GAPL3X said:
... Also, do you think a 1 Mhz clock is suitable for this task? I can go even lower?
The clock speed you need is dependent on how much time you need to complete your task. That's going to be based on how much code has to be executed for you app and the timer it takes to "check" your voltages.
 

    M3GAPL3X

    Points: 2
    Helpful Answer Positive Rating
timer pic18f4520 code c

Thanks for the replies guys.

In the spec sheet, it says the following:

"Entering the Sleep mode from either Run or Idle mode
does not require a clock switch. This is because no
clocks are needed once the controller has entered
Sleep. If the WDT is selected, the LFINTOSC source
will continue to operate. If the Timer1 oscillator is
enabled, it will also continue to run."


However, I think in order to use this functionality, I will need to use an external crystal to even take part of this feature.

And yes, all internal clocks are disabled when PIC is in sleep mode. Thanks GSM for that and thanks RYU for clarifying the Timer 1 clock during sleep mode.

I might have to go a different route for my design now since I can't make any changes to put an external crystal on Timer 1. I can't think of any other way to wake up the PIC from sleep mode every 10 ms without a watch dog timer.
 

pic18f clock

Why don't you want to use the watchdog timer? It's a common way to wake up the device.
 

    M3GAPL3X

    Points: 2
    Helpful Answer Positive Rating
pic18 idle mode

I will be implementing a watchdog timer in this device. I thought there might have been a better way rather than using a watchdog timer to wake up the device.

Thanks GSM!

Added after 1 hours 14 minutes:

Here is my code. Is this a good way to implement the watchdog timer?

Code:
//LFINTOSC will turn on automatically if WDT is on. In our case, WDTEN = 0 and WDTCON.SWDTEN = 1

#include <p18f14k50.h>

//pragma codes
#pragma config FOSC = IRC //enable Internal Oscillator
#pragma config WDTEN = OFF //watchdog timer will be software controlled
#pragma config WDTPS = 256//1:128 postscalar for the watchdog timer



void main()
{
	TRISC = 0x00; //set all C ports as outputs

	while(1)
	{
	WDTCONbits.SWDTEN = 1; //enable watchdog timer
	
	Sleep(); //CPU sleep. Wakeup when Watchdog overlfows
	LATC ^= 0xff; //when watchdog timer overflows, light up all LED's 
	WDTCONbits.SWDTEN = 0; //turn off watchdog timer
	}
}
 

pic18f4520 turn on internal oscillator

That looks good, but to conserse power while in sleep mode make sure you disable all peripherals.
 

    M3GAPL3X

    Points: 2
    Helpful Answer Positive Rating
pic watchdog timer

Ok will do. Thanks for all your help guys. I really learned a lot through this whole process and will probably have more questions. Here is a quick LED counter I made with a watchdog of 1 second if anyone needs help with the command

Code:
//LFINTOSC will turn on automatically if WDT is on. In our case, WDTEN = 0 and WDTCON.SWDTEN = 1

#include <p18f14k50.h>

//pragma codes
#pragma config FOSC = IRC //enable Internal Oscillator
#pragma config WDTEN = OFF //watchdog timer will be software controlled
#pragma config WDTPS = 256//1:256 postscalar for watchdog, this will give around a 1 second delay



void main()
{
	unsigned int x = 0;
	TRISC = 0x00; //set all C ports as outputs
	
	while (1)
	{
		WDTCONbits.SWDTEN = 1; //enable watchdog timer
	
		Sleep(); //sleep for 1 second
		x++; //increment variable x
	
		//check to see if variable x is under 15
		if (x <= 15)
			{
				LATC  = x;
			}
		else //if over 15, reset LATC and variable x and start over
			{
				LATC = 0;
				x = 0;
			}
		WDTCONbits.SWDTEN = 0; //disable watchdog timer
	}
}
 

incrementing count watchdog

You should move the line:
"WDTCONbits.SWDTEN = 0; //disable watchdog timer "
between the lines "SLeep();" and "x++". You want to disable the watchdog timer before executing your code. With your existing code it won't make a difference because your code will be done well before the watchdog times out. However, if you add to your code you may find the the watchdog times out before you get back to the sleep instruction. In which case the processor will be reset .
 

    M3GAPL3X

    Points: 2
    Helpful Answer Positive Rating
pic 18f

Will do on my code with a 10 ms delay. Thanks again!

Added after 23 minutes:

It sounds like that the WDT clears itself automatically when the SLEEP () instruction occurs. This is what it says in the spec sheet:


The WDT and
postscaler are cleared when any of the following events
occur: a SLEEP or CLRWDT instruction is executed,
the
IRCF bits of the OSCCON register are changed or a
clock failure has occurred

Added after 45 minutes:

Another quick question, I also want to do some ADC when PIC is sleeping. Can I run the WDT timer clock and the ADC clock (Frc) during sleep mode at the same time??
 

counting 10 secs with pic18f1320

M3GAPL3X said:
It sounds like that the WDT clears itself automatically when the SLEEP () instruction occurs. This is what it says in the spec sheet:
The WDT and postscaler are cleared when any of the following events occur: a SLEEP or CLRWDT instruction is executed
The WDT counter and postscaler are cleared, but the WDT is not disabled. That just means you have 10mS From the time you awake until it fires

M3GAPL3X said:
Another quick question, I also want to do some ADC when PIC is sleeping. Can I run the WDT timer clock and the ADC clock (Frc) during sleep mode at the same time??
Yes
 

    M3GAPL3X

    Points: 2
    Helpful Answer Positive Rating
pic18f c18 กดสวิตช์

Thanks GSM. Here is my layout of my program so far:

Configure WDT for software controlled
Use a 1:2 post-scalar
Create a global counter variable

Start Main Program
Set RC0 as an input to monitor switch using TRISC
Start forever while loop
Enable watchdog timer
Start up the AtoD which uses Frc as a clock to operate during sleep mode (turn off when done)
Go into sleep mode
Disable the watchdog timer
Check pin RC0 to see if held down, if so, increment counter, if not, counter 0
Do this forever

How is that logic??

Added after 23 minutes:

Here is my code. I need to test this as I don't have a debugger on my at the moment. This is for the AtoD conversion. Any hints or tips would be appreciated.

Code:
void AtoD(void)
{
//1. Configure Port
	//disable output pin driver for RC0, RC1 and RC3
	TRISC = 0b00001011;
	
	//configure pins as analog
	ANSEL = 0b10110000; //RC3, RC1 and RC0 set as analog pins

	
	//configure voltage references 
	
	//(positive voltage reference will come from VREF+ pin) - set to 01
	ADCON1bits.PVCFG1 = 0;
	ADCON1bits.PVCFG1 = 1;
	
	//negative voltage reference will come from VREF- pin - set to 01
	ADCON1bits.NVCFG1 = 0;
	ADCON1bits.NVCFG1 = 1;
	
	//select ADC conversion channel 
	ADCON0bits.CHS3 =0; //select RC3 bit first (0111)
	ADCON0bits.CHS2 =1;
	ADCON0bits.CHS1 =1;
	ADCON0bits.CHS0 =1;

	//select result format
	ADCON2bits.ADFM = 0; //left justified
	
	//select acquisition delay  (16 tads - 110)
	ADCON2bits.ACQT2 = 1;
	ADCON2bits.ACQT2 = 1;
	ADCON2bits.ACQT2 = 0;

	//turn on ADC module
	ADCON0bits.ADON = 1; 

//5. Start conversion by setting the GO/DONE bit
	ADCON0bits.GO_DONE = 1;  //NOTE: bit is automatically cleared by hardware when the conversion is done

	if(ADCON0bits.GO_DONE = 0) //poll GODONE flag to see if conversion is done for RC3
	{
		//CHECK TO SEE IF RESULT IS GREATER THAN A CERTAIN AMOUNT
	}
	
	ADCON0bits.ADON = 0;
}

Thanks in advance!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top