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

counting hours

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



Joined: 10 May 2005
Posts: 323
Location: Belgium

View user's profile Send private message

counting hours
PostPosted: Thu Jun 30, 2005 6:39 am     Reply with quote

Hi,

every time I switch the device off, I want the timer1 to start counting hours, after 24 hours, the PIC should do something and go to sleep.
Unfortunately, my PIC counts wrong:

Code:
//************************** Timer1 interrupt ********************************//
//Timer1 wordt gestart als de NS uit staat
#int_timer1
Timer1_isr()
{
      if(int_count++ == 2)
      {         
            ++secs;
     
            if(secs==10)
            {
                putc('E');
               //mins=mins+1;
                secs=0;
                setup_timer_1(T1_DISABLED);
            }   
     
           // if(mins==60)
           // {
             //  hrs=hrs+1;
              // mins=0;
            //}
     
         /*   if(hrs == 2)
            {
               hrs=0;
            }       
        */
         int_count = 0;   
      }
     
      Set_Timer1 ( 0x0BDB);
}// TIMER1


Code:
//************************** EXTERNAL interrupt ******************************//
//Interrupt als de AAN/UIT schakelaar wordt bewogen
#INT_EXT
void ext_isr()
{
   int8 i;

   for (i = 0; i < 30; i++)         // Debounce
   {
      delay_us(999);
   }
   
 

   if (input(AAN_UIT))
   {
      E3_SLEEP = FALSE;
      setup_timer_1(T1_DISABLED);
      sleep_mode = FALSE;
   }

   else
   {
       E3_SLEEP = TRUE;
       setup_timer_1( T1_INTERNAL| T1_DIV_BY_8 );
       Set_timer1 ( 0x0BDB); // Start de Timer1 vanaf
   }
   
   if (E3_SLEEP)
   {
      printf("%c%S%c",COMMANDO_BYTE, SLEEP_COMMANDO,0);
      ext_int_edge(L_TO_H);
   }

   else
   {
      setup_uart(TRUE);
      if (!sleep_mode)
         printf("%c%S%c",COMMANDO_BYTE, WAKE_COMMANDO,0);
      ext_int_edge(H_TO_L);   
   }
}
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Thu Jun 30, 2005 7:28 am     Reply with quote

Think about how this works. You could do the same with timer 1.

Code:

/*********************************************
*    Initialize RTC                          *
*********************************************/
void Initialize_RTC(void)
{  Ticker = XTAL_FREQUENCY;
   setup_timer_2 ( T2_DIV_BY_4, 0xff, 4 );   // initialize 8 bit counter/timer 2 to interupt every 16384 clock cycles
   enable_interrupts( INT_TIMER2 );          // Start RTC
}

// Global Real Time Clock Information
#int_TIMER2                                  // Clock interrupt adjusted to occurs ~ 1ms
void TIMER2_isr()                            //  -=Process Zero Drift Real Time Clock Information=-
{  tickmSec = 1;                             // set each MS very approximately
   Ticker -= 16384;                          // Decrement ticker by clocks per interupt
   if ( Ticker < 16384 )                     // If second has expired
   {  Ticker += XTAL_FREQUENCY;              // Increment ticker by clocks per second
      second++;                              // Increment number of seconds
      tickSec = 1;                           // set each Second very accuratly
   }
}
Christophe



Joined: 10 May 2005
Posts: 323
Location: Belgium

View user's profile Send private message

PostPosted: Thu Jun 30, 2005 7:35 am     Reply with quote

well,

I could it with timer2; difference:

Timer2: interrupt every 16384 us (your example)
Timer1: interrupt every 50000 us (1/2 s)

What you think is most efficient?

In the meanwhile I found the bug.. thanks f|#C=n CCS:

the bug is this statement:

Code:
Timer1_isr()
{
      if(int_count++ == 2)
      {      blabla;... }


should be:

Code:
Timer1_isr()
{
      int_count++;
      if(int_count == 2)
      {      blabla;... }


Don't ask me why, but it works now.. took me some hours :\
Ttelmah
Guest







PostPosted: Thu Jun 30, 2005 7:55 am     Reply with quote

The two statements are different.
In the 'new' one, you increment the value _before_ testing it.
The equivalent 'combined' statement, is:
Code:

Timer1_isr()
{
      if(++int_count == 2)
      {      blabla;... }


Note where the '++' is. Putting it in front of the variable, means 'increment, then take the value'.
However the syntax 'int_count++', means take the value, _then_ increment the variable.
This will give an extra 'count' in the interrupt routine, before the value is seen to be right.

Best Wishes
Christophe



Joined: 10 May 2005
Posts: 323
Location: Belgium

View user's profile Send private message

PostPosted: Thu Jun 30, 2005 8:39 am     Reply with quote

Just tested that,

it's true, thanks again for the information
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