View previous topic :: View next topic |
Author |
Message |
mdemuth
Joined: 16 Apr 2007 Posts: 71 Location: Stuttgart, Germany
|
PIC10F200 sleep() |
Posted: Fri Jun 17, 2011 9:24 am |
|
|
Hi there,
I just want to flash a red LED for 10 times and the flash a green LED.
The controller is supposed to sleep as much as possible.
Trying to make a little thing work drives me crazy, the code looks like this:
Code: |
//// CCS Compiler Version: 4.121
#include <10f200.h> //
#fuses WDT,NOPROTECT,NOMCLR
#use delay(clock=4000000)
#use fast_io(B)
#define LED_GN PIN_B2 // Ausgang zur LED
#define LED_RT PIN_B1 // Ausgang zur LED
#zero_ram
int8 counter=0;
void main()
{
output_low(LED_RT);
output_low(LED_GN);
set_tris_b(0b11111000);
SETUP_WDT(WDT_36MS); //WDT
restart_wdt();
while (1)
{
counter++;
if (counter > 200) counter = 200; // catch the overflow
if (counter < 11) output_high(LED_RT); // switch on red LED for the first 10 cycles
else output_high(LED_GN); // then switch on green LED
SETUP_WDT(WDT_36MS); // on-time for LEDs
sleep();
delay_cycles(1);
restart_wdt();
output_low(LED_RT); // switch off both LEDs
output_low(LED_GN);
SETUP_WDT(WDT_576MS); // switch-off time for LEDs
sleep();
delay_cycles(1);
restart_wdt();
}
}
|
I also tried to use the restart_cause but no success either...
I have never used the sleep function so far, thanks in advance!
Michael |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jun 17, 2011 12:00 pm |
|
|
Quote: | while (1)
{
counter++;
if (counter > 200) counter = 200; // catch the overflow
if (counter < 11) output_high(LED_RT); // switch on red LED for the first 10 cycles
else output_high(LED_GN); // then switch on green LED
SETUP_WDT(WDT_36MS); // on-time for LEDs
sleep();
delay_cycles(1);
restart_wdt();
output_low(LED_RT); // switch off both LEDs
output_low(LED_GN);
SETUP_WDT(WDT_576MS); // switch-off time for LEDs
sleep();
delay_cycles(1);
restart_wdt();
}
} |
The implication of this code is that it's going to wake-up after going to
sleep, by a Watchdog timeout, and continue execution after the sleep().
But that's not true.
Download the 10F200 data sheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/41239D.pdf
Read the section below. You're not going to ever enter the code that
occurs after the sleep(). All wake-up events (including WDT) cause a
device reset. The code will start executing from the beginning.
Quote: |
9.9.2 WAKE-UP FROM SLEEP
The device can wake-up from Sleep through one of
the following events:
1. An external Reset input on GP3/MCLR/VPP pin,
when configured as MCLR.
2. A Watchdog Timer time-out Reset (if WDT was
enabled).
3. A change on input pin GP0, GP1 or GP3 when
wake-up on change is enabled.
4. A comparator output change has occurred when
wake-up on comparator change is enabled.
These events cause a device Reset.
|
Here are two threads on the 10F200-series and sleep:
http://www.ccsinfo.com/forum/viewtopic.php?t=42138
http://www.ccsinfo.com/forum/viewtopic.php?t=41993 |
|
|
mdemuth
Joined: 16 Apr 2007 Posts: 71 Location: Stuttgart, Germany
|
solved |
Posted: Mon Jun 20, 2011 1:22 am |
|
|
Thanks for the helpful hint!
The code now looks like this:
Code: |
/////////////////////////////////////////////////////////////////////////
//// Version 0.0 20.06.11
//// Michael Demuth indEAS GmbH
//// CCS Compiler Version: 4.121
/////////////////////////////////////////////////////////////////////////
#include <10f200.h> //
#fuses WDT,NOPROTECT,NOMCLR
#use delay(clock=4000000)
#use fast_io(B)
#define LED_GN PIN_B2 // Ausgang zur LED
#define LED_RT PIN_B1 // Ausgang zur LED
#zero_ram
int8 counter;
int8 RESTART;
void main()
{
RESTART = RESTART_CAUSE();
output_low(LED_RT);
output_low(LED_GN);
set_tris_b(0b11111000);
restart_wdt();
if (RESTART ==NORMAL_POWER_UP) counter = 0;
counter++;
if (bit_test(counter,0))
{
SETUP_WDT(WDT_36MS);
if (counter < 20) output_high(LED_RT); // switch on red LED for the first 10 cycles
else output_high(LED_GN); // then switch on green LED
}
else
{
output_low(LED_RT); // switch off both LEDs
output_low(LED_GN);
SETUP_WDT(WDT_576MS);
}
sleep();
}
|
It works!
|
|
|
|