PDA

View Full Version : 16F181: can't wake up from sleep



Navaidstech
- 8th January 2010, 03:49
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>

Gusse
- 8th January 2010, 06:18
Hi,

Enable also Global Interrupt Enable (GIE) bit.

INTCON = %10001000

BTW, what is your microcontroller type? 16F181??

BR,
-Gusse-

savnik
- 8th January 2010, 06:41
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

Acetronics2
- 8th January 2010, 08:32
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"



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

Gusse
- 8th January 2010, 09:16
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-

Navaidstech
- 8th January 2010, 22:53
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.

Archangel
- 9th January 2010, 02:17
Some lessons learnt <u>again</u> :)

That happens to you too ? Welcome to my world :D

Navaidstech
- 9th January 2010, 04:33
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?



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