aboutsummaryrefslogtreecommitdiff
path: root/amforth-6.5/appl/arduino/blocks/led-mega.readme
blob: cc56aff0af31bb2ec6baea2f1f4fee4603679722 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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.