View previous topic :: View next topic |
Author |
Message |
jpts
Joined: 08 Mar 2017 Posts: 40
|
|
Posted: Fri May 12, 2017 9:11 am |
|
|
Let me simulate your program here and see what happen ....Thanks |
|
|
jpts
Joined: 08 Mar 2017 Posts: 40
|
|
Posted: Sat May 13, 2017 12:59 pm |
|
|
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
|
|
Posted: Sat May 13, 2017 1:16 pm |
|
|
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
|
|
Posted: Mon May 15, 2017 11:12 am |
|
|
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
|
|
Posted: Mon May 15, 2017 11:17 am |
|
|
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. |
|
|
|