328eForth Workshop C. H. Ting February 16, 2012 SVFIG
Summary Optimized FORTH for Arduino Uno ATmega328P Demonstrations BLINK a LED TONE Generator SERVO Motors TRAFFIC Controller
Optimized Forth for Arduino Subroutine Thread Model Fully optimized Uniform byte addressing Case insensitive Interpreter is in Bootloader section Optimized flash programming Visible words are reduced to 140 for ease of learning
ATmega328P 8 Bit microcontroller, 131 instructions 20 MIPS at 20 MHz 32KB Flash, 2 KB RAM, 1 KB EEPROM 3 Counter/Timers 6 Channels 10-bit ADC USART, SPI, I2C, ISP 23 Programmable I/O lines 1.8-5.5 V
BLINK a LED Toggle LED HEX 20 24 C! \ make Line 13 an output pin 20 23 C! \ toggle Line 13 LED BLINK program : MS ( n -- ) FOR AFT $1CB FOR NEXT THEN NEXT ; : BLINK 20 24 C! BEGIN 20 23 C! 400 MS AGAIN ; FLUSH BLINK
TONE Generator Timer/Counter1 Setup HEX 2 24 C! \ set Line 9 as output 40 80 C! \ set CTC mode 8000 88 !\ init compare reg 9 81 C! \ prescaler=1, start
TONE Generator : /SO A000 88 ! ; : DO 77B1 88 ! ; : RE 6AAA 88 ! ; : ME 5F07 88 ! ; : FA 59DB 88 ! ; : SO 5000 88 ! ; : LA 4745 88 ! ; : MS ( n -- ) FOR AFT $1CB FOR NEXT THEN NEXT ; : 1/4 100 MS ; : 1/2 200 MS ; : 1/8 400 MS ;
TONE Generator : PHRASE1 DO 1/4 RE 1/4 ME 1/4 DO 1/4 ; : PHRASE2 ME 1/4 FA 1/4 ME 1/4 SO 1/2 ; : PHRASE3 SO 1/8 LA 1/8 SO 1/8 FA 1/8 ME 1/4 ME 1/4 DO 1/4 ; : PHRASE1 DO 1/4 /SO ¼ ME 1/4 DO 1/2 ; : SONG 2 24 C! 40 80 C! 9 81 C! PHRASE1 PHRASE1 PHRASE2 PHRASE2 PHRASE3 PHRASE3 PHRASE4 PHRASE4 0 81 C! ;
SERVO Motors Assembling by AVR Assembler 2 Simulating by AVR Simulator 2 In System Programming with AVRISP mkll Running 328eForth from HyperTerminal
SERVO Motors HEX : INIT-PORTS E 24 C! 68 2A C! \ OUTPUT PORTS A3 44 C! 5 45 C! \ TCCR0A, TCCR0B 18 47 C! 18 48 C! \ OCR0A, OCR0B A1 80 C! D 81 C! \ TCCR1A, TCCR1B 18 88 C! 18 8A C! \ OCR1A, OCR1B A3 B0 C! 7 B1 C! \ TCCR2A, TCCR2B 18 B3 C! 18 B4 C! \ OCR2A, OCR2B ;
SERVO Motors : S1 ( N -- ) 88 C! ; : S2 ( N -- ) 8A C! ; : S3 ( N -- ) B3 C! ; : S4 ( N -- ) B4 C! ; : S5 ( N -- ) 47 C! ; : S6 ( N -- ) 48 C! ; FLUSH
TRAFFIC Controller : init-ports fc 2a c! 3 24 c! \ output ports 3f 28 c! c 25 c! ; \ input ports, pullup : seconds for aft 100 for 400 for next next then next ; : lights ( n -- ) dup 2b c! \ PD outputs 100 / C or 25 c! ; \ PB outputs, maintain pullups : switches ( -- n ) 23 c@ 100 * \ PB inputs 26 c@ or \ PC inputs dup cr . ;
TRAFFIC Controller : N-S begin 104 lights 5 seconds switches c3a and if 108 lights 2 seconds then switches a and if 130 lights 3 seconds then switches c30 and until ; : E-W begin 50 lights 5 seconds switches 82f and if 90 lights 2 seconds then switches 820 and if 310 lights 3 seconds then switches f and until ; : go init-ports begin N-S E-W ?key until drop ; flush
Turn Key Application Application Turn Key Burn Fuses : go init-ports begin N-S E-W ?key until drop ; Turn Key ‘ go 100 ! 100 erase 100 100 write Burn Fuses Lfuse=F2, Hfuse=D8, Efuse=FD
Tao of Arduino No external crystal No external reset No current limiting resistors to drive LED’s No external pull up resistors to read sensors 2 AA batteries are sufficient for power
Conclusion Arduino is the best platform to teach FORTH. ATmega328 is the simplest microcontroller for embedded applications. 328eForth is the easiest way to build applications on Arduino Uno.
為學日益,為道日損. 損之又損,以至於無. 為無為而無不為. Ardunio之道 為學日益,為道日損. 損之又損,以至於無. 為無為而無不為.