Hi,
here is a routine for time measurement. 
It measures time in float seconds. Requires Leon's float lib and
Pito's floatconstants lib. You may change the granularity by
changing the prescaler, and the constant tck_ms. Mind the f_cpu as
well.

\ 2010-09-02 PITO - FLOAT TIME MEASUREMENT ATMEGA 1284P
\ based on  2007-12-26 EW   w4_clock_tick.fs
\ words: timer    2variable
\        tick_isr  interupt service routine: increments timer
\        +ticks    register and enable interupt
\        -ticks    disable interupt

marker -timer

decimal 

2variable timer
1. 2constant _one
0. 2constant _zero

\ overflow2 interupt service routine
\ increment tick
: tick_isr
_one timer 2@ d+ timer 2!
;

hex
\ enable ticks
\ overflow:  every f_cpu/256/1024
: +ticks
7 TCCR2B c! ( 111b = f_cpu clock/1024 )
0 ASSR  c! ( source: internal clock f_cpu)
['] tick_isr TIMER2_OVFAddr int! ( register interrupt )
_zero timer 2!
TIMSK2 c@  01 or TIMSK2 c! ( enable timer2 interrupt )
;

\ disable ticks
: -ticks
TIMSK2 c@ fe and TIMSK2 c! ( clr timer2 interrupt )
;

\ MEASURE TIME IN SECONDS (FLOAT)
decimal
2variable elapsed_ticks
\ f_cpu = 11059200
\ tck_ms = f_cpu \ 256 \ 1024
23703703. d>f _1e-9 f* fconstant tck_ms

: timer-start ( -- ) timer 2@ elapsed_ticks 2! ;

: timer-stop (  -- f ) timer 2@ d>f elapsed_ticks 2@ d>f f- tck_ms
f* ;

\ print elapsed time in ms 12345 = 12.345sec 
: prnt_elapsed_ms ( f -- ) _1e3 f* f>d d. ;

------------------------------------------

Example:
> +ticks
ok
> timer-start
ok
> timer-stop prnt_elapsed_ms
16023  ok
> 
.......Elapsed time 16.023sec.
Pito


------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
Amforth-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/amforth-devel

Reply via email to