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

10F200 power up problem

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



Joined: 22 Dec 2003
Posts: 82

View user's profile Send private message

10F200 power up problem
PostPosted: Mon Nov 26, 2012 3:32 am     Reply with quote

I have 10F200 working with one CR2032 battery and blink 2 LEDs randomly, start to work after pushing a tact switch.
The problem - sometimes after insertion of the battery to LEDs start to blink without touching the tact switch.
Any idea?

MPLAB 8.80
PCWHD 4.12

Code:

#include <10F200.h>

#fuses WDT,NOMCLR,NOPROTECT

#use delay(clock=4000000)
#use fast_io(B)
#byte portb=0x06
#byte OSCCAL=0x05

#bit SPARE = portb.0
#bit LED1 = portb.1
#bit LED2 = portb.2
#bit TACT = portb.3

short ON_OFF;
unsigned int MY_RANDOM;

#define set_options(value)   {#ASM         \
                              MOVLW  value \
                              OPTION       \
                              #ENDASM}


void check_TACT()
{
   int8 i;
   delay_ms(30);  //debounce
   if (!TACT)
   {
      LED1 = 0;
      LED2 = 0;
      delay_ms(1000);     //long press to turn off
      if (!TACT)
      {
         restart_wdt();
         for (i=0; i<5; i++)
         {
            LED1 = 0;
            LED2 = 1;
            delay_ms (200);
            LED1 = 1;
            LED2 = 0;
            delay_ms (200);
         }
         LED2 = 1;
         ON_OFF=0;
         sleep();
      }
   }
   LED1 = 1;
   LED2 = 1;
}


void init()
{
   OSCCAL = OSCCAL & 0xFE;  //this is a must to enable GP2 as IO and not FOSC
   LED1 = 1;
   LED2 = 1;
   TACT = 1;
   SPARE = 1;
   set_tris_b(0b1000);
   setup_wdt(WDT_2304MS);
   restart_wdt();
}


void main()
{
   unsigned int8 loop, i, ZROR, y;
   set_options(0b01010111);   //enable wake on change and disable pullups
   y = restart_cause(); // This must be first execution in the main!!!
   init();

   if (y == NORMAL_POWER_UP) //Reset cause by NORMAL P.U
      sleep();

   if ((y & 0x90) == 0x90) //PIN_CHANGE_FROM_SLEEP
   {
      MY_RANDOM=0b11001011;  //This is the sequence of blinking
      ON_OFF = 1;
   }
   else if (!ON_OFF)
      sleep();

   while(1)
   {
      restart_wdt();
      ZROR = (MY_RANDOM & 0b00000111);
      for (loop=0; loop<ZROR; loop++)
      {
         LED1 = 0;                  //LED ON
         LED2 = 0;                  //LED ON
         delay_ms(10);             
         LED1 = 1;
         LED2 = 1;
         delay_ms(100);             
      }
      rotate_right (&MY_RANDOM,1);

      if (!TACT)
         check_tact();
      sleep();
     
   }
}
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Mon Nov 26, 2012 4:15 am     Reply with quote

Have you allowed for coin cell connection bouncing as you insert?

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19278

View user's profile Send private message

PostPosted: Mon Nov 26, 2012 4:34 am     Reply with quote

Several little things:
First as Mike says, connection bounce.
Then you are not initialising ON_OFF. It could contain anything (think how this affects your code). Initialise it in code called for NORMAL_POWER_UP.
Then - same problem, you are not reading the input before entering sleep. Data sheet:
"Caution: Right before entering Sleep,
read the input pins. When in Sleep, wakeup
occurs when the values at the pins
change from the state they were in at the
last reading. If a wake-up on change
occurs and the pins are not read before reentering
Sleep, a wake-up will occur
immediately even if no pins change while
in Sleep mode."

Problem is the pin change latch can get set during power on. Then you will get an immediate wake up (won't sleep), and with ON_OFF containing random values, it can then fall through your routines into the flash code.

Your set_options code, breaches the comment: "// This must be first execution in the main!!! ". Setting options can affect the bits seen, reading restart_cause should be done first.

Best Wishes
edi



Joined: 22 Dec 2003
Posts: 82

View user's profile Send private message

PostPosted: Mon Nov 26, 2012 5:47 am     Reply with quote

Many thanks,
Will try it and update.

Regards.
edi



Joined: 22 Dec 2003
Posts: 82

View user's profile Send private message

PostPosted: Mon Nov 26, 2012 6:17 am     Reply with quote

Additional 2 question please:

1. What can be done regarding the coin battery connection bounce?
2. The board cost is very critical, is there a need for coupling capacitor while using only direct CR20302 battery?
Ttelmah



Joined: 11 Mar 2010
Posts: 19278

View user's profile Send private message

PostPosted: Mon Nov 26, 2012 8:30 am     Reply with quote

Yes. Something like a 0.1uF ceramic though, not an electrolytic. Cost lower, and has better performance at high frequencies (which is where it matters). Whether you need more capacitance than this, will depend on what else is on your board. Anything that 'switches', implies more need for capacitance.

Best Wishes
temtronic



Joined: 01 Jul 2010
Posts: 9142
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Nov 26, 2012 8:38 am     Reply with quote

also..
if applicable add PUT to the fuses. PowerUpTimer option allows the PIC to 'stabilze' on powerup making for a 'clean' start.

add a delay_ms(500); to your main code before you do any checking. Again to help the PIC get 'organized' and 'settled'.

As previously stated, preset all variables with default values,clear any interrupts,etc.

hth
jay
edi



Joined: 22 Dec 2003
Posts: 82

View user's profile Send private message

PostPosted: Tue Nov 27, 2012 12:11 am     Reply with quote

Updated my code with all your recommendations.
Now its working perfect.

THANKS.
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