View previous topic :: View next topic |
Author |
Message |
johnl
Joined: 30 Sep 2003 Posts: 120
|
PIC10F220 sleep problem |
Posted: Fri Jun 08, 2012 9:49 pm |
|
|
Trying to put to sleep with a button press on B3, and then wake up with the same button press.
The program lights up LEDs on B0 and B1 when the battery is charged, then flashes them if the voltage falls below a threshold. That part works. It doesn't go to sleep or perhaps does but doesn't stay asleep.
Code: |
#include <10f220.h>
#fuses NOMCLR,NOWDT,NOPROTECT,IOSC4
#use delay (CLOCK=4000000)
#define set_options(value) {#ASM \
MOVLW value \
OPTION \
#ENDASM}
#define LOW_VOLTS 32
#byte PortB = 6
int Volts, i, dummy;
void run_mode(void);
void main(void)
{
set_tris_b(0b001000) ; // GP3 input; rest outputs
set_options(0); //pull-ups and pin change wake-up
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_ON);
set_adc_channel(2); // internal voltage reference
run_mode();
} ///end of main()
void run_mode(void)
{
while(1)
{
if (!input(PIN_B3)) // put to sleep on button press
{
delay_ms(50); //debounce
while (!input(PIN_B3))
delay_ms(50);
dummy = PortB;
sleep();
}
Volts = 1530/read_adc();
if (Volts > 32) PORTB = 0; //Battery good: Keep LEDs on
if (Volts < 33) // Battery low: Flash LEDs
{
delay_ms(900);
PORTB = 0;
delay_ms(1);
PORTB = 0xFF;
}
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
![](templates/subSilver/images/spacer.gif) |
johnl
Joined: 30 Sep 2003 Posts: 120
|
|
Posted: Sat Jun 09, 2012 11:18 am |
|
|
Thanks for the links which make sense but I am obviously still missing something.
I had read the datasheet section on sleep and wake-up. In my case, the chip should sleep after a button press is detected, by virtue of these lines:
Code: | if (!input(PIN_B3)) // put to sleep on button press
{
delay_ms(50); //debounce
while (!input(PIN_B3))
delay_ms(50);
PORTB = 3; //turn off LEDs << added line since orig. post >>
dummy = PortB;
sleep(); |
The above now works.
When sleeping, another button press should wake it up as described in the data sheet:
"3. A change on input pin GP0, GP1 or GP3 when
wake-up on change is enabled."
-at which point it should reset and start execution at the beginning of main(), but I don't see that happening.
Any additional clues would be appreciated.
BTW the compiler version PCB 4.132.
[/code] |
|
![](templates/subSilver/images/spacer.gif) |
johnl
Joined: 30 Sep 2003 Posts: 120
|
|
Posted: Sat Jun 09, 2012 11:24 am |
|
|
Working now!
I needed to trap the button press at the beginning so it would not enter sleep again.
Thanks for the help.
Code: | #include <10f220.h>
#fuses NOMCLR,NOWDT,NOPROTECT,IOSC4
#use delay (CLOCK=4000000)
#define set_options(value) {#ASM \
MOVLW value \
OPTION \
#ENDASM}
#define LOW_VOLTS 32
#byte PortB = 6
int Volts, i, cause, dummy;
void run_mode(void);
void main(void)
{
set_tris_b(0b001000) ; // GP3 input; rest outputs
set_options(0); //pull-ups and pin change wake-up
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_ON);
set_adc_channel(2); // internal voltage reference
delay_ms(100);
while (!input(PIN_B3))
delay_ms(50);
run_mode();
} ///end of main()
void run_mode(void)
{
while(1)
{
if (!input(PIN_B3)) // put to sleep on button press
{
delay_ms(50); //debounce
while (!input(PIN_B3))
delay_ms(50);
PORTB = 3; //turn off LEDs
dummy = PortB;
sleep();
}
Volts = 1530/read_adc();
if (Volts > 32) PORTB = 0; //Battery good: Keep LEDs on
if (Volts < 33) // Battery low: Flash LEDs
{
delay_ms(900);
PORTB = 0;
delay_ms(1);
PORTB = 0xFF;
}
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
|