aboutsummaryrefslogtreecommitdiff
path: root/amforth-6.5/appl/arduino/blocks
diff options
context:
space:
mode:
Diffstat (limited to 'amforth-6.5/appl/arduino/blocks')
-rw-r--r--amforth-6.5/appl/arduino/blocks/led-mega.frt38
-rw-r--r--amforth-6.5/appl/arduino/blocks/led-mega.readme114
-rw-r--r--amforth-6.5/appl/arduino/blocks/ports-leonardo.frt74
-rw-r--r--amforth-6.5/appl/arduino/blocks/ports-mega128.frt104
-rw-r--r--amforth-6.5/appl/arduino/blocks/ports-standard.frt49
-rw-r--r--amforth-6.5/appl/arduino/blocks/test_danger_shield.fs437
-rw-r--r--amforth-6.5/appl/arduino/blocks/wiring_analog.frt65
7 files changed, 881 insertions, 0 deletions
diff --git a/amforth-6.5/appl/arduino/blocks/led-mega.frt b/amforth-6.5/appl/arduino/blocks/led-mega.frt
new file mode 100644
index 0000000..ada5b36
--- /dev/null
+++ b/amforth-6.5/appl/arduino/blocks/led-mega.frt
@@ -0,0 +1,38 @@
+\ let the led at digital-13 aka PortB.7 blink
+
+\
+$25 constant PORTB
+$24 constant DDRB
+
+\ initialize the Port: change to output mode
+: led-init
+ $80 DDRB c!
+;
+
+\ turn the led on
+: led-on
+ $80 PORTB c!
+;
+
+\ turn the led off
+: led-off
+ 0 PORTB c!
+;
+
+\ let led blink once
+: led-blink
+ led-on 500 ms led-off 500 ms
+;
+
+\ let led blink until a keystroke
+: blink
+ ." press any key to stop "
+ begin
+ led-blink
+ key?
+ until
+ key drop \ we do not want to keep this key stroke
+;
+
+\ and do it....
+led-init blink
diff --git a/amforth-6.5/appl/arduino/blocks/led-mega.readme b/amforth-6.5/appl/arduino/blocks/led-mega.readme
new file mode 100644
index 0000000..cc56aff
--- /dev/null
+++ b/amforth-6.5/appl/arduino/blocks/led-mega.readme
@@ -0,0 +1,114 @@
+The example for the blinking LED works on every arduino with a LED
+attached to Digital-13. It is tested on a arduino mega only however.
+
+What does the code do? It lets the LED blink and
+gives some hints for using and enjoying amforth.
+
+
+First: it defines a few constants:
+
+$25 constant PORTB
+$24 constant DDRB
+
+The arduino uses its own numbering schema for pins, but
+for now we use the atmega ones: digial-13 is the same as
+bit 7 of PORT-B. Port B has three registers, we need only
+two of them: The Data Direction Register (DDR) and the PORT
+(Output) Register. The third register is used for reading
+from the port (PIN).
+
+To quickly test the hardware enter the following commands
+
+$80 DDRB c! $80 PORTB c! <enter>
+
+The led turns on. With
+
+0 PORTB c!
+
+the led turns off. You can repeat these commands and watch the LED.
+
+The next step is to define some commands and use them. And add some
+more features that makes live easier.
+
+Forth usually uses many small words which do exactly one thing.
+When entering forth commands take care that every word is
+seperated by at least one space. In forth almost every character
+can be used as part of a command name.
+
+The first command in this example sets up the Data Direction Register
+to make the LED Port an output pin. In arduino sketch it would be
+void setup() { pinMode(13, OUTPUT); }
+
+: led-init $80 DDRB c! ;
+
+By entering the command line the interpreter will learn a new command:
+led-init. This command can be called immediatly after the command prompt
+says OK. And it can be used in further command definitions.
+
+It writes the 8bit number 128 (hex 80) to the register DDRB (hex 24)
+as defined above. This makes the 7th bit of PORTB an Output pin.
+
+Calling our newly defined word does not change anything
+visible. But with the next word, the LED will turn on
+
+: led-on $80 PORTB c! ;
+
+Here the 7th bit will be set to 1, and that makes the led to be connected
+to VCC (5V) and it will turn on (the LED is connected to ground already).
+
+If the led-on command does not turn on the LED just call the
+led-init command (again). The led-init is needed after an reset
+or power cycle as well.
+
+Now that the led is active, we want a command to turn it off. One solution
+is to repeat the command from above: 0 PORTB c! . Smarter is a new command
+word:
+
+: led-off 0 PORTB c! ;
+
+You can now use the newly defined commands to turn the led on and off:
+
+ led-on led-off led-on led-off
+
+(since there is no timing involved yet, you may not even see the led glow)
+
+Our next word will simplify this, saves many keystrokes, and gives the
+real blink experience:
+
+: led-blink
+ led-on 500 ms led-off 500 ms
+;
+
+Calling this command will turn the led on, waits half a second, turn it
+off again and waits again half a second before returning to the command
+prompt.
+
+With a command line like
+
+led-blink led-blink led-blink
+
+The led will blink for a few seconds.
+
+To make it blink "forever", the next word is helpful
+
+: blink-forever
+ ." press any key to stop "
+ begin
+ led-blink
+ key?
+ until
+ key drop \ we do not want to keep this key stroke
+;
+
+This wird prints some text ("press any key to stop) and starts a loop.
+This loop lets the led blink one and checks for a keystroke. If no key
+is pressed, the loops is repeated. If a key is pressed, the loop is
+finished. The last two commands are housekeeping: read the key pressed
+and forget it. Otherwise the key pressed would be the first character
+of the next command line.
+
+The advantage of defining many words is that you can test them immediatly.
+Thus any further code can rely on words already being tested and that
+makes debugging alot easier. The drawback of that many words? You need
+more code space for the names of the commmands. There is no real speed
+penalty however.
diff --git a/amforth-6.5/appl/arduino/blocks/ports-leonardo.frt b/amforth-6.5/appl/arduino/blocks/ports-leonardo.frt
new file mode 100644
index 0000000..54d1117
--- /dev/null
+++ b/amforth-6.5/appl/arduino/blocks/ports-leonardo.frt
@@ -0,0 +1,74 @@
+\
+\ port definitions for Atmegas as found on the Arduino Standard
+\ Atmega168, Atmega328p
+\
+decimal
+
+
+};
+
+PORTD 2 portpin: digital.0
+PORTD 3 portpin: digital.1
+PORTD 1 portpin: digital.2
+PORTD 0 portpin: digital.3
+PORTD 4 portpin: digital.4
+PORTC 6 portpin: digital.5
+PORTD 7 portpin: digital.6
+PORTE 6 portpin: digital.7
+
+PORTB 4 portpin: digital.8
+PORTB 5 portpin: digital.9
+PORTB 6 portpin: digital.10
+PORTB 7 portpin: digital.11
+PORTD 6 portpin: digital.12
+PORTC 7 portpin: digital.13
+
+PORTB 3 portpin: digital.14
+PORTB 1 portpin: digital.15
+PORTB 2 portpin: digital.16
+PORTB 0 portpin: digital.17
+PORTF 7 portpin: digital.18
+PORTF 6 portpin: digital.19
+
+PORTF 5 portpin: digital.20
+PORTF 4 portpin: digital.21
+PORTF 1 portpin: digital.22
+PORTF 0 portpin: digital.23
+PORTD 4 portpin: digital.24
+PORTD 7 portpin: digital.25
+PORTB 4 portpin: digital.26
+PORTB 5 portpin: digital.27
+PORTB 6 portpin: digital.28
+PORTD 6 portpin: digital.29
+
+PORTD 5 portpin: TXLED
+PORTB 0 portpin: RXLED
+PORTE 2 portpin: HWB
+
+\ some digital ports have an alternative use
+\ synonym is available since amforth 5.0
+
+synonym SPI:SS digital.17
+synonym SPI:MOSI digital.16
+synonym SPI:MISO digital.14
+synonym SPI:SCK digital.15
+
+synonym TWI:SDA digital.2
+synonym TWI:SCL digital.3
+synonym SERIAL:RX digital.0
+synonym SERIAL:TX digital.1
+synonym LED_BUILTIN digital.13
+
+synonym analog.0 digital.18
+synonym analog.1 digital.19
+synonym analog.2 digital.20
+synonym analog.3 digital.21
+synonym analog.4 digital.22
+synonym analog.5 digital.23
+
+synonym analog.6 digital.24
+synonym analog.7 digital.25
+synonym analog.8 digital.26
+synonym analog.9 digital.27
+synonym analog.10 digital.28
+synonym analog.11 digital.29
diff --git a/amforth-6.5/appl/arduino/blocks/ports-mega128.frt b/amforth-6.5/appl/arduino/blocks/ports-mega128.frt
new file mode 100644
index 0000000..61a32df
--- /dev/null
+++ b/amforth-6.5/appl/arduino/blocks/ports-mega128.frt
@@ -0,0 +1,104 @@
+\
+\ port definitions for Atmega128 as found on the Arduino Mega128
+\
+decimal
+PORTE 0 portpin: digital.0 \ PE 0 ** 0 ** USART0_RX
+PORTE 1 portpin: digital.1 \ PE 1 ** 1 ** USART0_TX
+PORTE 4 portpin: digital.2 \ PE 4 ** 2 ** PWM2
+PORTE 5 portpin: digital.3 \ PE 5 ** 3 ** PWM3
+PORTG 5 portpin: digital.4 \ PG 5 ** 4 ** PWM4
+PORTE 3 portpin: digital.5 \ PE 3 ** 5 ** PWM5
+PORTH 3 portpin: digital.6 \ PH 3 ** 6 ** PWM6
+PORTH 4 portpin: digital.7 \ PH 4 ** 7 ** PWM7
+PORTH 5 portpin: digital.8 \ PH 5 ** 8 ** PWM8
+PORTH 6 portpin: digital.9 \ PH 6 ** 9 ** PWM9
+PORTB 4 portpin: digital.10 \ PB 4 ** 10 ** PWM10
+PORTB 5 portpin: digital.11 \ PB 5 ** 11 ** PWM11
+PORTB 6 portpin: digital.12 \ PB 6 ** 12 ** PWM12
+PORTB 7 portpin: digital.13 \ PB 7 ** 13 ** PWM13
+PORTJ 2 portpin: digital.14 \ PJ 1 ** 14 ** USART3_TX
+PORTJ 0 portpin: digital.15 \ PJ 0 ** 15 ** USART3_RX
+PORTH 2 portpin: digital.16 \ PH 1 ** 16 ** USART2_TX
+PORTH 0 portpin: digital.17 \ PH 0 ** 17 ** USART2_RX
+PORTD 3 portpin: digital.18 \ PD 3 ** 18 ** USART1_TX
+PORTD 2 portpin: digital.19 \ PD 2 ** 19 ** USART1_RX
+PORTD 1 portpin: digital.20 \ PD 1 ** 20 ** I2C_SDA
+PORTD 0 portpin: digital.21 \ PD 0 ** 21 ** I2C_SCL
+PORTA 0 portpin: digital.22 \ PA 0 ** 22 ** D22
+PORTA 1 portpin: digital.23 \ PA 1 ** 23 ** D23
+PORTA 2 portpin: digital.24 \ PA 2 ** 24 ** D24
+PORTA 3 portpin: digital.25 \ PA 3 ** 25 ** D25
+PORTA 4 portpin: digital.26 \ PA 4 ** 26 ** D26
+PORTA 5 portpin: digital.27 \ PA 5 ** 27 ** D27
+PORTA 6 portpin: digital.28 \ PA 6 ** 28 ** D28
+PORTA 7 portpin: digital.29 \ PA 7 ** 29 ** D29
+PORTC 8 portpin: digital.30 \ PC 7 ** 30 ** D30
+PORTC 6 portpin: digital.31 \ PC 6 ** 31 ** D31
+PORTC 5 portpin: digital.32 \ PC 5 ** 32 ** D32
+PORTC 4 portpin: digital.33 \ PC 4 ** 33 ** D33
+PORTC 3 portpin: digital.34 \ PC 3 ** 34 ** D34
+PORTC 2 portpin: digital.35 \ PC 2 ** 35 ** D35
+PORTC 1 portpin: digital.36 \ PC 1 ** 36 ** D36
+PORTC 0 portpin: digital.37 \ PC 0 ** 37 ** D37
+PORTD 7 portpin: digital.38 \ PD 7 ** 38 ** D38
+PORTG 2 portpin: digital.39 \ PG 2 ** 39 ** D39
+PORTG 1 portpin: digital.40 \ PG 1 ** 40 ** D40
+PORTG 0 portpin: digital.41 \ PG 0 ** 41 ** D41
+PORTL 7 portpin: digital.42 \ PL 7 ** 42 ** D42
+PORTL 6 portpin: digital.43 \ PL 6 ** 43 ** D43
+PORTL 5 portpin: digital.44 \ PL 5 ** 44 ** D44
+PORTL 4 portpin: digital.45 \ PL 4 ** 45 ** D45
+PORTL 3 portpin: digital.46 \ PL 3 ** 46 ** D46
+PORTL 2 portpin: digital.47 \ PL 2 ** 47 ** D47
+PORTL 1 portpin: digital.48 \ PL 1 ** 48 ** D48
+PORTL 0 portpin: digital.49 \ PL 0 ** 49 ** D49
+PORTB 3 portpin: digital.50 \ PB 3 ** 50 ** SPI_MISO
+PORTB 2 portpin: digital.51 \ PB 2 ** 51 ** SPI_MOSI
+PORTB 1 portpin: digital.52 \ PB 1 ** 52 ** SPI_SCK
+PORTB 0 portpin: digital.53 \ PB 0 ** 53 ** SPI_SS
+PORTF 0 portpin: digital.54 \ PF 0 ** 54 ** A0
+PORTF 1 portpin: digital.55 \ PF 1 ** 55 ** A1
+PORTF 2 portpin: digital.56 \ PF 2 ** 56 ** A2
+PORTF 3 portpin: digital.57 \ PF 3 ** 57 ** A3
+PORTF 4 portpin: digital.58 \ PF 4 ** 58 ** A4
+PORTF 5 portpin: digital.59 \ PF 5 ** 59 ** A5
+PORTF 6 portpin: digital.60 \ PF 6 ** 60 ** A6
+PORTF 7 portpin: digital.61 \ PF 7 ** 61 ** A7
+PORTK 0 portpin: digital.62 \ PK 0 ** 62 ** A8
+PORTK 1 portpin: digital.63 \ PK 1 ** 63 ** A9
+PORTK 2 portpin: digital.64 \ PK 2 ** 64 ** A10
+PORTK 3 portpin: digital.65 \ PK 3 ** 65 ** A11
+PORTK 4 portpin: digital.66 \ PK 4 ** 66 ** A12
+PORTK 5 portpin: digital.67 \ PK 5 ** 67 ** A13
+PORTK 6 portpin: digital.68 \ PK 6 ** 68 ** A14
+PORTK 7 portpin: digital.69 \ PK 7 ** 69 ** A15
+
+\ some digital ports have an alternative use
+\ synonym is available since amforth 5.0
+
+synonym analog.0 digital.54
+synonym analog.1 digital.55
+synonym analog.2 digital.56
+synonym analog.3 digital.57
+synonym analog.4 digital.58
+synonym analog.5 digital.59
+synonym analog.6 digital.60
+synonym analog.7 digital.61
+synonym analog.8 digital.62
+synonym analog.9 digital.63
+synonym analog.10 digital.64
+synonym analog.11 digital.65
+synonym analog.12 digital.66
+synonym analog.13 digital.67
+synonym analog.14 digital.68
+synonym analog.15 digital.69
+synonym SPI:SS digital.53
+synonym SPI:MOSI digital.51
+synonym SPI:MISO digital.50
+synonym SPI:SCK digital.52
+synonym TWI:SDA digital.20
+synonym TWI:SCL digital.21
+synonym LED_BUILTIN digital.13
+
+synonym SERIAL:RX digital.0
+synonym SERIAL:TX digital.1 \ No newline at end of file
diff --git a/amforth-6.5/appl/arduino/blocks/ports-standard.frt b/amforth-6.5/appl/arduino/blocks/ports-standard.frt
new file mode 100644
index 0000000..5f2e2de
--- /dev/null
+++ b/amforth-6.5/appl/arduino/blocks/ports-standard.frt
@@ -0,0 +1,49 @@
+\
+\ port definitions for Atmegas as found on the Arduino Standard
+\ Atmega168, Atmega328p
+\
+decimal
+
+PORTD 0 portpin: digital.0
+PORTD 1 portpin: digital.1
+PORTD 2 portpin: digital.2
+PORTD 3 portpin: digital.3
+PORTD 4 portpin: digital.4
+PORTD 5 portpin: digital.5
+PORTD 6 portpin: digital.6
+PORTD 7 portpin: digital.7
+
+PORTB 0 portpin: digital.8
+PORTB 1 portpin: digital.9
+PORTB 2 portpin: digital.10
+PORTB 3 portpin: digital.11
+PORTB 4 portpin: digital.12
+PORTB 5 portpin: digital.13
+
+PORTC 0 portpin: digital.14
+PORTC 1 portpin: digital.15
+PORTC 2 portpin: digital.16
+PORTC 3 portpin: digital.17
+PORTC 4 portpin: digital.18
+PORTC 5 portpin: digital.19
+
+\ some digital ports have an alternative use
+\ synonym is available since amforth 5.0
+synonym SPI:SS digital.10
+synonym SPI:MOSI digital.11
+synonym SPI:MISO digital.12
+synonym SPI:SCK digital.13
+synonym TWI:SDA digital.18
+synonym TWI:SCL digital.19
+synonym LED_BUILTIN digital.13
+synonym SERIAL:RX digital.0
+synonym SERIAL:TX digital.1
+synonym analog.0 digital.14
+synonym analog.1 digital.15
+synonym analog.2 digital.16
+synonym analog.3 digital.17
+synonym analog.4 digital.18
+synonym analog.5 digital.19
+\ not on all chips but defined in arduino sources
+synonym analog.6 digital.20
+synonym analog.7 digital.21
diff --git a/amforth-6.5/appl/arduino/blocks/test_danger_shield.fs b/amforth-6.5/appl/arduino/blocks/test_danger_shield.fs
new file mode 100644
index 0000000..0b6d493
--- /dev/null
+++ b/amforth-6.5/appl/arduino/blocks/test_danger_shield.fs
@@ -0,0 +1,437 @@
+\ 2011-03-06 EW
+\ test arduino duemilanove + danger shield
+
+\ hw layout
+\ arduino | atmega328p | danger shield
+\ D0 | PD0 rx |
+\ D1 | PD1 tx |
+\ D2 | PD2 int0 |
+\ D3 | PD3 int1 oc2b | bz (buzzer)
+\ D4 | PD4 t0 | sr_in (shift register DS)
+\ D5 | PD5 t1 oc0b | led1
+\ D6 | PD6 oc0a | led2
+\ D7 | PD7 | sr_latch (shift register /OE)
+\ |
+\ D8 | PB0 icp | sr_clk (shift register SH_CP)
+\ D9 | PB1 oc1a |
+\ D10 | PB2 /ss oc1b | sw1 (switch)
+\ D11 | PB3 mosi oc2a | sw2 (switch)
+\ D12 | PB4 miso | sw3 (switch)
+\ D13 | PB5 sck |
+\ |
+\ A0 | PC0 adc0 | sl3 (slider)
+\ A1 | PC1 adc1 | sl2 (slider)
+\ A2 | PC2 adc2 | sl1 (slider)
+\ A3 | PC3 adc3 | light (photo cell)
+\ A4 | PC4 adc4 scl | temp (temperature)
+\ A5 | PC5 adc5 sda | knock (buzzer 2)
+
+\ make marker
+
+marker --start--
+
+decimal
+
+PORTB 2 portpin: sw1
+PORTB 3 portpin: sw2
+PORTB 4 portpin: sw3
+
+PORTD 5 portpin: led1
+PORTD 6 portpin: led2
+
+PORTD 3 portpin: bz
+
+PORTC 2 portpin: sl1
+PORTC 1 portpin: sl2
+PORTC 0 portpin: sl3
+
+PORTC 3 portpin: photocell
+PORTC 4 portpin: thermometer
+PORTC 5 portpin: knocksensor
+
+PORTD 4 portpin: sr_in
+PORTD 7 portpin: sr_oe \ output enable
+PORTB 0 portpin: sr_cl
+
+variable 1delay 20 1delay !
+: msg_quit
+ ." press switch 1 (D10) to quit" cr
+;
+
+
+\ --- switches -----------------------------------------------
+: sw1?
+ sw1 pin_low? if
+ 20 ms \ very simple debounce
+ sw1 pin_low? if
+ -1
+ else
+ 0
+ then
+ else
+ 0
+ then
+;
+
+\ --- buzzer -------------------------------------------------
+
+\ 2 ms T_period =^= 500 Hz
+: buzz ( cycles -- )
+ 0 ?do bz low 1ms bz high 1ms loop
+;
+
+\ --- analog digital converter -------------------------------
+\ --- adc ---
+
+: or! dup c@ rot or swap c! ;
+
+\ pin>pos
+\ convert bitmask of portpin: back to value (bitposition)
+: pin>pos ( pinmask portaddr -- pos )
+ drop ( -- pinmask )
+ log2 ( -- pos_of_most_significant_bit )
+;
+
+: adc.init ( -- )
+ \ ADMUX
+ \ A_ref is NOT connected externally
+ \ ==> need to set bit REFS0 in register ADMUX
+ [ 1 5 lshift \ ADLAR
+ 1 6 lshift or \ REFS0
+ ] literal ADMUX c!
+ \ ADCSRA
+ [ 1 7 lshift \ ADEN ADC enabled
+ 1 2 lshift or \ ADPS2 prescaler = 128
+ 1 1 lshift or \ ADPS1 .
+ 1 or \ ADPS0 .
+ ] literal ADCSRA c!
+;
+: adc.init.pin ( bitmask portaddr -- )
+ over over high
+ pin_input
+;
+
+1 6 lshift constant ADSC_MSK \ ADStartConversion bitmask
+: adc.start
+ \ start conversion
+ ADSC_MSK ADCSRA or!
+;
+: adc.wait
+ \ wait for completion of conversion
+ begin
+ ADCSRA c@ ADSC_MSK and 0=
+ until
+;
+: adc.channel! ( channel -- )
+ 7 and \ clip channel to 0..7
+ ADMUX c@ 7 invert and \ read ADMUX, clear old channel
+ or \ add new channel
+ ADMUX c! \ write
+;
+: adc.get10 ( channel -- a )
+ adc.channel! adc.start adc.wait
+\ 10 bit
+ ADCL c@
+ ADCH c@ 8 lshift + 6 rshift
+;
+: adc.get ( channel -- a )
+ adc.channel! adc.start adc.wait
+\ 8 bit
+ ADCH c@
+;
+
+\ --- shift register -----------------------------------------
+
+\ --- shift register ---
+
+: bit>sr ( bit -- )
+ if sr_in high else sr_in low then
+ sr_cl high noop sr_cl low noop
+;
+
+: get.bit ( byte pos -- bit )
+ 1 swap lshift \ -- byte bitmask
+ and \ -- bit
+;
+
+\ clock one byte out, MSB first!
+: byte>sr ( byte -- )
+ 8 0 do
+ dup 7 i - \ 7 6 5 ... 0: MSB first!
+ get.bit
+ bit>sr
+ loop
+ drop
+;
+
+: >7seg
+ invert
+ byte>sr
+ sr_oe low noop sr_oe high
+;
+
+
+create HexDigits
+$3f , \ 0
+$06 , \ 1
+$5b , \ 2
+$4f , \ 3
+$66 , \ 4
+$6d , \ 5
+$7d , \ 6
+$07 , \ 7
+$7f , \ 8
+$6f , \ 9
+$77 , \ A
+$7c , \ b
+$58 , \ c
+$5e , \ d
+$79 , \ E
+$71 , \ F
+
+$80 constant dec.point
+: emit.7seg ( n -- )
+ dup 0 $F within if
+ HexDigits + i@ >7seg
+ else
+ drop
+ then
+;
+
+\ --- convert thermometer reading --------------------------
+
+: >T
+ 51 -
+ 100 256 */
+ 25 +
+;
+
+: .T
+ thermometer pin>pos adc.get dup . space >T . cr
+;
+
+
+\ --- test functions ---------------------------------------
+: test_switches
+ ." press switch 2,3 to light up led 1,2" cr
+ msg_quit
+
+ begin
+ sw2 pin_low? if led1 high else led1 low then
+ sw3 pin_low? if led2 high else led2 low then
+ sw1? until
+;
+
+: test_buzzer
+ ." press switch 2 (D11) to test buzzer" cr
+ msg_quit
+ begin
+ sw2 pin_low? if 500 buzz then
+ sw1? until
+;
+
+: test_sliders
+ ." move sliders" cr
+ msg_quit
+ begin
+ sl1 pin>pos adc.get 4 u0.r space space
+ sl2 pin>pos adc.get 4 u0.r space space
+ sl3 pin>pos adc.get 4 u0.r $0d emit
+ 1delay @ ms
+ sw1? until
+ cr
+;
+
+: test_photocell
+ ." light/shadow photocell" cr
+ msg_quit
+ begin
+ photocell pin>pos adc.get 4 u0.r $0d emit
+ 1delay @ ms
+ sw1? until
+ cr
+;
+
+: test_thermometer
+ ." warm/cool thermometer" cr
+ msg_quit
+ begin
+ thermometer pin>pos adc.get 4 u0.r $0d emit
+ 1delay @ ms
+ sw1? until
+ cr
+;
+
+
+: test_bits.7seg
+ 8 0 do
+ 1 i lshift >7seg
+ 500 ms
+ loop
+;
+: test_emit.7seg
+ $10 0 do
+ i emit.7seg
+ 500 ms
+ loop
+;
+: test_7seg
+ ." show single segments on 7seg" cr
+ test_bits.7seg
+ 1000 ms
+ ." show hex numbers on 7seg" cr
+ test_emit.7seg
+ 1000 ms
+;
+
+\ --- main: init, run --------------------------------------
+: init
+ 20 1delay !
+
+ led1 pin_output
+ led2 pin_output
+ bz pin_output
+
+ sw1 pin_input
+ sw2 pin_input
+ sw3 pin_input
+
+ adc.init
+ sl1 adc.init.pin
+ sl2 adc.init.pin
+ sl3 adc.init.pin
+ photocell adc.init.pin
+ thermometer adc.init.pin
+ knocksensor adc.init.pin
+
+ sr_in high sr_in pin_output
+ sr_oe high sr_oe pin_output
+ sr_cl low sr_cl pin_output
+ $ff >7seg
+;
+
+
+\ --- pwm: timer/counter0, led1,2 ---
+\ D5 | PD5 t1 oc0b | led1
+\ D6 | PD6 oc0a | led2
+\ timer/counter0
+\ fast pwm mode
+\ TCCR0A bits
+\ . COM0A[1,0] = 1,0 (non inverted mode)
+\ . COM0B[1,0] = 1,0 (non inverted mode)
+\ . WGM0[1,0] = 1,1 (mode3: fast pwm)
+\ TCCR0B bits
+\ . CS[2,1,0] = 0,1,1 (clk_io/64)
+
+\ TIMSK0 TIFR0 TCNT0 TCCR0B TCCR0A OCR0B OCR0A
+
+: pwm.leds.init
+ 0 TCNT0 c! \ clear counter
+ led1 high led1 pin_output
+ led2 high led2 pin_output
+ \ TCCR0A = COM0A1 | COM0B1 | WGM01 | WGM00
+ %10100011 TCCR0A c!
+ \ TCCR0B = CS1 | CS0
+ %00000011 TCCR0B c!
+;
+\ control brightness via registers
+\ OCR0A (D6)
+\ OCR0B (D5)
+
+variable pwm5
+variable pwm6
+: test_leds_pwm
+ pwm.leds.init
+ 0 pwm5 !
+ $ff pwm6 !
+ begin
+ pwm5 @ 1+ $00ff and dup pwm5 ! OCR0B c!
+ pwm6 @ 1- $00ff and dup pwm6 ! OCR0A c!
+ 10 ms
+ key? until
+
+;
+
+: test_leds_pwm_slider
+ pwm.leds.init
+ \ adc.init
+ \ sl1 adc.init.pin
+ \ sl2 adc.init.pin
+ begin
+ sl1 pin>pos adc.get OCR0B c!
+ sl2 pin>pos adc.get OCR0A c!
+ 1 ms
+ key? until
+;
+
+
+\ --- pwm: timer/counter2, buzzer ---
+\ D3 | PD3 int1 oc2b | bz
+
+\ timer/counter2
+\ clear timer on compare match, ctc mode
+\ TCCR2A bits
+\ . COM2B[1,0] = 0,1 (non inverted mode)
+\ . WGM2[1,0] = 1,0 (mode2: ctc)
+\ TCCR2B bits
+\ . CS[2,1,0] = 1,1,1 (clk_t2s/256)
+
+\ TIMSK0 TIFR0 TCNT0 TCCR0B TCCR0A OCR0B OCR0A
+
+: pwm.bz.init
+ 0 TCNT2 c! \ clear counter
+ bz high bz pin_output
+
+ \ TCCR2A = COM0B0 | WGM01
+ %00010010 TCCR2A c!
+ \ TCCR2B = CS1 | CS0
+ %00000110 TCCR0B c!
+;
+\ control frequency via register
+\ OCR2A
+
+: test_bz_pwm
+ pwm.bz.init
+ $20 OCR2A c! 200 ms
+ $30 OCR2A c! 200 ms
+ $40 OCR2A c! 200 ms
+ $0 OCR2A c!
+;
+
+
+\ --- --- ---
+
+variable state
+8 constant max_state
+
+: run
+
+ init
+ 0 state !
+
+ ." press switch 1 (D10) for next test" cr
+ begin
+
+ sw1? if
+ state @ 1+
+ dup max_state > if drop 0 then
+ dup state !
+ . cr
+ then
+
+\ state @ 0 = ( do nothing )
+
+ state @ 1 = if test_switches then
+ state @ 2 = if test_buzzer then
+ state @ 3 = if test_sliders then
+ state @ 4 = if test_photocell then
+ state @ 5 = if test_thermometer then
+ state @ 6 = if test_7seg 1 state +! then
+
+ \ wait some
+ 1delay @ 5 * ms
+
+ key? until
+;
+
+\ fin
diff --git a/amforth-6.5/appl/arduino/blocks/wiring_analog.frt b/amforth-6.5/appl/arduino/blocks/wiring_analog.frt
new file mode 100644
index 0000000..e3faa9a
--- /dev/null
+++ b/amforth-6.5/appl/arduino/blocks/wiring_analog.frt
@@ -0,0 +1,65 @@
+\ analog read functions for arduino.
+\
+\ usage
+\ once (in turnkey): adc.init (sets up the subsystem)
+\ repeated: analog.X analog_read ( -- n)
+\
+
+\ \\\\\\\\\\\\\\\\\\\\\\\\\\\
+\ HELPER ROUTINES \
+\ \\\\\\\\\\\\\\\\\\\\\\\\\\\
+\ pin>channel
+\ convert bitmask of portpin: back to value (bitposition)
+: pin>channel ( pinmask portaddr -- pos )
+ drop ( -- pinmask )
+ log2 ( -- pos_of_most_significant_bit )
+;
+
+: adc.init ( -- )
+ \ ADMUX
+ \ A_ref is NOT connected externally
+ \ ==> need to set bit REFS0 in register ADMUX
+ [ 0 5 lshift \ ADLAR off, makes read operation simpler
+ 1 6 lshift or \ REFS0
+ ] literal ADMUX c!
+ \ ADCSRA
+ [ 1 7 lshift \ ADEN ADC enabled
+ 1 2 lshift or \ ADPS2 prescaler = 128
+ 1 1 lshift or \ ADPS1 .
+ 1 or \ ADPS0 .
+ ] literal ADCSRA c!
+;
+: adc.init.pin ( bitmask portaddr -- )
+ over over high
+ pin_input
+;
+
+1 6 lshift constant ADSC_MSK \ ADStartConversion bitmask
+: adc.start
+ \ start conversion
+ ADSC_MSK ADCSRA high
+;
+: adc.wait
+ \ wait for completion of conversion
+ begin
+ ADCSRA c@ ADSC_MSK and 0=
+ until
+;
+: adc.channel! ( channel -- )
+ 7 and \ clip channel to 0..7
+ ADMUX c@ 7 invert and \ read ADMUX, clear old channel
+ or \ add new channel
+ ADMUX c! \ write
+;
+
+: adc.get ( namedpin -- a )
+ pin>channel adc.channel! adc.start adc.wait
+ ADC @ \ always 10bit
+;
+
+\ make sure the ports are set up and do one
+\ conversion.
+: analog_read ( pinmask portaddr -- n )
+ 2dup adc.init.pin
+ adc.get
+;