PDA

View Full Version : Need help with SLEEP mode



aggie007
- 27th November 2007, 04:24
Hi,

I have a PIC 18F4525 connected to a LCD screen (16X2). When I turn on the power, I want the PIC to be in SLEEP mode forever until a button, which is connected to one of the pins of the PIC, is pressed.

Once the button is pressed, the PIC will perform some operation and display the desired result on the LCD screen for 2 minutes.

After displaying the result for 2 minutes, I want the PIC to go back into SLEEP mode....until the button is pressed again.

I am using "@ SLEEP" command to make the PIC go into SLEEP mode forever. Is this right ???

Please help me with this code.



DEFINE OSC 8 .................................................. ... 'Set the frequency of the PIC

'******Setting up the LCD display******

DEFINE LCD_DREG PORTB ................................ 'Set LCD Data port
DEFINE LCD_DBIT 4 ................................ 'Set starting Data bit
DEFINE LCD_RSREG PORTB ................................ 'Set LCD Register Select port
DEFINE LCD_RSBIT 0 ................................ 'Set LCD Register Select pin
DEFINE LCD_EREG PORTB ................................ 'Set LCD Enable port
DEFINE LCD_EBIT 1 ................................ 'Set LCD Enable pin
DEFINE LCD_BITS 4 ................................ 'Set LCD in 8 bit data mode
DEFINE LCD_LINES 2 ................................ 'Set number of lines on LCD
DEFINE LCD_COMMANDUS 2000 ................................ 'Set command delay time in us
DEFINE LCD_DATAUS 50 ................................ 'Set data delay time in us


@_CONFIG_CONFIG1H_OSC_INTIO67_1H

OSCCON = %01110010 ................................ 'Set the frequency of internal osc as 8 MHz
TRISB=0 ................................ 'Set Port B as output

b0 var byte
b0=100


LCDOut $FE, 1
PAUSE 500 ................................ 'Wait for LCD to initialize


pic_sleep:


IF PORTD.7=1 THEN ................................ 'Button is connected to Port D pin 7

LCDOut $FE, 1
LCDOut " LCD TEST " ................................ 'Display line1
LCDOut $FE, $C0 ................................ 'Move to line 2
LCDOut " b0 = ", DEC b0 ................................ 'Display b0 on LCD
Pause 10000 ................................ 'Display the result for 10 seconds
LCDOut " " ................................ 'Make the LCD screen blank



ELSE
@ SLEEP ................................ 'If button is not pressed, PIC should continue to be in SLEEP mode and the LCD screen should remain blank

LCDOut $FE, 1
'LCDOUT " "
'LCDOut $FE, $C0
'LCDOUT " "

ENDIF
GOTO pic_sleep

END

Jerson
- 27th November 2007, 08:54
My feeling is your code won't work ( I cant say for sure since I havent used the 4525 yet ). However, point to note is that the PIC has to be waken out of sleep to check the button you specified on PortD.7 In your application, I suggest you look at the 'interrupt on change' function on portb. When your button gets pressed, the sleep terminates and you perform the activity you desire. Then again ------ sleep.

mister_e
- 28th November 2007, 01:09
you could still turn the internal osc to 32KHz (OSCCON) and poll the switches. Not sure of the pro/cons of this against SLEEP...

Acetronics2
- 28th November 2007, 09:56
Hi,

Did you had a look here ...???

http://www.picbasic.co.uk/forum/showthread.php?t=7540

You'll find ALL that you need ... and more !!!

Note for the "sleep at power on " you'll need FIRST to config LCD and processor ( without any LCD activating ) before entering a REAL sleep mode ...

user won't see anything "alive" ... but your processor will have to work a bit at power on !!!

Alain