23 Jan 2012 -- You can now download hex files for external code
modules, such as support libraries or standalone programs, into KLB via
the console. This means you can write dedicated modules in C or
assembly, download them into KLBasic, then run them from the
interpreter or from a stored Basic program on autostart. See
below for details on using the hex download
feature. See
below for details on
writing custom external control modules (ECMs).
I now have my KLBasic modified to run on the mbed (LPC1768)
device. KLBasic
is a tokenized interpreter built upon work done earlier by Gordon
Doughman
of Motorola for the 68hc11. Gordon released the code (in 68hc11
assembler) and I recoded it in C so it is (mostly) platform
independent. For more info on KLB in general, go back to my main
page and check out the link for KLBasic on the Atmel devices.
In the mbed implementation, KLB is a 32-bit integer Basic that reduces
your source lines into tokens, then runs the tokenized program through
an interpreter. The source is fully tokenized, including any
whitespace. When you list out your program, the KLB runtime
simply re-expands the tokenized file. Your original source is not
stored as ASCII text; it is always rederived from this list of tokens.
Here is a list of KLBasic features that are derived from Gordon's
original design:
- 32-bit integer math
- Variable names up to 14 chars long (Gordon originally used
single-letter variables)
- Single-dimension arrays using DIM statement
- Ability to save up to three programs locally in LPC1768 on-chip
flash
- Ability to autostart any program saved in flash upon reset
(Gordon originally saved to EEPROM)
- Trace support (TRON and TROFF)
- Direct access to select LPC1768 registers (GPIO, A/D, RTC, etc.)
- Ability to execute C or assembly language executables via CALL
statement
Here is a list of additional features that I've added to KLBasic:
- 8-, 16-, and 32-bit indirection operators (replace PEEK and POKE)
- Support for four down-counting timers with 1 msec resolution
- Ability to download hex files into upper flash for later
execution via CALL statement
- Time/date support via LPC1678's real-time clock subsystem
(requires 3V battery at pin VB)
- Direct printing of time and date in different formats
Here is a list of features on my to-do list:
- Add string support (but you can print literal strings from a
PRINT
statement)
- Add multidimension arrays
- Add SD card file support
KLBasic is intended for controller-type embedded applications.
Think home automation, greenhouse control, alarm systems, temperature
control, model railroading, hobby racing monitors, that kind of thing.
Known issues
The per-line internal tokenizing logic is not robust; it is possible
for the tokenizer to walk off the edge of the token buffer. I
need to fix the logic, but for now the buffer will hold 128 tokens,
which should be plenty for typical source lines.
Some examples
Since examples are always the best way to show off stuff, here is a
KLBasic program for blinking the mbed LEDs:
100 ' Program to blink a
few LEDs
110 '
200 dim leds(4)
210 dim states(4)
220 dim timers(4)
300 leds(1) = 2^18
310 states(1) = 0
320 timers(1) = addr(timer0)
350 leds(2) = 2^20
360 states(2) = 0
370 timers(2) = addr(timer1)
400 leds(3) = 2^21
410 states(3) = 0
420 timers(3) = addr(timer2)
450 leds(4) = 2^23
460 states(4) = 0
470 timers(4) = addr(timer3)
800 while 1 = 1
820 for n = 1
to 4
840
if @32 timers(n) = 0 then gosub 2000
860 next n
880 endwh
2000 '
2010 ' Subroutine to change
an LED state and rearm the
2020 ' LED timer.
Variable n holds LED index.
2030 '
2060 if states(n) = 0 then
gpio1_set = leds(n)
2080 if states(n) = 1 then
gpio1_clr = leds(n)
2100 states(n) = 1 -
states(n)
2110 p = timers(n)
2120 @32 p = rnd(2000) + 500
2190 return