PDA

View Full Version : Setting up a simple timer


jinx
11-28-05, 04:25 AM
Hi,

Sorry if this sounds like an extremely naive request. I just want to set up a ServoPod USB timer so that it starts counting when I initialise it and then I want to read the count whenever I have to and then zero it out and reinit it to count again.
Basically I want to measure the time between when I take samples from my quadrature decoder, which I have set up using QUAD0. I need this to compute the angular velocity from the position differences.
I've seen a lot of sample code in the forums which deals with timers to capture and count pulse widths, rising/falling edges etc. What I was actually looking for is just a simple code/macro which will set up a timer in the simplest mode (just incrementing on each system clock tick) and which I can read the count from program and which I can then reset to start counting again, ad infinitum.

If anyone has an example of setting such a (single-shot?) timer up or can point me to a place which does, I'll be extremely grateful.

Thanks again,
Jinx.

nmitech
12-01-05, 02:00 PM
One second timer example from the 'Pod download page.

\ TIMING EXAMPLE USING TCFTICKS
\ TCFTICKS is a counter variable which is incremented once per IsoMax cycle.
\ Unless otherwise specified with CYCLES or PERIOD, the default rate is
\ 100 Hz. TCFTICKS is an unsigned 16-bit value which rolls over from FFFF to 0.

DECIMAL 100 CONSTANT 1-SECOND \ 100 counts of a 100 Hz clock

: WAIT-1-SECOND ( -- )
ISOMAX-START \ starting time
TCFTICKS @
BEGIN
TCFTICKS @ OVER - \ elapsed time since start
1-SECOND > UNTIL
DROP ; \ discard starting time


Similarly, you could do some thing like this,

COLD

DECIMAL
100 CONSTANT 1SEC
VARIABLE SECOND

: MSP
0 SECOND !
0 TCFTICKS !
TC0 SET-PWM-IN ( set up TC0 as input to measure the pulse
50000 PERIOD ( 50000 is a default number. Change this if required )
ISOMAX-START ( start the clock
BEGIN
TCFTICKS @ 1SEC > IF
SECOND 1+! ( second++;
0 TCFTICKS ! ( reset tick
THEN
CHK-PWM-IN ?DUP UNTIL DROP
SECOND @ U. ." sec "( display time measured )
;


You may change the period and/or constant number to measure certain pulse length.