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

Timer1 as a 32bit counter

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest Who Needs Help
Guest







Timer1 as a 32bit counter
PostPosted: Wed Jan 21, 2004 9:26 am     Reply with quote

Can someone help me out with getting a 32bit value from Timer1? I thought this would work, but I am getting odd results:

Code:

int16 high16_counter;
int32 last_counter;

#int_TIMER1
TIMER1_isr() {
   high16_counter++;
}

int1 got_half_sample;
int1 got_sample;

void stop_scanning(void) {
   disable_interrupts(INT_EXT);
   disable_interrupts(INT_TIMER1);
}

void start_scanning(void) {
   setup_timer_1(T1_DISABLED);
   got_half_sample=FALSE;
   got_sample=FALSE;
   enable_interrupts(INT_EXT);
}

#int_EXT
EXT_isr2() {
   if (got_half_sample) {
      setup_timer_1(T1_DISABLED);
      disable_interrupts(INT_TIMER1);
      if (TMR1F) {high16_counter++;}
      TMR1F=0;
      disable_interrupts(int_EXT);

      got_sample=TRUE;
      last_counter=make32(high16_counter,get_timer1());
   }
   else {
      setup_timer_1(T1_DISABLED);
      set_timer1(0);

      T1CON=0x01; //setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_1 );

      TMR1F=0;
      high16_counter=0;
      enable_interrupts(INT_TIMER1);
      got_half_sample=TRUE;
   }
}


Basically I want to count the number of cycles between pulses on B0. I call start_scanning() when I want a new 32bit value, and then wait until got_sample is true. First, it doesn't look like int_Timer1 is triggering because the high16 counter never increments. Second, the low 16bit values are never the same (way off) between samples even though the frequency into B0 is steady.


Thanks for any help you can provide. Very Happy
Guest Who Needs Help
Guest







PostPosted: Wed Jan 21, 2004 9:28 am     Reply with quote

I forgot to mention that this is for a PIC16C926, and that I am using various versions of PCM (3.182 mostly)
Guest








PostPosted: Wed Jan 21, 2004 6:36 pm     Reply with quote

Wow, no one wanted to help. Sad

I figured it out, I had to clear the int_ext interrupt flag before enable int_ext interrupts in start_scanning(). Else as soon as I enabled the interrupt it would service it, and then the counter not setup correctly.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 21, 2004 6:57 pm     Reply with quote

It's not that no one wanted to help.

Sometimes, the people who regularly post replies on here
are extremely busy at their companies. It's like that for
me today. I have got to get this board layout done.
NEWBOY



Joined: 19 Dec 2003
Posts: 9

View user's profile Send private message

PostPosted: Tue Jan 27, 2004 2:31 pm     Reply with quote

Hi
Did your program for the 32 bit counter for Timer1 work
As a learner in C could you be kind enough to explain it to me.
i am thinking of using it with a few MODS on the 18F452 to count a master clock at 40mHz

Santana
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Tue Jan 27, 2004 4:28 pm     Reply with quote

Start with this Idea . No time to test it !!



Code:


int16 high16_counter;
int16 low16_counter;
int32 last_counter;

#int_TIMER1
TIMER1_isr()
{
   low16_counter = get_timer1();

   if(!low16_counter)
     {
       high16_counter++;
     }
}

#int_EXT
EXT_isr2()
{
      set_timer_1(1);
      enable_interrupts(INT_TIMER1);
      edge_count++;
}
     
main()
{
  setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_1 );
  enable_interrupts(GLOBAL);
 
  while(1)
        {
         if(!edge_count)
           {
            ext_int_edge( H_TO_L );
            enable_interrupts(INT_EXT);
           }
         else
           {
            if(edge_count == 1)
              {
               ext_int_edge( L_TO_H );
              }
             
            if(edge_count == 2)
             {
               disable_interrupts(INT_EXT);
               disable_interrupts(INT_TIMER1);
               last_counter=make32(high16_counter,low16_counter);
               edge_count = 0;
               delay_ms(500);
               printf("\r\nPulses %4X %4X", high16_counter,low16_counter);
             }     
           }
       }
}





hope this help you, Rolling Eyes

Humberto
NEWBOY



Joined: 19 Dec 2003
Posts: 9

View user's profile Send private message

PostPosted: Thu Jan 29, 2004 3:38 pm     Reply with quote

It did help

Thank you very much

Santana
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