View previous topic :: View next topic |
Author |
Message |
ben_r
Joined: 26 Mar 2004 Posts: 4
|
Sleep not sleeping |
Posted: Fri Mar 26, 2004 6:18 pm |
|
|
Hi,
I'm trying to get my 18F2320 to sleep - but Im having difficulty. I have a suspicion it may be because Im using the Internal clock - but cant find any documentation to back this up.
The code in its reduced form is :
Code: |
#include <18F2320.h>
#fuses INTRC,NOWDT,NOPROTECT,NOLVP
#use delay(clock=8000000)
void main() {
setup_oscillator(OSC_8MHZ | OSC_INTRC);
// disable_interrupts(GLOBAL) ; /* this has no effect */
while (TRUE) {
/* flash the led */
output_low(PIN_B0);
delay_ms(50);
output_high(PIN_B0);
delay_ms(50);
sleep();
}
}
|
The LED just flashes, and keeps flashing though in theory the system should be asleep (one flash, then sleep)? I have tried disabling interrupts incase that was the problem (something keeps waking it up) - but it has no effect.
Is there anything obviously wrong with the code? and is it possible to sleep when using an internal oscillator?
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Mar 26, 2004 7:46 pm |
|
|
In the 18F2320 data sheet, in section 3.1.2,
it has this highlighted remark:
Quote: | Executing a SLEEP instruction does not
necessarily place the device into SLEEP
mode; executing a SLEEP instruction is
simply a trigger to place the controller into
a Power Managed mode selected by the
OSCCON register, one of which is
SLEEP mode. |
You should read that section of the data
sheet and then see if CCS has a special
function to handle it, and if not -- then
put in code to directly write to the
appropriate register. |
|
|
Guest
|
getting 18fx220 to sleep |
Posted: Sun Mar 28, 2004 5:51 pm |
|
|
My 18F4320 would not sleep and wake properly with Timer 2 running, though it works fine with only the ADC timer running. I never did solve the problem.
I don't know if this helps you, but the reason I never solved the problem is that I found that 10-bit ADC conversions are pretty steady even in the LSB, whether or not the processor is sleeping during the conversion. I am using a linear reg supply for VDD with a 0.33 uF ceramic bypass cap.
gordon |
|
|
cbstieff
Joined: 09 Aug 2004 Posts: 7 Location: Kensington, MD
|
Wish this was less complicated |
Posted: Wed Sep 08, 2004 2:34 am |
|
|
The following code is used to create 8 interupts per second from a 32KHz external crystal on Timer1. There are a lot of pieces of code required to do the job. For more information read the Data Sheet sections on Interupts and Timer1.
---Code below ---
// assuming that there is a 32KHz crystal with the proper capacitors on the Timer1 I/O pins
// for 10ths (approximately)
//#define TIMER1INIT 62300
// for 8ths (exactly)
#define TIMER1INIT 61440
#define GIE_ONLY 0xF280
#byte OSCCON = 0xFD3
...
#INT_TIMER1
void Timer_1_ISR()
{
set_timer1(TIMER1INIT); // just reinitialize the timer - the interupt will restart the program when the ISR is returns
}
...
Main()
{ // All initialization code here
...
// initialize and setup Timer1
set_timer1(TIMER1INIT);
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT); // setup external clock
// note a delay of as much as 20ms may be requird to allow the Clock to stabilize
enable_interrupts(INT_TIMER1); // enable the timer interupt
enable_interrupts(GLOBAL); // enable interupt processing
//the Timer1 ISR will now start processing 8 interupts per second wiether the CPU is in sleep mode or not
// note a delay of as much as 20ms may be requird to allow the Clock to stabilize
...
While (1) // do forever
{ // start main processing loop
// main processing loop MUST execute the code at sleeploop in less than 1/8 second or unexpected things WILL happen.
...
sleeploop:
disable_interrupts(GIE_ONLY); // Note this causes the execution to continue with the next instruction
// (after the sleep instruction rather than doing a Reset)
OSCCON |= 0x80; // set to idle mode
sleep(); // may not execute some instruction correctly when the CPU wakes up so make the first instruction a NOP
#asm ASIS
nop
#endasm
enable_interrupts(GLOBAL);
} // end main processing loop |
|
|
valemike Guest
|
|
Posted: Wed Sep 08, 2004 6:30 am |
|
|
I currently have a PIC16LF627A app:
1. A 9V battery regulated down to 3.3V. The device sleeps until the WDT times out or RB0 gets a falling edge. I use the internal 37Khz oscillator (thanks to PCM Programmer for that macro I cut and pasted from the archives)
The CCS-supplied function "sleep()" works just fine for me. However, I do not have other interrupts enabled, such as timer interrupts. I thought that these timers weren't generally supposed to run anyways while the pic is in sleep. |
|
|
|