SeaBass is a Basic-like language that outputs C code. This allows you to write programs using a simple Basic-like language, but get the compatibility and optimization power of C. Unlike some Basic language systems, SeaBass programs can easily interface with existing C and assembly language code. You can even mix C programming calls in your SeaBass program if you like.
This article will show you how to use SeaBass with the APP-IV (our Atmel ATmega development system) and an APP-II (our PIC development system). However, SeaBass generates C code that most compilers can handle. In addition, SeaBass has a flexible build process that you can customize to automatically invoke the compiler of your choice. We've even used SeaBass to write PC-based programs by using a PC C compiler!
Consider this small SeaBass program:
` Example SeaBass Program ` Sends a little Morse code on an LED on Port B pin 0 include "avrio.bh" ` get I/O routines cinclude "app4delay.h" ` get delay routines (from C) #link "app4delay.c" ` include C library ` Define a string type deftype string char * ` Speed of a dot in milliseconds Const speed=200 ` play a dot sub dot() HIGH(B,0) ` LED on delay_ms(speed) LOW(B,0) ` LED off delay_ms(speed) end sub ` play a dash sub dash() HIGH(B,0) delay_ms(speed*3) LOW(B,0) delay_ms(speed) end sub ` pause between characters sub space() delay_ms(speed*3) end sub ` Main program function main() as int dim text as string dim i text="-.-. --.-" ` message to send ` Set LED to output OUTPUT(B,0) ` Main code top: for i=0 to strlen(text)-1 ` walk through text ` do the right thing if text[i]='.' then dot() end if if text[i]='-' then dash() end if if text[i]=' ' then space() end if next delay_ms(10000) ` wait 10 seconds goto top ` and do it again end function
This program defines four subroutines and functions:
main - The main program (all SeaBass programs have a main)
space - Delay for a bit
dot - Output a Morse code dot
dash - Output a Morse code dash
If you understand the top of the program, the rest of it falls right into place:
include "avrio.bh" ` get I/O routines cinclude "app4delay.h" ` get delay routines (from C) #link "app4delay.c" ` include C library ` Define a string type deftype 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. 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 type deftype 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 ` do the right thing 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?
Notice that the string is just an array (as it is in C). In SeaBass, like C, array subscripts use square brackets. So text gives you the third character in the text string (the first character is at index 0).
This short program provides you with a glimpse of many powerful SeaBass features. In particular:
Simple Basic-like syntax
Ability to include and call SeaBass libraries
Ability to use C libraries and functions
Access to C strings and other types
Transparent sharing of resources with C
The previous example used the APP-IV because the GNU C compiler has so much support for it. However, the GNU C compiler doesn't work with the PIC. However, there is an open source compiler for the PIC (SDCC; see this article). SeaBass will work with this compiler just as well as it will with GNU C.
Here's a simple PIC program:
` simple test program for APP-II cinclude "app2.h" cinclude "app2lib.h" ` delay using the APP-II's delay ` routine sub wait() dim i for i=1 to 10000 delay(255) next end sub sub main() TRISC=0xF7 ` make LED an output (0x indicates hex number) blink_loop: APP2_LED=1 ` turn on LED wait() ` wait a bit APP2_LED=0 ` turn off wait() ` wait... goto blink_loop ` do it over and over (could use a while loop here) end sub
This code uses the built-in APP2 functions defined in app2.h and app2lib.h to generate delays. Note that the program has access to PIC registers (like TRISC).
One thing to keep in mind is that SeaBass has the same limitations as the underlying C compiler you are using. For example, SDCC (by default) does not allow reentrant functions (that is, functions that call themselves recursively). Therefore, SeaBass has the same default limitation.
SeaBass opens up many processors to the Basic programmer. Although SeaBass makes programming simpler, it also allows you to use C libraries, functions, and even C code where necessary. This allows you to ease into C programming when you need to use it, but stay in a comfortable Basic dialect for rapid application development.
Since SeaBass will work with practically any C compiler, your investment in learning SeaBass will carry over to other processors. You can use SeaBass on a variety of hardware platforms -- even the PC if you want to develop workstation code.
seabass.htm - Main page for SeaBass
app2kit.htm - APP-II Kit (PIC)
app4kit.htm - APP-IV Kit (AVR)
Site contents © 1997-2014 by AWC, 310 Ivy Glen, League City, TX 77573 (281) 334-4341