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

10F222 sleep/wake up Help !!

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



Joined: 31 Mar 2019
Posts: 43
Location: Sao paulo/Brazil

View user's profile Send private message Send e-mail

10F222 sleep/wake up Help !!
PostPosted: Thu May 23, 2019 1:27 pm     Reply with quote

Please, I humbly ask to help colleagues for a problem I can not solve.

I have this code, which transmits a code (garage control) but after transmitting I want the pic to go into sleep mode to save battery, and when you press a button it will wake up and transmit the code as long as the button is pressed, after releasing the button he goes back to sleep.

Code:
#include <10F222.H>
#fuses IOSC4,NOWDT,NOMCLR
#use delay(clock=4000000)
#define saida PIN_B0



INT8 DATA[3];
 const int16 te = 500;  //valor de TE
 int8 a;
  int x;
int1 chave = true;
//==============================================================================
// rotina geradora de tempo piloto de 23 TE sem transmissão
//==============================================================================
void pilot_period(){
 output_low(saida);
 delay_us(23*te);
}
//==============================================================================
// rotina geradora do start bit
//==============================================================================
void start_bit(){
    output_high(saida);
    delay_us(te);
    output_low(saida);
//==============================================================================
// rotina geradora de bit '1'
//==============================================================================
}
void bit1(){
    output_low(saida);  //'1'
    delay_us(2*te);
    output_high(saida);
    delay_us(te);
    output_low(saida);
//==============================================================================
// rotina geradora de bit '0'
//==============================================================================
}
void bit0(){
    output_low(saida);  //'0'
    delay_us(te);
    output_high(saida);
    delay_us(2*te);
    output_low(saida);
}
//==============================================================================
// rotina geradora de bits de fim de transmissão
//==============================================================================
void end_period(){
    bit0();
    bit1();
    bit0();
    bit1();
}

//==============================================================================
// rotina de envio para saída rf de um byte
//==============================================================================

void envia(int valor){
  int b;
  for(b=0;b<8;b++){

      if(valor&1){
         bit1();
      }
      else{
         bit0();
      }
      valor>>=1;
  }
}

void main()
{
 

while(true)
{

  while(!input(pin_B3))
  {
   
         for(x=0; x<=50;x++)
         {
            data[2] = 1;
             data[1] = 2;     
            data[0]=0b01000000; //botão 1 acionado?     
             
            pilot_period();// 23 TE desligado
            start_bit();//bit de inicio
                 
            for(a=3;a>0;a--){ //envia os 3 bytes do serial
            envia(data[a-1]);
            }
            end_period();// envia os bits de finalização 0-1-0-1
           
             output_toggle(pin_b1);
         }
            output_low(pin_b1);
     sleep(); // Here he goes back to sleep.
     
  }


Please show me by some example, I am a beginner.
Ttelmah



Joined: 11 Mar 2010
Posts: 19219

View user's profile Send private message

PostPosted: Thu May 23, 2019 2:13 pm     Reply with quote

Understand, that on this chip, the code will _reset_ on a wake.
It'll start again from the beginning.

Test 'restart_cause' at the beginning to see if this is a normal power on, or
wake from sleep. If you need to tell these apart.

At the start of the code, add:

setup_wdt(PIN_CHANGE_FROM_SLEEP);

Then when pin_B3 (or B0 or B1), changes from what it was, the chip will
wake. Since B0 is an output, B1 is the only one that matters. You need to
make sure this is not floating. Gentle pull up resistor on this, and read it at
boot (best to read _all_ the pins). If you don't read a pin, and it was the pin
that triggered the wake, you will not be able to go to sleep again....
rodrigo_cirilo



Joined: 31 Mar 2019
Posts: 43
Location: Sao paulo/Brazil

View user's profile Send private message Send e-mail

PostPosted: Fri May 24, 2019 5:25 am     Reply with quote

Ttelmah wrote:
Understand, that on this chip, the code will _reset_ on a wake.
It'll start again from the beginning.

Test 'restart_cause' at the beginning to see if this is a normal power on, or
wake from sleep. If you need to tell these apart.

At the start of the code, add:

setup_wdt(PIN_CHANGE_FROM_SLEEP);

Then when pin_B3 (or B0 or B1), changes from what it was, the chip will
wake. Since B0 is an output, B1 is the only one that matters. You need to
make sure this is not floating. Gentle pull up resistor on this, and read it at
boot (best to read _all_ the pins). If you don't read a pin, and it was the pin
that triggered the wake, you will not be able to go to sleep again....



I really appreciate your response !!

I ask for kindness if you can exemplify in my code how to do the things you asked for I thank you.

I am lost from where to put each thing, and how to read the pins !!
rodrigo_cirilo



Joined: 31 Mar 2019
Posts: 43
Location: Sao paulo/Brazil

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 03, 2019 11:28 am     Reply with quote

Please someone help me on this issue ?? Also I need internal pull_up
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 03, 2019 12:26 pm     Reply with quote

Use the forum's search page.
http://www.ccsinfo.com/forum/search.php
Type in:
Quote:
restart_cause WDT_FROM_SLEEP 10F*

Set it to:
Quote:
Search for all terms

Also, you could skip the 10F*, and just search for:
Quote:
restart_cause WDT_FROM_SLEEP

Then you get threads with example code. See this post by Ttelmah:
http://www.ccsinfo.com/forum/viewtopic.php?t=38277

Because you probably don't have RS-232 implemented, you could
blink an LED 3x if it's a Watchdog wakeup from sleep, or 1x if it's
some other reason.
Ttelmah



Joined: 11 Mar 2010
Posts: 19219

View user's profile Send private message

PostPosted: Tue Jun 04, 2019 7:23 am     Reply with quote

The idea here is that you try things yourself, and we will _help_. Not
do it for you.

However as an example of waking on B3 changing on your chip:
Code:

//existing headers here
#byte latch=getenv("SFR:GPIO")

void main()
{
   int8 dummy;

   setup_wdt(PIN_CHANGE_FROM_SLEEP); //This will make the chip wake when a pin changes
   //This should also default to enabling the pullups

   while(input(pin_B3)==0) //While this pin is low
   {
      for(x=0; x<=50;x++)
      {
         data[2] = 1;
         data[1] = 2;     
         data[0]=0b01000000; //botão 1 acionado?     
             
         pilot_period();// 23 TE desligado
         start_bit();//bit de inicio
                 
         for(a=3;a>0;a--)
         { //envia os 3 bytes do serial
            envia(data[a-1]);
         }
         end_period();// envia os bits de finalização 0-1-0-1
           
         output_toggle(pin_B1);
      }
      output_low(pin_B1);   
   }
   //When B3 goes high get here
   //Now to go to sleep, I must ensure I have read all pins - do this without changing
   //TRIS, by accessing SFR
   dummy=latch;
   
   sleep(); // Here he goes back to sleep.
   delay_cycles(1); //On most chips a NOP is required after the sleep. Data sheet
   //does not say, and since it can't execute the instruction after, probably nbt
   //needed
}
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