Masaya Tanaka
Joined: 04 Feb 2011 Posts: 5 Location: Japan
|
Timer1 will miss counting the first clock pulse |
Posted: Fri May 04, 2012 1:53 am |
|
|
Hi, I got to know that "Timer1" made a mistake.
Information sauce is "Microchip".
http://ww1.microchip.com/downloads/en/DeviceDoc/80329a.pdf
Is there any solution in CCS?
Code: | #include <18F97J60.h>
setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1 | T1_CLK_OUT);
set_timer1(0);
int32 count = 0;
while (true)
{
count = get_timer1();
if (0 < count)
{
set_timer1(0);
fprintf("Input : %Ld \r\n", count);
count = 0;
}
} |
Please comment, and inform us of it kindly. |
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Fri May 04, 2012 7:26 am |
|
|
Depends how fast your CPU is running.
No 'fix' is needed provided your main CPU is clocking faster than about 20MHz.
It wouldn't normally apply in the code you show, anyway, since there is no relationship between when you are doing things, and the external clock. The 'point' is that when you get an interrupt, and want to update the timer inside the handler for this, you need to ensure that both parts of the write are performed before the clock next drops, _or_ wait for the following edge, and then perform both updates, and increment the value, if you are not going to miss an edge.
You are potentially making things 'worse', by using int32 comparison on an int16 value, so the loop will be slower than it can be.
Best Wishes |
|