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

Question about Timer 1.

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



Joined: 20 Sep 2003
Posts: 47
Location: Brussel - Belgium

View user's profile Send private message

Question about Timer 1.
PostPosted: Fri Jun 03, 2005 10:18 am     Reply with quote

I use a 16F876A and PCWM 3.217. I want to use the timer1 to count the time in msec.

At each timer1 interrupt, I reset it with (65536-4000) to count 1 msec and I increment the mseccount.
My pic receive also one pulse each second (accuracy of 1 µSec) on B0.
During this interrupt, I reset the timer1, set a flag, copy mseccount to mseccountold and set msecount to 0.
In the main I print the mseccountold value.

My projet use 92 % of the space.

When I run it, I see that between two pulses on B0, the counter have 993 mSec.

Now I write a little program (see below) with only both interrupts. The output give 995 msec.

My questions are :
- why not 1000 msec ?
- why 2 msec difference, time in bank switching ?


Code:

#include <16F876A.h>
#device *=16
#use delay(clock=16000000)
#define CRYSTAL_FREQ    16000000
#fuses HS,NOWDT,PUT,BROWNOUT,NOWRT,NOLVP
#use rs232 (baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=PC,errors)

#define timer1init      61536      // (65536 - 4000)

#use fast_io ( A )
#use fast_io ( B )
#use fast_io ( C )
#priority  EXT , TIMER1

static long lmSecCount, lmSecCountOld;
static char Pulse;

/****  Interrupts   ******************************************/

#INT_TIMER1
   void Timer1Interrupt ( void )
      {
      set_timer1(timer1init+get_timer1());
      lmSecCount ++;
      output_toggle(PIN_A1);
      }

#int_ext
      //  Pulse on B0
     void intRB0 ()
      {
      set_timer1(timer1init);
      lmSecCountOld = lmSecCount;
      lmSecCount=0;
      Pulse = 1;
      }

/* Main  */

void main ( void )
{
      /* INITIALIZE */
      port_b_pullups(TRUE);
      setup_adc_ports ( NO_ANALOGS );

      set_tris_b(0xff);
      set_tris_a(0x20);
     set_tris_c(0b10011010);

      setup_timer_1(T1_INTERNAL ||T1_DIV_BY_1);
      set_timer1(timer1init);

      /* INTERRUPTS */
      Pulse = 0 ;
      enable_interrupts ( INT_EXT );     // PPS interrupt
      enable_interrupts (INT_TIMER1);    // enable timer1 interrupts
      enable_interrupts ( GLOBAL );       // enable all interrupts

       /* MAIN LOOP */

      while ( TRUE )
      {
         if (Pulse == 1)
               {
               fprintf(PC,"%04lu  ",lmSecCountOld);
               Pulse = 0;
               }

         }
      }


If somebody can put me on the correct way. Thanks in advance.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 03, 2005 11:09 am     Reply with quote

In this thread, Mark and ckielstra recommend against the method of
adding the current timer value to the constant, and instead they suggest
that you should just tweak the constant value so it works.
This thread is about Timer0, but the same idea applies to Timer1.
http://www.ccsinfo.com/forum/viewtopic.php?t=21573&start=0
GDetienne



Joined: 20 Sep 2003
Posts: 47
Location: Brussel - Belgium

View user's profile Send private message

PostPosted: Sat Jun 04, 2005 9:10 am     Reply with quote

Many thanks for your answer.

You are right, the instruction "set_timer1(timer1init+get_timer1());" ask 21 instructions. During this time I loss minimum 5 µSec.

Do you think it's possible to reduce this code ?

Thanks a lot, have a nice week end.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Sat Jun 04, 2005 10:14 am     Reply with quote

If you use timer2, you won't have to reload the value.
You could use the CCP module in compare mode as well. You can either set it to reset on match or you could just keeping adding the value to the ccp register.
GDetienne



Joined: 20 Sep 2003
Posts: 47
Location: Brussel - Belgium

View user's profile Send private message

Thanks
PostPosted: Sat Jun 04, 2005 3:01 pm     Reply with quote

Thanks Mark and PCM programmer for your help.
I try in this way.

Have a nice week end.
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