Is it Basic or is it C? Its SEABASS

February 18th, 2008 by wd5gnr

Personally, I like to program in C. Luckily, there is pretty good C language support for most modern microprocessors. However, if you don’t know C, its a pretty steep learning curve. Sometimes you just want to bang something out quick.

If your processor wants you to use C but you want to use Basic, you might try Seabass. Seabass is a BASIC compiler that outputs C code. So anywhere you can compile a C program (even on Linux or Windows) you can write BASIC programs. Even better, Seabass integrates with existing C libraries and you can even embed C code in your program.

The link shows an entire article about using Seabass to produce some Morse code on an Atmel AVR and a PIC. Here’s the first part of the AVR program:

include "avrio.bh"       ` get I/O routines
cinclude "app4delay.h"   ` get delay routines (from C)
#link "app4delay.c"      ` include C library

` Define a string typedef
type string char *

` Speed of a dot in milliseconds
Const speed=200

The first line includes a basic header file (by convention, these files have a .bh extension). This is a SeaBass file (included on the CDROM) that provides some common I/O routines for the APP-IV (the target board). This line allows us to use things like HIGH and LOW to affect the I/O pins.

The second line includes a C file, not a SeaBass file. This file is one that the APP-IV kit supplies and provides an easy way to write delays. Notice the line just under this tells the compiler where to find the associated C file. If you are manually compiling SeaBass’ output, this isn’t necessary, but if SeaBass controls the build process, this will allow it to automatically bring in the correct C code that your program needs.

Type names in SeaBass have to be a single word. However, many C types use multiple words (or even symbols). For example, if you want to deal with a string, in C you use a character pointer represented by “char *”. The SeaBass DefType statement allows you to make a SeaBass type that represents a complex C type:

` Define a string typedef
type string char *

The final part of the initial part of the program sets a constant using Const. In this case, the speed value is set to 200 (this will be the number of milliseconds to delay for a dot).

Now it is very simple to understand the main code:

` Main program
function main() as int
dim text as string
dim i
text="-.-. --.-"   ` message to send
` Set LED to output
OUTPUT(B,0)

The first two lines in the function define variables (notice one of them is a string).

Here’s the main loop:

` Main code
top: for i=0 to strlen(text)-1
` walk through text
     if text[i]=’.’ then
	dot()
     end if
     if text[i]=’-’ then
	dash()
     end if
    if text[i]=’ ‘ then
      space()
    end if
 next

Notice that the program uses the C library call strlen to find the length of the string. Of course, you could write a version of strlen in SeaBass, but since the C compiler already has this function, why not use it?

You can read the whole article or look at an example with an LCD library. You can also play with a demo of Seabass online.

Add This! BlogLines del.icio.us Digg Diigo DZone Facebook Google Google Reader Yahoo! MyWeb Netscape Netvouz reddit SlashDot Sphere StumbleUpon Technorati

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.