aboutsummaryrefslogtreecommitdiff
path: root/forth/forth/avr/us.fs
diff options
context:
space:
mode:
Diffstat (limited to 'forth/forth/avr/us.fs')
-rw-r--r--forth/forth/avr/us.fs37
1 files changed, 37 insertions, 0 deletions
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
+