Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC and sleep command.

Status
Not open for further replies.

mramos1

Active Member
I read in the datasheet, the sleep would continue where it stopped when the WDT woke it up. I set option to xxxxx111 and it sleeps, more like 1 sec than to.. Then I put

sleep 15 times, trying to save batteries for 30 seconds. The external 32Khz osc should help, but the WDT uses an internal RC osc.

Anyway, 1 sleep or 15 sleeps, it sleeps as if one, maybe it is resetting?

Anyone have a clever ways to put the pic in low power mode and automatically wake up after 30 seconds? With nothing external to the chip?
 
mramos1 said:
I read in the datasheet, the sleep would continue where it stopped when the WDT woke it up. I set option to xxxxx111 and it sleeps, more like 1 sec than to.. Then I put

sleep 15 times, trying to save batteries for 30 seconds. The external 32Khz osc should help, but the WDT uses an internal RC osc.

Anyway, 1 sleep or 15 sleeps, it sleeps as if one, maybe it is resetting?

Anyone have a clever ways to put the pic in low power mode and automatically wake up after 30 seconds? With nothing external to the chip?

You can't just put 'sleep' x number of times, use it in a loop that counts the number of times it wakes up, and returns to 'sleep' until a certain number is exceeded.
 
it depends on which PIC? the 12F and 16F lines aren't going to work very well since they don't have the ability to run an oscillator other than WDT in sleep mode. Of course, the WDT isn't very accurate. the 18F and higher series have better power conservation features but I suspect you are using a 16F chip.

If you want precision in your sleep times with ultra low power, check out the TI msp430 line. they have the ability to use a watch crystal with a built in low power oscillator and shut down everything. iirc, it consumes nanoAmps in this mode. There is an apnote that shows how to get to multi-year run times on a 3V button cell.
 
Why sleep a particular number of times?
Generally you sleep until you have something to do. That may be a mS or an hour. If you have the WDT wake up the PIC, then you just want to check if there's anything to do and if not just go back to sleep.

As noted, you can use this as a timer, but it's a very inaccurate timer.

Make sure no other interrupt sources are waking up the PIC.
 
If you are still using the 509 then the WDT will cause a reset. Check to /PD and /TO bits of the status register - both are zero for a WDT reset after sleep.

Mike.
 
6.5 Timer1 Oscillator
A crystal oscillator circuit is built-in between pins T1OSI
(input) and T1OSO (amplifier output). It is enabled by
setting control bit, T1OSCEN (T1CON<3>). The oscillator
is a low-power oscillator, rated up to 200 kHz. It
will continue to run during Sleep
. It is primarily intended
for use with a 32 kHz crystal. Table 6-1 shows the
capacitor selection for the Timer1 oscillator.
 
I had an Atmel circuit that I am converting to PIC 12F509. I dropped the clock from 1Mhz to 32Khz (since it was only running a week as it was). I have not tested the PIC yet as I have to get the code working today.

But I would like to sleep as many times as I can to help the life. In the simulator, it looks like it resets the processor, but leaves the registers intact, so I will work with that today.
 
mramos1 said:
I had an Atmel circuit that I am converting to PIC 12F509. I dropped the clock from 1Mhz to 32Khz (since it was only running a week as it was). I have not tested the PIC yet as I have to get the code working today.

But I would like to sleep as many times as I can to help the life. In the simulator, it looks like it resets the processor, but leaves the registers intact, so I will work with that today.

What EXACTLY are you trying to do?, then we can perhaps make helpful suggestions?.
 
Tring to get the longest battery life from a 12F509, running at 32Khz and on 3 AA batteries. Gonna try without sleep for now.
 
mramos1 said:
Tring to get the longest battery life from a 12F509, running at 32Khz and on 3 AA batteries. Gonna try without sleep for now.

You seem to have a very different view of 'EXACTLY' from the one I do?, that's no use whatsoever! - from your description it's dead easy, don't connect the battery!.
 
I need a circuit to detect if a light is on and beep every 30 seconds while the light it on. I have an LDR/cap on one pin of a 12F509 and a piezo on another pin. At 1Mhz with an Atmel 1200, it ran a week. I now have this on the 12F509 running at 32Khz. Basically, I ground the LDR/cap (LDR is pullup) count the highs, and pulse a piezo if the count is higher than a set number. I can include the schematic and code if it helps.
 
Last edited:
mramos1 said:
I need a circuit to detect if a light is on and beep every 30 seconds while the light it on. I have an LDR/cap on one pin of a 12F509 and a piezo on another pin. At 1Mhz with an Atmel 1200, it ran a week. I now have this on the 12F509 running at 32Khz. Basically, I ground the LDR/cap (LDR is pullup) count the highs, and pulse a piezo if the count is higher than a set number. I can include the schematic and code if it helps.

To be honest, that sounds like you should easily do months - obviously depending how often the light is on and it's triggered.

You might like to check the EPE website, they did a freezer alarm (using the capacitor charging technique) with a 12C508 a couple of years ago, it would obviously be VERY trivial to make it light sensitive instead.

One particularly clever point of the project was the fact that it used TWO charging circuits, one from a thermistor, and one from a fixed resistor - and compared them. This means that the battery voltage doesn't affect it's accuracy - I found that really impressive!, I built one, and actually modified the code (which the magazine printed later) to drive the piezo in bridge mode, making it MUCH louder with no hardware additions!.
 
I looked, I guess would have to order the back issue to see the schematic.

I am having trouble now with the cap not staying low.. I really need an ICD2 it looks like. One thought I had, was used the LDR to interrupt the PIC and buzz and delay 30 seconds and end, and the main routine being a sleep. hehehe..

