|
Home Introduction Using Holon Holon Tools Contact |
Forth versus C: Serial output on the 68HC11 Let us compare a real case. This function sends a character via the serial port of the 68HC11 microcontroller. SerCout(char)
int char;
{
while(!(peekb(SCSR) & 0x80)) { } /* wait for trans data to clear out */
pokeb(SCDR,char); /* send the data out */
}
: SerCout ( char -- )
begin SCSR c@ $80 and until \ wait for old data sent
SCDR c! \ send the data out
;
The function is now used to send text that is located at memory address s. SerPrint(s)
char *s;
{
while (*s){
if (*s == ']') {
SerCout(0x0D); /* do a CR line feed for terminal */
SerCout(0x0A);
}
else SerCout(*s); /* output the character */
s++;
}
: SerPrint ( s -- )
begin dup c@ \ read the byte at addr s
while \ while byte<>0, continue
dup c@ ascii ] = \ is char = "]" ?
if $0D SerCout \ then send CR LF
$0A SerCout
else dup c@ SerCout
then 1+
repeat drop
;
Flow controlThe Forth words until and while take a value off the stack. A non-zero value is interpreted as true. -- Forth needs no block delimiters. The Forth flow control structures are delimited by words. Compare:
Driving hardware "hands-on"At first glance, the C and Forth versions of SerCout und SerPrint are equivalent. You can use one or the other. You feel the difference when you test the words. In Forth you simply load the new words into your system and add the words to the dictionary. Now you can test them interactively. You can also test the hardware directly, read and write to the peripheral registers. Example: SCSR c@ <Enter> -- shows the contents of the SCI status register in the stack window 66 SCDR c! <Enter> -- writes the character 'B' to the SCI data register. The direct control of hardware makes Forth an ideal language for writing drivers. The Holon 11 system contains a sample driver for the I2C Bus. There is much more to discuss about Forth and C. I hope that the examples help to give you a good feeling for Forth.
|
|||