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

Timer0 vs Timer1
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jpts



Joined: 08 Mar 2017
Posts: 40

View user's profile Send private message

PostPosted: Fri May 12, 2017 9:11 am     Reply with quote

Let me simulate your program here and see what happen ....Thanks
jpts



Joined: 08 Mar 2017
Posts: 40

View user's profile Send private message

PostPosted: Sat May 13, 2017 12:59 pm     Reply with quote

I add couple line program...
The Pulse_total shouldn`t be 10k?

Code:
long int Pulse_in = 0;

void main()     
{
printf("Start\n\r");

setup_timer_1(T1_EXTERNAL_SYNC | T1_DIV_BY_1);
set_timer1(0);

while(TRUE)
  {
      delay_ms(500); // Just to accumulate pulses in T1 register.
     
       Pulse_in = get_timer1();
       set_timer1(0);

       Pulse_total=Pulse_total+Pulse_in;

 

   if(!input(BUTTON_PIN))  // If button is pressed
     {
      delay_ms(10);        // Debounce delay
      printf("Pulse_total = %lu \n\r",Pulse_total);
      printf("Timer1 = %lu \n\r", get_timer1());
      while(!input(BUTTON_PIN));  // Wait for button to be released
      delay_ms(10);        // Debounce delay
     }
  }
Ttelmah



Joined: 11 Mar 2010
Posts: 19219

View user's profile Send private message

PostPosted: Sat May 13, 2017 1:16 pm     Reply with quote

Your program is only acquiring for 0.5 seconds. It takes 4 seconds to send 10000 pulses....

Depending on when you push your button, you could see 2500, 5000, 7500, 10000, 12500...... 65000, 1964 (when the timer wraps) etc.. Almost anything...

As another poster has said, you also should not set the timer back to zero once you start recording. If a pulse comes inside this instruction, it will get missed. Basically only ever read the timer, and if you want to count 'from' a value, then record the current count and subtract this.
jpts



Joined: 08 Mar 2017
Posts: 40

View user's profile Send private message

PostPosted: Mon May 15, 2017 11:12 am     Reply with quote

Ttelmah, you are right about back to zero once you start recording the TIMER. Its really cause "miss pulses". TIMER 1 is more evidence about missing pulses than TIMER0. Depending the pulses frequency coming in, the TIMER0 won't be affect, but TIMER1 will. Maybe because 16bits register.

I did change program for not reseting Timer while getting pulses, and it seems to work without missing pulses.
Thanks a lot !!....my Thanks also to PCM programmer spending your time for solution....

Regards, jpts
Ttelmah



Joined: 11 Mar 2010
Posts: 19219

View user's profile Send private message

PostPosted: Mon May 15, 2017 11:17 am     Reply with quote

Yes.
The 16bit timer takes two instructions to update. It resets it's value on the second instruction, losing anything that happens over two instruction times. The 8bit timer only takes one instruction to update.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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