From: pito <pi...@vo...> - 2012-01-20 15:25:22
|
..long time back.. maybe somebody finds it useful.. provided as-is.. Time measurement (with float results) 48bit timer (timer + epoch) for 1284p: \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \ 2007-12-26 EW w4_clock_tick.fs \ 2010-09-02 PITO - FLOAT TIME MEASUREMENT ATMEGA 1284P \ +timer register and enable interupt \ -timer disable interupt marker -mytimer 2variable timer variable epoch 1. 2constant _one 0. 2constant _zero 1 s>f fconstant f1.0 $B1 constant TCCR2B $B6 constant ASSR $70 constant TIMSK2 $016 constant TIMER2_OVFAddr \ overflow2 interupt service routine \ increment tick timer : tick_isr timer 2@ 2dup _one d+ timer 2! 0= if epoch @ 1+ epoch ! then ; \ enable ticks \ overflow fires each f_cpu/256/256 : +timer $6 TCCR2B c! ( prescaler 110b = f_cpu/256 ) $0 ASSR c! ( source: internal f_cpu) ['] tick_isr TIMER2_OVFAddr int! ( do register the interrupt ) 0 epoch ! _zero timer 2! ( clear the timer at start ) TIMSK2 c@ $01 or TIMSK2 c! ( do enable the timer2 interrupt ) ; \ disable ticks : -timer TIMSK2 c@ $fe and TIMSK2 c! ( disable - clr timer2 interrupt ) ; \ MEASURE TIME IN SECONDS (FLOAT) decimal 2variable 0_ticks \ tick_in_secs = 1 / f_cpu / 256 / 256 f_cpu d>f 256 s>f f/ 256 s>f f/ f1.0 fswap f/ fconstant tick_in_secs : timer-start ( -- ) timer 2@ 0_ticks 2! ; : .elapsed ( -- ) timer 2@ 0_ticks 2@ d- d>f tick_in_secs f* 3 fsn. ." secs " ; \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ and "zero drift clock" - it increments in one second interval - when you know the exact crystal frequency (ie f_cpu = 12.487.388 Hz) then you get a precise second - none other tweaking required \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ marker _zerodriftclock_ \ ZERO DRIFT CLOCK \ IT USES F_CPU ONLY (XTAL FREQUENCY SHALL BE KNOWN AS PRECISE AS \ POSSIBLE) FOR PRECISE 1 SECOND INCREMENT \ IT WORKS WITH _ANY_ VALUE XTAL \ THE SECONDS ARE INCREMENTED IN 1 SEC INTERVAL \ USES TIMER2 ATMEGA32 \ Pito July2010 decimal 2variable ticker variable seconds 262144. 2constant clkpi ( clk per interrupt = 256*prescaler ) 1. ticker 2! 0 seconds ! \ overflow2 interupt service routine \ ! not optimised yet : tick_isr ticker 2@ clkpi d- ticker 2! ticker 2@ clkpi d< if f_cpu ticker 2@ d+ ticker 2! 1 seconds +! then ; hex \ enable ticks : +ticks 7 TCCR2 c! ( 111b = f_cpu clock/1024 ) 0 ASSR c! ( source: internal clock f_cpu) ['] tick_isr TIMER2_OVFAddr int! ( register interupt ) TIMSK c@ 40 or TIMSK c! ( enable timer2 interupt ) ; \ disable ticks : -ticks TIMSK c@ [ 40 invert ff and ] literal and TIMSK c! ( clr timer2 ) ; decimal : shwticker 1000 0 do cr ticker 2@ d. space seconds @ u. loop ; : shwsec seconds @ u. ; \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ p. -- - - Reklama - - - - - - - - - - - - - - Lyže, snowboardy, monoski, sáňky, brusle, sněžnice a další kluzadla oprášit, zimní radovánky jsou tu! Speciál Zima portálu VOLNÝ.cz najdete na http://bit.ly/ymcefA |