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 support@ccsinfo.com

PIC10F220 sleep problem

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



Joined: 30 Sep 2003
Posts: 120

View user's profile Send private message

PIC10F220 sleep problem
PostPosted: Fri Jun 08, 2012 9:49 pm     Reply with quote

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;   
       }
   }
}   
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 08, 2012 10:18 pm     Reply with quote

See this thread about what the PIC does when it wakes up.
The 10F220 behaves the same way:
http://www.ccsinfo.com/forum/viewtopic.php?t=45680
Also see the links in that thread.
johnl



Joined: 30 Sep 2003
Posts: 120

View user's profile Send private message

PostPosted: Sat Jun 09, 2012 11:18 am     Reply with quote

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]
johnl



Joined: 30 Sep 2003
Posts: 120

View user's profile Send private message

PostPosted: Sat Jun 09, 2012 11:24 am     Reply with quote

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;   
       }
   }
}   

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