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 CCS Technical Support

Timer1 is 20% off on PIC18F8722

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



Joined: 15 Jan 2007
Posts: 11
Location: Germany

View user's profile Send private message

Timer1 is 20% off on PIC18F8722
PostPosted: Mon Sep 14, 2009 12:52 am     Reply with quote

Hello

I am using Timer1 to count my operating hours of the device and store the value every 15 min into the EEPROM. After 71 hours of operating the counter shows me only 57 hours. This is a difference of 20% to the expected value.

I use to following to setup the counter. The clock is 32 MHz and an overflow will occur every 65,536 ms.
Code:
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);      // Timer 1 enabled, int every 65ms


The fuse is

Code:
#FUSES LPT1OSC



Later on in my code i enable the counter with

Code:
enable_interrupts(int_TIMER1);


The ISR for Timer1
Code:

#int_TIMER1
void TIMER1_isr()
{
int temp, i = 0;
int32 zero = 0;                              // used for writing a 0 into EEPROM
   Power_on_counter++;                       // increment the power on counter
   if (Power_on_counter >= 13733)            // 15 minutes are gone by
   {
      if (GOperating_min >= 0x04)                                // 60min are gone by
      {
             Internal_EEPROM(0, Addr_Operating_time, GOperating_time, 32); // read hours
            GOperating_time++;                                        // add one hour
            Internal_EEPROM(1, Addr_Operating_time, GOperating_time, 32); // write hours
            Internal_EEPROM(1, Addr_Operating_min, zero, 8);              // write minutes = 0
            GOperating_min = 0x00;                                        // clear the min counter
            for (i = 0; i < 4; i++)                                       // store the time in ring buffer
                ring_buf[114+i] = *((int8 *)&GOperating_time+i);          // storing each byte       
      }
      else                                                             // copy data back to global variable
      {
            GOperating_min++;
            Internal_EEPROM(1, Addr_Operating_min, GOperating_min, 8);
      }
      Power_on_counter = 0;                                 // clear the counter
   }


An external Interrupt occurs 833 times per second Int RBA, RBA2, TBE and TBE2 are also used. The serial speed is 19k2 and 115k2 for the two serial lines.
The ISR for the external interrupt has quite some code in it and cannot be changed. But this takes not longer than 65ms. So in my opinion it should be no problem even if the Timer1 Int occurs while the prgoramm another ISR executes. After exiting the ISR the Timer1 ISR should be executed, right? As the Timer1 rolls over automatically at 2^16 counts and starts at the beginning nothing should be lost.
Is there something I have missed or a general problem?

Thanks.

Marcus


The code for Internal_EEPROM():
Code:
/******************************************************************************
*                                                                             *
*   Access to the internal EEPROM of the Microcontroller                      *
*                                                                             *
*    R_W         -> 0..read, 1..write                                         *
*    Base_Address-> internal EEPROM address                                   *
*    Data        -> data to read from/write into internal EEPROM              *
*    Length      -> how long is variable ( 8..8 bit,  16..16 bit,             *
*                                         24..24 bit, 32..32 bit)             *
*    Returns     -> 1..everything OK, 0..EEPROM write failed                  *
*                                                                             *
******************************************************************************/
#inline
int1 Internal_EEPROM(int1 R_W, int Base_Address ,int32& Data, int Length)
{
   int   j, i, loop_end;
   int32 ReRead;

   loop_end = Length/8;                            // determine how many bytes need to be read/written
   switch (R_W)
      {
      case 0:                                      // Read from internal EEPROM
         Data = 0x00;
         for(i=0; i <  loop_end; i++)
         {
            *(((int8 *)&Data)+loop_end-i-1) = Read_EEPROM(Base_Address + i);
         }
         return(OK);
         break;
      case 1:                                      // Write to internal EEPROM
         for (j = 0; j < INT_ROM_RETRY; j++)       // write until error exceeds threshold
            {
            ReRead = 0x00;
            for(i=1; i <= loop_end; i++)
               Write_EEPROM(Base_Address+i-1, *((int8 *)&Data + loop_end - i)); // write data to the EEPROM
            for(i=0; i <  loop_end; i++)
               *(&(int8 *)ReRead+loop_end-i-1) = Read_EEPROM(Base_Address + i); // read written data
            if (ReRead != Data) { continue; } else return(OK);
            }
//            Error_Report(Error_Int_EEPROM);
            return(FAILURE);
         }  break;
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Sep 14, 2009 12:58 am     Reply with quote

Quote:
The clock is 32 MHz

The fuse is #FUSES LPT1OSC

The Timer1 oscillator doesn't run that fast. What is the frequency
of the crystal on the T1OSC pins ? Is it 32.768 KHz ? (watch crystal)
Or some other frequency ?
marc10k



Joined: 15 Jan 2007
Posts: 11
Location: Germany

View user's profile Send private message

PostPosted: Mon Sep 14, 2009 1:41 am     Reply with quote

I am using only the 32 MHz system clock. T1OSC (T1OSO and T1OSI Pins) are used as a output Pins.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Mon Sep 14, 2009 4:24 am     Reply with quote

Please consider, that the processor executes your code rather than guessing your intention.

GOperating_min counts 0->1, 1->2, 2->3, 3->4, 4->0. How many states do you see?
71*4/5 = (about) 57
marc10k



Joined: 15 Jan 2007
Posts: 11
Location: Germany

View user's profile Send private message

PostPosted: Mon Sep 14, 2009 6:16 am     Reply with quote

@FvM

Thank you for your help. After I changed to 4 states it works well. I must have been blind to see that.
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Mon Sep 14, 2009 11:15 am     Reply with quote

It happens to all of us...

External review is one of the best methods to find/fix problems.

Also, make sure to comment the crap out of your code.

When I'm doing something new, I write (in text comments) the flow of what I want right in the file...

Then I file in the code of how it should work (and sometimes alternative methods for doing it and why they might be better)....

You'd be surprised how writing that stuff out helps not only YOU but the NEXT GUY who has to work on your code.

I used to write code for a test fixture system made by this one company. Their programming language was horrible..

One of my co-workers would work with the fixture and always complain that my testing procedures were awkward and I would always tell him "that's the way the language works... I wish it was different."

I quit that company and was back visiting one day and that same co-worker said to me, "I had to jump into some of your code to update it... and I owe you an apology." He went on to say how horrid the language was and how the obscene amounts of comments by me saved him a lot of time and hassle.

So it DOES help!

Very Happy

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
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