16F181: can't wake up from sleep


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2007
    Location
    Mississauga, Ontario, Canada
    Posts
    29

    Default 16F181: can't wake up from sleep

    OK guys, here is my dilemma. I've been at it for a while now and can't get the PIC to wake up from sleep. What am I don't wrong?
    The idea here is a switch connected to PORTB.5
    While the program is running, I'll press the switch and after about a second the PIC goes to sleep... no problems here.
    I want to utilize that same switch to wake it back up but I can't... here is the snippet of the code:

    <CODE>

    OSCCON = %01100000 '
    TRISA = %11111111
    TRISB = %11100000
    ADCON0 = %11000001
    ADCON1 = %10001111 '
    T1CON = $31




    MainLoop:

    if powerswitch=1 then ' Check if Power Switch Pressed
    gosub pause1sec ' Wait 1 second
    if powerswitch=1 then ' Still pressed?
    gosub pause1sec 'yes, wait one more second
    intcon=%00001000 'enable interrupt on PORTB
    @ sleep ' go to sleep wait for change on PORTB
    @ nop
    intcon=%00000000
    goto AnotherPartOfTheProgram <------ this is where I want the program to resume when the chip wakes up
    endif
    endif


    </CODE>

  2. #2
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Post

    Hi,

    Enable also Global Interrupt Enable (GIE) bit.
    Code:
    INTCON = %10001000
    BTW, what is your microcontroller type? 16F181??

    BR,
    -Gusse-
    Last edited by Gusse; - 8th January 2010 at 06:21.

  3. #3
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302


    Did you find this post helpful? Yes | No

    Default

    intcon=%00001000 'enable interrupt on PORTB

    MainLoop:
    if powerswitch=1 then ' Check if Power Switch Pressed
    gosub pause1sec ' Wait 1 second
    if powerswitch=1 then ' Still pressed?
    gosub pause1sec 'yes, wait one more second
    @ sleep ' go to sleep wait for change on PORTB
    @ nop
    intcon=%00000000 ------> change to INTCON.0 = 0
    goto AnotherPartOfTheProgram <------ this is where I want the program to resume when the chip wakes up
    endif
    endif

  4. #4
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Lightbulb

    Hi, Navaid

    1) think to reset the interrupt flag BEFORE allowing PORTB interrupt and entering sleep ...

    2) not sure ... but , generally, change on portB need to read portB to clear the " Mismatch condition " ( @ wake-up ...)

    3) enabling GIE sends you @ wake up ... to address 04 ( interrupt vector )
    disabling GIE ...... to the next address of your program ( @sleep +1)
    where a "nop" is highly recommended ( you did it fine ...).



    a little example for a PIC 18F:

    Here INT1 is the Wake-up "button"

    Code:
    Sommeil:
    
    	LCDOUT $FE,1, "!!*BYE - BYE*!!"
    
    	For I = 16 to 0	Step -1					'Animation arret
    	
    		LCDOUT 	$FE,$C0,REP "*"\I," "			'	...  4s 			
    		PAUSE 250
    		
    	NEXT I
    	
    
    
    	PORTD = %00000000			' PortD à 0
    	AlLCD = 1					' Coupure LCD
    	ADCON0.0 = 0				' Désactivation ADC
    	AlRef = 0					' Coupure Vref
    	
    	
    	T3CON.0	= 0				' arret TMR3
    	T1CON.0	= 0				' arret TMR1
    	T0CON.7 = 0				' arret TMR0
    	
    	INTCON.7 = 0			' Wakeup sans interruptions
    	
    	INTCON3.0 = 0						' reset flag INT1
    	INTCON3.3 = 1			' validation INT1 pour réveil
    
    			
    @ SLEEP
    @ Nop						' Redémarrage par mise du contact
    
    
    	INTCON3.3 = 0			'Neutralisation INT1
    	INTCON3.0 = 0			'Reset Flag INT1
    
    '*****************************************************************************
    Wakeup: 'Début du Programme - reveil du PIC / Affichage Memoire
    '*****************************************************************************
    '
    FLAGS 	= 0					' Validation Reset LCD
    PORTD	= 0
    Reveil 	= 1
    LSelect	= 0
    ....
    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  5. #5
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Post

    Quote Originally Posted by Acetronics View Post
    3) enabling GIE sends you @ wake up ... to address 04 ( interrupt vector )
    disabling GIE ...... to the next address of your program ( @sleep +1)
    where a "nop" is highly recommended ( you did it fine ...).
    Thanks Alain for this information. I have had lots of luck with my code when GIE option was enabled. Now I will disable it immediately.

    Some lessons learnt again

    BR,
    -Gusse-

  6. #6
    Join Date
    Aug 2007
    Location
    Mississauga, Ontario, Canada
    Posts
    29


    Did you find this post helpful? Yes | No

    Default

    Hi guys... thank you for the info.
    Yes, I'm using a 16F818 chip... not really my choice but rather something that my buddy chose for his paintball gun and I'm stuck writing the software for it.

    Anyway, thanks to your suggestions I'm getting close. However, what I've noticed is that on wake up the TMR1 interrupt is disabled.
    I'm curious if this is normal and I just wonder if I need to re-enable it via the PIE register. I'll try that and see what happens.

  7. #7
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Gusse View Post
    Some lessons learnt <u>again</u>
    That happens to you too ? Welcome to my world
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  8. #8
    Join Date
    Aug 2007
    Location
    Mississauga, Ontario, Canada
    Posts
    29


    Did you find this post helpful? Yes | No

    Default

    OK guys... got it going. Here is the snippet of the the code around the sleep command. Works like a charm.
    What I've done is I gave it a delay of 3 seconds from the closure of the switch to make sure PortB is undisturbed, then I took a snapshot of PortB (read it into a dummy variable), set up the interrupt and put the PIC to sleep. Once awoken, I read PortB again and cleared the flag.

    Now here is another question... I'm also running Darrel Taylor's interrupt routine based on TMR1. I use it to flash a LED if a battery is low.
    However, once the chip awakens from sleep, it would appear that this interrupt isn't working anymore (ie. no flashing).

    I've noticed the same thing happens if I try to declare INTCON=00001000 at the top of the program... does this make sense?

    Code:
        pause 3000
        dummyread=PortB
        intcon=%00001000
        @ sleep
        @ nop                       
        dummyread=PortB
        intcon.0=0

Similar Threads

  1. Won't go back to SLEEP after 1st Interrupt
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 29th June 2009, 09:00
  2. wake up from sleep
    By savnik in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 31st August 2007, 17:09
  3. 16F628A current high during sleep
    By Rubicon in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 9th October 2006, 10:21
  4. Wierd sleep issue
    By orca in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 14th March 2006, 22:06
  5. 18F2320 and sleep function
    By Brian in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 10th March 2005, 13:11

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts