From: Erich W. <ew....@na...> - 2009-12-16 20:45:55
|
Hi and welcome! Robert Liesenfeld wrote: > I'm just beginning to use amforth for some of my ATmega-based projects, and > I have a need for a timer ISR. Ideally I'd like this to be in Forth rather > than AVR assembly, mostly because I'm not very good at assembly. Basically > all I want to do is keep track of the number of milliseconds since "boot", > for interval tracking purposes. The technical documentation for amforth > makes it sound like this is possible in a native Forth word, but doesn't go > into detail, or if it does, I missed it. I'm using timer2 driven by a 32768 Hz quarz to generate a timebase. No assembly involved, because there is no need to clear a bit to acknowledge the interrupt. --- begin clock_tick.fs --------------------------------- \ 2007-12-26 EW clock_tick.fs \ geklaut aus appl/tv/blocks/realtimeclock.frt \ words: timer2 variable \ tick_isr interupt service routine: increments timer2 \ +ticks register and enable interupt \ -ticks disable interupt variable timer2 hex \ overflow2 interupt service routine \ increment tick : tick_isr 1 timer2 +! ; \ enable ticks \ crystal: 32768 /sec \ clock src: 32768 /sec \ overflow: 32768/256 = 128 /sec =^= 7.8125 milli-sec ticks : +ticks 1 TCCR2 c! ( 001 = clock_ts2/1 ) 8 ASSR c! ( source: 32 kiHz crystal ) ['] 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 Timer 2 ) ; ----------------------------------------------------------------- > +ticks makes the "clock" run > timer2 @ u. DA6 ok > timer2 @ u. 10DA ok > -ticks ok > timer2 @ u. 129F ok > timer2 @ u. 129F ok and -tick stop it. Use the content of variable timer2 to keep track of the time. You must read this variable regularly and count up other counters to avoid untracked overflow of timer2. Have a look at amforth/applications/ewlib/timeup.frt for using this. Cheers, Erich |