aboutsummaryrefslogtreecommitdiff
path: root/forth/forth/avr/us.fs
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2018-09-29 01:02:00 +0200
committerDimitri Sokolyuk <demon@dim13.org>2018-09-29 01:02:00 +0200
commit79cabd5b1c8bcd9f50dc49e77776e713a57dff48 (patch)
tree6eb73857e9c174610839291cf05a02d77fc82b8c /forth/forth/avr/us.fs
parent247df3bb9d4c89d4e5ff18dcc23ba1c9532f28c0 (diff)
parent110a6411bae421260476eacf6173897c1d1f4b8a (diff)
Merge branch 'master' of dim13.org:elegoo
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
+