But I have this one built and want to get it running.

Here is the code.

Code:
   PROCESSOR 12F509
        __config   _MCLRE_OFF & _CP_OFF &_WDT_OFF & _LP_OSC
        list p=12f509
        #include p12f509.inc
;
#define BUZZ    GPIO,1          ; piezo buzzer pin
#define LDR     GPIO,0          ; LDR/cap pin

		org	0
		goto start
count   equ     08                ; how many highs when cap is discharging
dcount  equ     09                ; delay counter
lcount  equ     0a                ; loop counter to read LDR
scount  equ     0b                ; loop counter to read LDR
cntmsec	equ		0c 
cntmsec1 equ	0d

.code
start
		
top
        movlw   b'00000000'     ; set buzzer and LDR as output
        tris    GPIO            ;
		bcf		BUZZ			; kill buzzer
        bcf     LDR             ; ground out LDR/cap
		movlw	d'100'			; for 100 msec 
		call	mdelay
        movlw   b'00000001'     ; flip LDR for input
        tris    GPIO            ;
        clrf    count           ; zero counters, count = number of highs from RC, lcount is passes to check
		movlw	d'255'			;do 255 checks
		movwf	lcount
loop
        btfsc   LDR             ; if bit is high
        incf    count,f         ; increment count
        decfsz  lcount,f        ; decrement loop counter
        goto    loop            ; if loop counter <> 0 loop    

		movlw	d'120'
		subwf	count,f

        btfsc   count,7         ; test bit 7 if more than 128 highs
        goto    buzz            ; if true go buzz
nap
		movlw	d'84'
		movwf	scount
nloop
		movlw	d'255'
		call	mdelay
		decfsz	scount,f
		goto	nloop
		goto	top
	
buzz
        movlw	h'a0'
		movwf	lcount
noise
        bsf     BUZZ            ; set piezo high
		call    msecdelay          ; pause
        bcf     BUZZ            ; clr piezo
		call    msecdelay          ; pause
        decfsz  lcount,f        ; if lcount is <> 0
        goto    noise           ; then more noise
        goto    nap             ; else to nap
;
msecdelay   movlw h'1'      	   ; allow for 4 microsec overhead.. 
			  movwf	cntmsec1
micro4        decfsz	cntmsec1,1 ; subtract 1 from countmsec
              GOTO micro4          ; more loops
              retlw		0

mdelay        movwf cntmsec       ; W to msec count register
			  clrwdt
msecloop      call msecdelay      ; 248 * 4 + 2 = 994 here
              decfsz cntmsec, f   ; decrement count, skip when zero
              goto msecloop       ; more loops
              retlw		0
		;
        end
 
Try posting the circuit you're using, so we can see what you're doing.

I have the original EPE issue somewhere, I'll try and find it for you - but it's really VERY simple.
 
Thanks Nigel.. LED and diode are gone, using 4.5VDC of AA batteries. LED was to blink my LDR count. But it appears I
get 255 on the LDR at the 32Khz. New cap size tonight.
 

Attachments

  • new4.jpg
    new4.jpg
    47.2 KB · Views: 743
mramos1 said:
Thanks Nigel.. LED and diode are gone, using 4.5VDC of AA batteries. LED was to blink my LDR count. But it appears I
get 255 on the LDR at the 32Khz. New cap size tonight.

Well I'm glad the LED is gone, as you don't show a series resistor with it?.

You also don't show a supply decoupling capacitor either?.
 
As you want to maximise battery life, may I suggest that you remove your capacitor and turn your MCLR pin into an input. Connect your new input to the junction of your LDR and your preset.

You can now save lots of power by making GP0 high and stopping any current flowing through the LDR/resistor combo. You can momentarily make GP0 low and read GP3 (MCLR) to check for light/dark. You will probably have to change the value of your preset to get the right transition level.

Mike.
Edit typo
 
Last edited:
Pommie said:
As you want to maximise battery life, may I suggest that you remove your capacitor and turn your MCLR pin into an input. Connect your new input to the junction of your LDR and your preset.

You can now save lots of power by making GP0 high and stopping any current flowing through the LDR/resistor combo. You can momentarily make GP0 low and read GP3 (MCLR) to check for light/dark. You will probably have to change the value of your preset to get the right transition level.
Edit typo

I think I am with you. Make a voltage divider that I can power and then test the junction, then remove power from the LDR and sleep 30.. That is great.

So use MCLR at input to read the junction, ground preset resistor, and LDR to GP? to power the voltage divider? Might be fun to find the trip level, but I like removing power from the LDR for that 30 seconds for sure.

Nigel? LED was a thought that never made it. On the CAP you mean for decoupling?
 
OK, the EPE Freezer alarm was in the May 2002 issue, I've scanned the circuit and attached it!.

As you can see it's VERY simple, you could replace the thermistor with your LDR - it does two readings, one of the thermistor, then one of the variable resistor - it then simply compares the values. This compensates for any changes caused by battery or temperature fluctuations, as they would affect both readings (with the obvious exception of the thermistor itself). In between readings it goes to sleep, and is woken by the WDT.
 

Attachments

  • freezer_alarm.gif
    freezer_alarm.gif
    113.2 KB · Views: 1,203
WOW.. That is cool.. So they set high on one termistor, ground junction, and count. Then go high on POT, ground junction and count? Subtracting thermistor from POT and that is the value?

I am lost on it, I see the logic with the two to compensate the for battery getting lower, just not the sequence to do it.

Something to play with for sure. I guess the 12C508/9 would handle 6V also, the F is 5.5 volts, why I went with 4.5 and no diode.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top