FlashForth for Arduino Wiki
FlashForth for Arduino as an interactive programm environment
Brought to you by:
ckuehnel
To realize exact timing Arduino has several timers which can be used with interrupts.
Timer0 is used for ms. Timer1 is used for the CPU load measurement. Timer2 is free.
If you want to use Timer 1 you need to recompile FF without CPU load measurement.
When you recompile FF you can in the configuration file select the timer used for MS. It can be timer 0, 1 or 2.
In the program sample msecTimer.ff I use Timer2 to generate a 10 ms output compare interrupt which increments a variable count each 10 ms.
http://sourceforge.net/projects/flashforthforarduino/files/msecTimer.ff
\
\ Title : msecTimer
\ Author : Claus Kuehnel
\ Date : 2015-01-17
\ Id : msecTimer.ff
\ Version : 5.0
\
\ DISCLAIMER:
\ The author is in no way responsible for any problems or damage caused by
\ using this code. Use at your own risk.
\
\ LICENSE:
\ This code is distributed under the GNU Public License
\ which can be found at http:\www.gnu.org/licenses/gpl.txt
\
\ Disable interrupt before removing the interrupt code
T2_COMPA_Dis
-msectimer
marker -msectimer
variable count
\ Port Register Definitions
$24 constant DDRB
$25 constant PORTB
#32 constant pLED
\ Timer2 Register Definitions
$b0 constant TCCR2A
$b1 constant TCCR2B
$b3 constant OCR2A
$70 constant TIMSK2
#8 constant OC2Aaddr \ interrupt vect no 8
\ OCR2A Reload for 10 ms@16 MHz clock & prescaler 256
$9c constant reload
: flash_led ( -- )
PORTB c@ pLED xor PORTB c!
;
\ Interrupt routine for Timer1 CompareA
: T2_COMPA_ISR
1 count +!
;i
: setup ( -- )
pLED DDRB mset \ set pin as output
pLED PORTB mclr \ LED off
0 TCCR2B c! \ Stop Timer2
reload OCR2A c! \
0 count !
['] T2_COMPA_ISR OC2Aaddr int!
%0010 TCCR2A c! \ CTC Mode
%0111 TCCR2B c! \ Prescaler = 1024
%0010 TIMSK2 c! \ Timer2 CompareA Interrupt enable
hex
cr ." OCR2A: " OCR2A c@ 2 u.r
cr ." TCCR2A: " TCCR2A c@ 2 u.r
cr ." TCCR2B: " TCCR2B c@ 2 u.r
cr ." TIMSK2: " TIMSK2 c@ 2 u.r
cr ." Setup finished." cr
decimal
;
: T2_COMPA_Dis
1 TIMSK2 mclr
;
: main ( -- )
setup
begin
count @ #50 >
if
." ."
flash_led
0 count !
then
key? until
;
: test ( -- )
0 count !
#1000 ms
count @ . ." counts in 1000ms" cr
;
decimal
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~