FORTH for Arduino?

Download Report

Transcript FORTH for Arduino?

Forth for Arduino
2013 Maker Faire
May 18-19, 2013
C. H. Ting
Silicon Valley Forth Interest Group
Arduino Forth Workbench



We have two workbenches setup for
you to try Forth on Arduino.
Each workbench has an Arduino Uno
kit hooked on to a PC.
HyperTerminal running on PC controls
the Forth on Arduino.
Arduino Forth Workbench


If you have prior experience on Forth,
you can explore this Forth on Arduino
by yourself.
If you are new to Forth, here are 3
experiments you can try:



Blink LED
Sound Generator
Hello, World!
Blink LED

Type the following commands to turn
on-board LED on and off:
HEX
20 24 C!
20 23 C!
20 23 C!
;set Pin 13 as output
;toggle Pin 13
;toggle Pin 13
Sound Generator
Install a speaker on Pin 6
 Turn beeper on and off
40 2A C! ;set Pin 6 as output
42 44 C! ;set Timer0 CTC mode
FF 47 C! ;maximum count for Timer0
3 45 C! ;prescaler=3, start beeper
0 45 C! ;prescaler=0, stop beeper
2 45 C! ;prescaler=2, high pitch
4 45 C! ;prescaler=4, low pitch

Hello, World!
Add a new command HELLO:
: HELLO cr .” Hello, World! “ ;

Type:
HELLO

to execute the command HELLO you just
added.
Arduino Forth Tutorials

For 2013 Maker Faire, I prepared two demos:



Arduino Forth Clock
3 Voice Electronic Organ
There are other tutorials presented by SVFIG
members:



Forth Haiku by Brad Nelson
Kestrel II by Sam Falvo
GreenArray Processors by Dennis Ruffer
Counter-Timers in ATmega

ATmega328P has 3 counter-timers:





Counter-Timer0, 8 bits
Counter-Timer1, 16 bits
Counter-Timer2, 8 bits
Each counter-timer can generate 2
PWM waves.
One ATmega328P can drive 6 servo
motors.
Arduino Forth Clock



I use 6 servos to implement a digital
Arduino Forth Clock.
This is how a clock would look if
Arduino were invented before the
round-faced mechanical clocks.
Two digits for seconds, two digits for
minutes, and two digits for hours.
Arduino Forth Clock
The servo registers and output pins are:
( OC1A: $88, PB1, Pin 9
)
( OC1B: $8a, PB2, Pin 10
)
( OC2A: $b3, PB3, Pin 11
)
( OC2B: $b4, PD3, Pin 3
)
( OC0B: $47, PD5, Pin 5
)
( OC0A: $48, PD6, Pin 6
)

Arduino Forth Clock

Clock arms are driven to show time:
variable second
variable minute
variable hour
: servo-count ( n : show-clock
Second @ 10 /mod
minute @ 10 /mod
hour
@ 10 /mod
;
n' ) left + ;
servo-count s1 servo-count s4
servo-count s2 servo-count s5
servo-count s3 servo-count s6 ;
3 Voice Electronic Organ



The 3 counter-timers in ATmega328P can
be tuned to produce 3 square waved to
implement a 3 Voice Electronic Organ.
The frequencies of these square waves
are accurate to 1 part in 64, adequate for
a simple, low cost electronic organ.
Bach wrote two 3-part fugues in his
The Art of Fugue, BWV1080.
3 Voice Electronic Organ


Square waves are very close to sound
from pipe organs.
These fugues never sounded right until
I used a good Roland mixer-amplifier
with decent bass outputs.
3 Voice Electronic Organ
3 counters are setup in CTC mode with
prescalers of 1024, 256 and 64 to
produced voices from B2 to B6.
( OC0A: $48, PD6, Pin 6
)
( OC1A: $88, PB1, Pin 9
)
( OC2A: $b3, PB3, Pin 11
)

3 Voice Electronic Organ
3 voices are initialized
: init-ports 4 45 c!
0a 24 c! 40 2a c!
\ output ports
42 44 c! 0 45 c!
\ TCCR0A, TCCR0B
40 80 c! 0 81 c!
\ TCCR1A, TCCR1B
42 0b0 c! 0 0b1 c! ; \ TCCR2A, TCCR2B
: ch0 ( scaler note -- ) swap 47 c! 45 c! ;
: ch1 ( scaler note -- ) swap 88 c! 81 c! ;
: ch2 ( scaler note -- ) swap 0b3 c! 0b1 c! ;
: note, ( low high -- ) 100 * + ;
: ms ( n -- ) for aft 100 for next then next ;
3 Voice Electronic Organ
Notes are defined
decimal
253 2 note,
constant B1
239 2 note,
constant C2
226 2 note,
constant C2#
215 2 note,
constant D2
…
3 Voice Electronic Organ
Chords are PLAYed
: PLAY ( ... mask ms -- ) >r
dup 4 and ( base? ) if swap ?dup
if split scaler2 + ic@ else 128 0 then ch2
then
dup 2 and ( alto? ) if swap ?dup
if split scaler1 + ic@ else 128 0 then ch1
then
1 and ( superano? ) if ?dup
if split scaler0 + ic@ else 128 0 then ch0
then
r> ms ;
3 Voice Electronic Organ
Measures and Lines of Music
: M1
Z 1 3/4 A4 2 % D5 2 % D4 2 % D4 E4 F4 2 *** E4 F4
G4 2 *** ;
: M3
F4 G4 A4 2 *** G4 2 % G4 A4 B4b 2 *** A4 B4b C5 2
*** ;
: M4
B4b A4 G4 2 *** A4 2 % A4 G4 F4 2 *** G4 F4 E4 2 *** ;
: M5
F4 F4 A4 2 *** D5 D5 a4 2 *** F4 F4 E4 2 ***
D3 F4 6 *. D3 4 * G4 2 * ;
: M6
A2 A4 6 *. E4 2 * G4 2 * A3 F4 6 *. E4 2 * D4 2 *
C4# 2 *. G3 4 * F3 4 * G3 B4b 6 *. F3 4 * E3 4 * ;
: M7
F3 A4 6 *. E3 4 * D3 4 * E3 4 *. G4 2 * F4 2 *
G4 2 *. D3 4 * C3# 4 * D3 C3# B2 4 *** ;
: M8
C3# D3 E3 4 *** D3 4 *. F4 2 * E4 2 *
F4 2 *. E3 4 * F3 D5 6 * E3 C5 6 *. F3 4 * G3 B4b 6 * ;
: L1
M1 M3 M4 M5 M6 M7 M8 ;
3 Voice Electronic Organ
An entire fugue is compiled
: FUGUE13
init-ports
2000 fudge !
L1 L3 L4 L5 L6 L7 L8 L9 L10
L11 L12 L13 L14 L15 L16 L17
L18
;