Project:Nanode/Tiny Basic

From London Hackspace Wiki
Jump to: navigation, search

Tiny Basic running on Nanode

We are all familiar with the process of editing code for the Arduino or Nanode, recompiling the code and then uploading the new hex code image up to the target board. Whilst this might be the way most applications for embedded microntrollers are developed these days - there is another way - and for some people it might just be what they need to get started.

Tiny Basic is an interpreted language which runs on most microcontrollers. It needs few resources apart from a serial terminal interface and is an ideal alternative language for the Arduino or Nanode. It can be customised to include functionality that is specific to the Nanode hardware, and work alongside the drivers that are used to exercise the hardware peripherals.

Nanode Tiny Basic is written in C, and so is reasonably compact at just under 8K bytes. The version we use was developed by Mike Field - - and he should be credited for his fine work everytime the code is modified or distributed.


As computers have got more powerful with more memory and processor resources, much of the early work done with simple basic has been forgotten - but for a whole generation of programmers - Basic was their first introduction to real computer programming.

The simplest of programs can be entered from a serial terminal window - and can be executed immediately using the RUN command - for example:

10 PRINT "London Hackspace is fab!"

20 GOTO 10

Most programmers aged 30 or over were introduced to programming with a similar first program - it is the "Hello World" of BASIC programming.

30 years ago - almost every home computer you could buy had some sort of Basic interpreter running in the background.

Origins of Tiny Basic

BASIC stands for Beginners All Purpose Symbolic Instruction Code and was developed around 1963/64 at Dartmouth College. Over the next 10 years it was ported to most of the mini and mainframe computers.

In 1975 a very snot-nosed Bill Gates and Paul Allen developed a version of Basic to run on the new Altair 8800 home computer This was the birth of Micro Soft and set Bill Gates out on his entrepreneurial career. The genius however was Paul Allen, who simulated the whole 8080 assembly language program on a minicomputer - without ever having touched an Intel 8080 microprocessor system - however this is a digression.

Our version of Tiny Basic hails from 1976, when members of the Homebrew Computer Club of Menlo Park - in Northern California's Silicon Valley were looking around for a simple and compact interpreted language that would run on their homemade Altair 8800 machines - and did not want to pay the young entrepreneur William Gates $150 for his version of 8080 Basic. So a challenge went out to the members to write their own - and several did, the most notable was Tom Pittman. .

Tom's original code has been adapted for many different platforms over the years - some written in native assembly language and some ported to C - so that it might be platform independent.

This is something I have wanted to do for some time - run an interpreted Tiny Basic on the Nanode - so that newcomers can program it quickly and easily.

This would make Nanode a lot more accessible to novice programmers - and could have an important role to play in helping to teach kids the basics of programming and computer science - just like we learned 30 years ago - on simple 8 bit machines running basic.

A Nanode as a web connected platform sells for just £25, or fully built, tested and expanded to include micro SD card, Realtime Clock and wireless transceiver for just £40. (That happens to be what I paid for a ZX81 kit in 1983).

The latest Nanode really is evolving into a small computer system, with it's 32K SRAM and the micro SD card. There should be the means to run programs out of these memory devices, and use the SD card like a hard disk for file storage and retrieval - and tasks such as datalogging.

The first task was to find a program which can be used like an operating system - in order to tie all the various hardware functions and libraries together.

Tiny Basic for Arduino - and Nanode!

I recently was made aware of an assembly language version of Tiny Basic that compiled on an AVR (eg ATmega328) into under 4K - but modifying this code was going to be a little to intense for most people - so I was delighted to see that Mike Field had taken the generic C version and updated it so that it can run on an Arduino - or Nanode - without modification.

Mike's working port of Tiny Basic - written in C, compiles into just under 7.8K on a standard Arduino. If you crank the baudrate up to 115200 - it is surprisingly quick at executing.

Arduino Basic Download

As it's written in C, and uses simple tables of tokens or keywords, it is easily extendable to write new keywords and functions which exercise the Nanode hardware. Additionally there is plenty program space left - about 22K, into which the various library functions for ethernet, SD card, RTC, MAC and SRAM may be added.

Whilst it currently executes code from internal RAM, this could probably be redirected to the external 32K SRAM - into which we can TFTP a simple basic program listing - or access the SD card - which we can use as a local repository or "juke box" of our favourite sketches.

Extending this Tiny Basic and including the common Arduino libraries to form an Nanode operating system (NanOS ?)will be an interesting but achievable task.

The Tiny Basic plus all the necessary hardware libraries fit into just 13K of program space leaving 17K for language extensions and application code. The breakthrough will be to get the SRAM and the SD working as a program application memory and solid state disk.

With the Tiny Basic is a means to list the program, so that lines of code can easily be edited with a terminal program. - or probably a whole file loaded using a file transfer program such as Hyperterminal or whatever.

A simple text editor could also be used to edit and manipulate html text, stored on SD or SRAM - so that web pages could be locally edited and then displayed on a browser.

Usability, Speed and Extensions

The Tiny Basic is certainly fast enough to be usable, especially with the baudrate at 57600 - I did 10,000 iteration loops of

10 For A = 0 to 9999

20 Print "My Name is Joe"

30 Next A

In just 30 seconds - anyone who remembers the early 1980s machines like the Spectrum will appreciate this is several times quicker.

It should be straight forward to get the Nanode (Arduino) I/O pins accessible from basic keywords - perhaps something like defining each pin as a keyword.

As there are only 20 I/O pins on a ATmega - it's not going to take much program space to code them up. eg

10 DOUT 4 = 1 // Set Dig 4 High

20 DOUT 6 = LOW // Set Dig 6 Low (alternative to 0) - Turns Nanode LED on...

30 Sleep 2000 // Wait 2 seconds

40 DOUT 6 = HIGH // Turn off Nanode LED

50 Let A = AIN(1) // Get input from Analogue 1 (reads 0 to 1023)

60 Print A

70 Let B = A / 4 // re-range it to 0-255

80 AOUT 5 = B // set PWM (Analogue Write) on Dig 5

The Version of Tiny Basic with these analogue and digital I/O extensions can be found on this Github Gist.

Notes : Current version only works on pins 0 to 9 - (ie not D10 - D13)

List of KeyWords

The Basic functions are implemented as a list of reserved Keywords. When the interpreter comes across a Keyword in a line of code it strips off any associated parameters and jumps to the subroutine that executes the code behind that Keyword function. It then returns control to the interpreter which then handles and executes the next line of code.

Our Version of TB has the following Keywords (KW) - but more are easily added to extend the language.

  1. define KW_LIST 0
  2. define KW_LOAD 1
  3. define KW_NEW 2
  4. define KW_RUN 3
  5. define KW_SAVE 4
  6. define KW_NEXT 5
  7. define KW_LET 6
  8. define KW_IF 7
  9. define KW_GOTO 8
  10. define KW_GOSUB 9
  11. define KW_RETURN 10
  12. define KW_REM 11
  13. define KW_FOR 12
  14. define KW_INPUT 13
  15. define KW_PRINT 14
  16. define KW_POKE 15
  17. define KW_STOP 16
  18. define KW_BYE 17
  19. define KW_DOUT 18
  20. define KW_AOUT 19
  21. define KW_SLEEP 20
  22. define KW_DEFAULT 21