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.

Using SLEEP

Status
Not open for further replies.

Peter_wadley

New Member
Im trying to make my PIC16F629 go into the SLEEP mode.. but cant seem to do it...

I have the INT on gpio2 set to wake it up from sleep..

here is the code to make it sleep..

Loop
bsf GPIO,1
bcf GPIO,1
movlw d'255'
call delay
movlw d'255'
call delay
SLEEP
goto Loop

basically I want it to pulse pin 1 .. then goto sleep then wake up when the INT is made .. do it again then back to sleep.
 
Are you processing the interrupt request or clearing the interrupt bit (bcf INTCON,INTF) after the sleep instruction. If the bit is not cleared it will cause it to wake immediately.

Mike.
 
GPIO2 interupt will set the INTF flag You don't appear to be clearing the flag in the INTCON register in the code example you show. It will wake from sleep if the flag is set, since you're not clearing it, it wakes up immediately after it executes the sleep instruction.
 
Here is the code I am using..

I clear the GP2 flag at then end of the interrupt.. but it still just keep pulsing (a 4017 decade counter) .. when I interrupt gp2 the pulsing stops.. and an LED on gp4 blinks on and off.. then it goes back..

Id like the pulsing of the 4017 to be once-stop-int-once-stop

HTML:
;**********************************************************************
list      p=12f629
#include <p12f629.inc>
errorlevel  -302
__CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT 
;**********************************************************************
;Variables
;**********************************************************************		

cblock 0x22
count
count1
counta
countb
counter
w_temp
status_temp
endc

;**********************************************************************
;Start
;**********************************************************************
ORG     0x000             ; processor reset vector
goto    Main              ; go to beginning of program

ORG     0x004             ; interrupt vector location
movwf   w_temp            ; save off current W register contents
movf	STATUS,w          ; move status register into W register
movwf	status_temp       ; save off contents of STATUS register

;Int Code:
movlw d'255'
call delay
movlw d'255'
call delay
movlw d'255'
call delay
movlw d'255'
call delay
bsf GPIO,4
movlw d'255'
call delay
movlw d'255'
call delay
movlw d'255'
call delay
movlw d'255'
call delay
bcf GPIO,4

movf    status_temp,w     ; retrieve copy of STATUS register
movwf	STATUS            ; restore pre-isr STATUS register contents
swapf   w_temp,f
swapf   w_temp,w          ; restore pre-isr W register contents
bcf INTCON,INTF			  ;clear GPIO2 flag
retfie                    ; return from interrupt

; these first 4 instructions are not required if the internal oscillator is not used
Main
call    0x3FF             ; retrieve factory calibration value
bsf     STATUS,RP0        ; set file register bank to 1 
movwf   OSCCAL            ; update register with factory cal value 
bcf     STATUS,RP0        ; set file register bank to 0

movlw	0x07		;turn comparators off
movwf	CMCON

bsf	STATUS,RP0
movlw	b'00000100'	;GPIO 2 is input for INT
movwf	TRISIO
bcf	STATUS,RP0

;INT Setup:
bsf INTCON,GIE
bsf INTCON,INTE

clrf GPIO


Loop
bsf GPIO,1
bcf GPIO,1
movlw d'255'
call delay
movlw d'255'
call delay
SLEEP
bcf INTCON,INTF
goto	Loop


;**********************************************************************
;Delay Routines
;**********************************************************************
delay       movwf    count1 
d1          movlw    0xC7        ;delay 1mS 
            movwf    counta 
            movlw    0x01 
            movwf    countb 
delay_0 
           	decfsz    counta,f 
          	goto    $+2 
           	decfsz    countb,f 
           	goto    delay_0 

           	decfsz    count1,f 
           	goto    d1 
           	retlw    0x00 

end
 
I've never figured out why people use a tiny 8pin PIC when a larger PIC would do the job better. Stop using glue logic, the chips are slowly disappearing and in most cases unnessary.
A 16f628a could do a cd4017 johnson counter in software
 
blueroomelectronics said:
I've never figured out why people use a tiny 8pin PIC when a larger PIC would do the job better. Stop using glue logic, the chips are slowly disappearing and in most cases unnessary.
A 16f628a could do a cd4017 johnson counter in software

I completely agree, what an utterly pointless idea?.
 
blueroomelectronics & Nigel Goodwin

Please post your predetermined PIC beliefs somewhere else.

What does me using the 12F629 have anything to do with this? Its a PIC! Im sure the SLEEP code is not much different from the 628a.

