CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

18F45K22 Sleep and WDT wakeup

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
allredsd



Joined: 02 Jun 2011
Posts: 4
Location: Austin, Texas

View user's profile Send private message

18F45K22 Sleep and WDT wakeup
PostPosted: Tue Mar 13, 2012 10:54 am     Reply with quote

I'm trying to put my 18F45K22 to sleep and have it wake up by WDT to save current when it's idle. I can't get it to work, so I've written a simple test program as provided below. I have no external crystal and am using the internal HF oscillator.

The fuses are set with this code:
Code:

//#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale (4ms Resolution) = 0.512 Seconds
//#FUSES WDT256                   // Watch Dog Timer uses 1:256 Postscale (4ms Resolution) = 1 Seconds
//#FUSES WDT512                   // Watch Dog Timer uses 1:512 Postscale (4ms Resolution) = 2 Seconds
#FUSES WDT32768                   // Watch Dog Timer uses 1:1024 Postscale (4ms Resolution) = 4 Seconds
//#FUSES NOWDT                    //No Watch Dog Timer
//#FUSES WDT
#FUSES WDT_SW                   //No Watch Dog Timer, enabled in Software

#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOFCMEN                  //Fail-safe clock monitor disabled
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES NOBROWNOUT               //No brownout reset
#FUSES PUT                      //Enable the Power-UP Timer
#FUSES NOSTVREN                 //Stack full/underflow will not cause reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)


and the test loop is:

Code:
   setup_adc_ports(sAN5|sAN6, VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2|ADC_TAD_MUL_0);

   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16);     //16=263ms //526 ms overflow
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_4,255/*124*/,1);      //12.5 us (4kHz) overflow, 12.5 us interrupt
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_timer_4(T4_DISABLED,0,1);
   setup_timer_5(T5_DISABLED);
   setup_timer_6(T6_DISABLED,0,1);
   setup_wdt(WDT_OFF); //turn off the wdt

   setup_ccp2(CCP_PWM);
   set_pwm2_duty(0);  //setup but disable PWM

   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
   
   enable_interrupts(INT_RTCC);
   //enable_interrupts(INT_EXT);
   //enable_interrupts(INT_RB);
   //enable_interrupts(INT_AD);
   enable_interrupts(GLOBAL);

 #ifdef CLOCK_16M
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8);     //16=263ms //526 ms overflow
   setup_oscillator(OSC_16MHZ|OSC_NORMAL|OSC_31250|OSC_PLL_OFF|OSC_IDLE_MODE);
 #endif

 #ifdef CLOCK_8M
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16);     //16=263ms //526 ms overflow
   setup_oscillator(OSC_8MHZ|OSC_NORMAL|OSC_31250|OSC_PLL_OFF|OSC_IDLE_MODE);
 #endif

 #ifdef CLOCK_4M
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16);     //16=263ms //526 ms overflow
   setup_oscillator(OSC_4MHZ|OSC_NORMAL|OSC_31250|OSC_PLL_OFF|OSC_IDLE_MODE);
 #endif

   int minutes = 0;
   int seconds = 0;
   int1 pir_active = 1;
   int1 run_timer=0;
   int1 delay_pir=1;
   int1 timer_on = 1;
   int1 display_on = 0;
   int current_state = STATE_INITIALIZE;
   int next_state = STATE_STOPPED;

   minutes = read_eeprom(EE_MINUTES);
   seconds = read_eeprom(EE_SECONDS);
   pir_active = read_eeprom(EE_PIR_ACTIVE);
   timer_on = read_eeprom(EE_TIMER_ON);
   display_on = read_eeprom(EE_DISPLAY_ON);

   WHY_RESET = restart_cause();
   
   //** Turn off Unused Periperals - see datasheet
   MCU_PMD0 = 0xFD; //all peripherals off except Timer2
   MCU_PMD1 = 0xDD; //all peripherals off except CCP2
   MCU_PMD2 = 0x0E; //all peripherals off except ADC

   while(1)
   {
      light_led(BLUE);
      delay_ms(750);
      dark_led(BLUE);
      setup_wdt(WDT_ON);
      sleep();
      delay_cycles(1);
   }


The LED Lights and turns off, then turns back on again (blinking). I don't ever hit the restart_cause() statement, so it appears that the processor isn't going to sleep and being reset by the WDT (I'm running in debugger). Any help would be greatly appreciated!!

Thanks.
Scott
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 13, 2012 11:17 am     Reply with quote

Quote:

I'm trying to put my 18F45K22 to sleep and have it wake up by WDT.
I'm running in debugger.

Look at the Help file for ICD2 in MPLAB. Type in 'sleep' and search for it.
Result:
Quote:

PIC18F Extended, PIC18FXXK Limitations

- The SLEEP instruction cannot be used when debugging.

- The Watchdog Timer (WDT) cannot be used when debugging.
allredsd



Joined: 02 Jun 2011
Posts: 4
Location: Austin, Texas

View user's profile Send private message

PostPosted: Tue Mar 13, 2012 11:22 am     Reply with quote

I've tried burning it into rom and get the same results Confused
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 13, 2012 11:37 am     Reply with quote

1. Make sure your IDE is set to compile and program in "Release" mode.
(not "debug" mode).

2. Simplify your program. Cut out about 90% of your code and test it
again. For example, you have tons of commented out lines, and you have
many lines to setup the Timers, and tons of #ifdefs for different clock
rates. Cut it all out and make a really simple test program. Simplicity
will allow you to see the problem more easily.
allredsd



Joined: 02 Jun 2011
Posts: 4
Location: Austin, Texas

View user's profile Send private message

PostPosted: Tue Mar 13, 2012 2:26 pm     Reply with quote

I've found the reason I'm not sleeping or WDT-ing...I had

Code:
#device ICD=TRUE


in my header file. Once I've got that commented out I can see that I'm going to sleep, and waking up on WDT.

For a follow on, I don't seem to be seeing a significant current savings when I go to sleep. Do I have to manually set up the sleep mode before I call sleep() ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 13, 2012 3:36 pm     Reply with quote

Quote:

Do I have to manually set up the sleep mode before I call sleep() ?

Yes, you do. You have to turn off all modules that consume power,
such as the A/D converter, the comparators, Vref generator, etc.
You also have to set all unused i/o pins so they are not floating.
Set unused pins to a low level. If you have an external pull-up or
pull-down resistor on a pin, then set that pin to be an input (ie., floating).

And the truth is, all your unused i/o pins that are not externally
terminated should be set to "output low" during normal operation as well,
so you won't use unnecessary amounts of current due to floating inputs.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Wed Mar 14, 2012 2:38 am     Reply with quote

Also, 'think' about what else may be drawing power.
There was another thread involving sleep only a few days ago, and the poster was using a7805 regulator. Problem is that these will typically draw about 5mA, with no load. Possibly 1000* what the PIC may be drawing....

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group