From 2f83a0bea9da444e3d70569eba3d6847ca02be03 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 21 Sep 2018 21:59:17 +0200 Subject: ... --- forth/forth/avr/us.fs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 forth/forth/avr/us.fs (limited to 'forth/forth/avr/us.fs') diff --git a/forth/forth/avr/us.fs b/forth/forth/avr/us.fs new file mode 100644 index 0000000..2cd0eda --- /dev/null +++ b/forth/forth/avr/us.fs @@ -0,0 +1,37 @@ +\ microseconds delay for Atmega +-us +marker -us + +\ Opcode only to flash +: op: ( opcode -- ) flash create , ram does> @ i, ; + +\ Atmega wdr instruction +$95a8 op: wdr, + +\ clear watchdog +: cwd [ wdr, ] ; inlined + +\ Clear watchdog (wdr instruction) takes one clock cycle +\ Adjust the number of CWD to achieve a one us delay +\ 9 CWD is needed @ 16MHz for ATmega 328 and 2560. +: us ( u -- ) \ busy wait for u microseconds + begin + cwd cwd cwd cwd cwd cwd cwd cwd cwd + 1- dup + 0= until + drop +; + +\ Helper word for calibrating the us loop +-us-cal +marker -us-cal +: us-cal ( u -- ) \ give target delay in ms + ticks >r + for #1000 us next + ticks r> - #1000 um* + cr d. ." microseconds" +; + +decimal +1000 us-cal + -- cgit v1.2.3