I got this 12F629 for free with my programmer. And Im trying to expand my programmer knowledge... GEEZ for every question you answer on this site you have a 50/50 chance of getting the answer or getting a lecture.
 
blueroomelectronics said:
I've never figured out why people use a tiny 8pin PIC when a larger PIC would do the job better. Stop using glue logic, the chips are slowly disappearing and in most cases unnessary.
A 16f628a could do a cd4017 johnson counter in software
Bill, I'm shocked. Are you taking lessons from Gramo?
 
LOL, I guess I'm not crazy about glue logic. I didn't say he HAD to use it, it's just amazing how much glue logic is still out there because people don't choose the right chip for the job.
I'm going to the Microchip 16bit seminar in Toronto June 7th. I'll suggest they don't need those silly big chips with lots of I/O

And Peter, you write good looking code, better than most I've seen posted. I'll take a look at it tonight.
 
Peter,

You seem to have two lots of code doing the same thing. One in the main loop and one in the ISR. I suggest trying to get it working without interrupts. Delete the line bsf INTCON,GIE and see if that fixes the problem.

Can I also suggest swapping your main loop around slightly.
Code:
Loop
	bsf GPIO,1
	movlw d'255'
	call delay
	bcf GPIO,1
	movlw d'255'
	call delay
	bcf INTCON,INTF
	SLEEP
	goto	Loop

This way the delay will also act as a debounce for your push button. It also outputs a longer pulse.

Mike.
BTW, you delay looks to be around 200mS.
 
Observations, you should not do this...
Code:
         bsf GPIO,1
        bcf GPIO,1
instead do this
Code:
         bsf GPIO,1
        nop
        bcf GPIO,1
Strange things can happen to a PIC when you write to the same port twice in a row. It's called read/modify/write.

Of course Pommies code has that fixed.

How are you pulsing GPIO,2 ? pushbutton?
 
Last edited:
Code:
;**********************************************************************
list      p=12f629
#include <p12f629.inc>
errorlevel  -302
__CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT 
;**********************************************************************
;Variables
;**********************************************************************		

cblock 0x22
count
count1
counta
countb
counter
w_temp
status_temp
endc

;**********************************************************************
;Start
;**********************************************************************
ORG     0x000             ; processor reset vector
goto    Main              ; go to beginning of program

ORG     0x004             ; interrupt vector location
movwf   w_temp            ; save off current W register contents
movf	STATUS,w          ; move status register into W register
movwf	status_temp       ; save off contents of STATUS register

;Int Code:
movlw d'255'
call delay
movlw d'255'
call delay
movlw d'255'
call delay
movlw d'255'
call delay
bsf GPIO,4
movlw d'255'
call delay
movlw d'255'
call delay
movlw d'255'
call delay
movlw d'255'
call delay
bcf GPIO,4

movf    status_temp,w     ; retrieve copy of STATUS register
movwf	STATUS            ; restore pre-isr STATUS register contents
swapf   w_temp,f
swapf   w_temp,w          ; restore pre-isr W register contents
bcf INTCON,INTF			  ;clear GPIO2 flag
retfie                    ; return from interrupt

; these first 4 instructions are not required if the internal oscillator is not used
Main
call    0x3FF             ; retrieve factory calibration value
bsf     STATUS,RP0        ; set file register bank to 1 
movwf   OSCCAL            ; update register with factory cal value 
bcf     STATUS,RP0        ; set file register bank to 0

movlw	0x07		;turn comparators off
movwf	CMCON

bsf	STATUS,RP0
movlw	b'00000100'	;GPIO 2 is input for INT
movwf	TRISIO
bcf	STATUS,RP0

;INT Setup:
bsf INTCON,GIE
bsf INTCON,INTE

clrf GPIO


Loop
bsf GPIO,1
movlw d'255'
call delay
bcf GPIO,1
movlw d'255'
call delay
bcf INTCON,INTF
SLEEP
goto	Loop


;**********************************************************************
;Delay Routines
;**********************************************************************
delay       movwf    count1 
d1          movlw    0xC7        ;delay 1mS 
            movwf    counta 
            movlw    0x01 
            movwf    countb 
delay_0 
           	decfsz    counta,f 
          	goto    $+2 
           	decfsz    countb,f 
           	goto    delay_0 

           	decfsz    count1,f 
           	goto    d1 
           	retlw    0x00 

end

Great,

Here is the code which is now working.

Thanks Blue and Pommie for your help.
 
Status
Not open for further replies.

Latest threads

Back
Top