You are here: Home DOCUMENTATION NanoCore12 Introduction to SBASIC for NanoCore12

Technological Arts Inc.

Your Shopping Cart

Your Cart is currently empty.

Introduction to SBASIC for NanoCore12

Introduction

SBASIC is a freeware DOS cross-compiler for HC11, HC12, and HCS12 microcontrollers created by Karl Lunt (Seattle Robotics Society).  It is for non-commercial use only, so don't use it to create applications that are intended for sale.  The compiler is very well documented, and relatively easy to use.  You'll be writing your own SBASIC programs for NanoCore12 in no time!

Before You Start

Make sure that you have read the NanoCore12 manual or QuickStart Package document appropriate for your module (i.e. Using Your NanoCore12DX QuickStart Package or Using Your NanoCore12MAX QuickStart Package), and you are familiar with how to load and run a program on the module.  You should also install TeraTermPro on your PC, or know how to use another terminal program, if you prefer it.

The Simplest Program

The simplest program you can write will not use any subroutines, interrupts, or even the serial port (SCI).  It will simply turn on an LED.  We will use absolute register addresses instead of labels so that you can get an idea of just how small an SBASIC program can be.

Setting Up the Compiler

If you haven't already done so, download and set up SBASIC.  Do this by going to the Resources page at www.nanocore12.com and look for BASIC Compilers.  There you'll find a link to SBASIC11.ZIP.  Create a folder in the root of your C: drive called SBASIC and unzip all the files into that folder.  Do the same thing with as12.zip (which you will find right above it, under Assemblers).  Lastly, copy the three files inkey12.lib, outch.lib, and reg9s12c.lib from the link entitled "NanoCore12-specific Libraries..." into your SBASIC folder.  Now you're ready to use SBASIC.

Doing the Two-step

Compiling your program to produce a loadable file is a two-step process.  First the compiler processes your SBASIC program file to produce an intermediate assembly language file.  The assembler then processes that file to produce a .s19 file, which gets loaded into the NanoCore12's program memory.

Turning on an LED

Open your text editor (e.g. Edit or Notepad) and enter the following SBASIC program:

Main:
    pokeb    $242,$1    'define PT0 pin for output
    pokeb    $240,$1    'turn on LED (on PT0)
    do                  'loop forever
    loop
end


Save the above program in the SBASIC folder you created earlier, giving it the filename simple1.bas.

Then open a Command Prompt (in Windows, do this by clicking on Start, select All Programs, then Accessories, then Command Prompt).  Enter the following commands to compile and assemble your program:

cd c:\SBASIC
sbasic simple1.bas /c4000 /v3800 /s3f80 /m6812 > simple1.asm

if no errors are reported, enter the following command
as12 simple1.asm

At this point there should be 0 errors reported.  If not, re-open simple1.bas and look for and correct any typos that you may have made.  The file generated by the assembler is called simple1.s19
, and it is this file that you will load into NanoCore12. Launch uBug12 in Windows and follow the procedure you learned in the QuickStart manual for erasing and loading a program.  In case you've forgotten them, here are the steps to follow:

  1. Ensure the switch is in the Load position, and reset your NanoCore12, then enter these commands, one by one:
  2. con 1
  3. fbulk
  4. fload ;b

Note: If you're using a com port other than com 1, use that number instead, for step 2.
When the Windows file browser pops up, select the file simple1.s19 and click OK.
  Verify that LED D2 (on Docking Module or School Board) comes on when you run the program (i.e. reset NanoCore12 in Run mode) before continuing to the next example.

Making a "Hello World!" Program

Below is a slightly more complicated program.  It takes advantage of the port and register definition file that has been created specifically for using SBASIC with the 9S12C microcontroller, called reg9s12c.lib.  When this file is used, you don't need to know absolute addresses of the various registers--  just use their names (as documented in the 9S12C Device Guide from Freescale).  Two other libraries are used as well:  INKEY12 and OUTCH12.  These are revised versions created specifically for the 9S12, and are called by the SBASIC print command.  If you are not familiar with these concepts, read the SBASIC manual to get up to speed.

Here is the program;  enter it and save it as simple2.bas:

include "reg9s12c.lib"    ' PORT definitions and constants for 9S12C family of MCUs
declare    n    'defines a 16-bit variable which we'll use as temporary storage for a character in the loop below
Main:
rem    Initialize the serial port and transmit message.
    poke  scibdh, 26          ' Set for 9600 baud
    pokeb    scicr2,$0c       ' Enable transceiver
    print "Hello World!\n\r"  'send the message to the terminal screen, followed by a line feed and carriage return
    do             'echo keys typed on the terminal keyboard to the terminal screen
          do
               n = inkey()    'wait until a key is typed on the keyboard
          loop while  n = 0
          outch  n            'send the key to the terminal screen
    loop
end

Then click on the Command Prompt window you opened earlier, and enter the following commands to compile and assemble your program:

sbasic simple2.bas /c4000 /v3800 /s3f80 /m6812 > simple2.asm
if no errors are reported, enter the following command
as12 simple2.asm

At this point there should be 0 errors reported (if not, fix the errors and try again).  The file generated by the assembler is called simple2.s19 and it is this file that you will load into NanoCore12.  Use uBug12, as before, to load the program into NanoCore12.  Then launch TeraTermPro from uBug12 by entering the TERM command in uBug12's command window.  A TeraTermPro window will open.  Use the pulldown menu titled Setup to configure the serial port for 9600 baud on the COM port you are using.  When you now reset NanoCore12 in Run mode, you will see the message Hello World! displayed in the terminal window.  Now type some characters on your keyboard and you will see them echoed in the terminal window.  If the characters you type are not displayed, click on the terminal window to make sure it is active for input (i.e. a flashing block cursor is present).  Then try typing again.

Flashing Two LEDs

Type this program into Notepad and save as simple3.bas:

include "reg9s12c.lib"   ' PORT definitions and constants for 9S12C family of MCUs
declare    n
Main:
    pokeb    ddrt,$ff    'designate all port t pins as outputs
do
    pokeb    ptt,$01     'turn on the PT0 LED
    for n=1 to* 65000    'waste some time
    next
    pokeb    ptt,$02     'toggle the LEDs on PT0 and PT1
    for n=1 to* 65000    'waste some more time
    next
loop
end


Click on the DOS Command Prompt, as before, and enter the following commands:

sbasic simple3.bas /c4000 /v3800 /s3f80 /m6812 > simple3.asm
if no errors are reported, enter the following command
as12 simple3.asm

Use uBug12 to load the resulting file (simple3.s19) into NanoCore12, as before.

After loading has finished, move switch SW2 to the Run position and reset NanoCore12.
The LEDs on the docking module or School Board should now flash alternately (like railroad crossing signals).  If you are using your own solderless breadboard, make sure you've placed an LED and resistor on each of PT0 and PT1 so that you can see the results (always disconnect power before adding or modifying circuits).

Congratulations!  You have now written three programs in SBASIC!  You'll find these programs, along with a few more, in our Support Library: http://support.technologicalarts.ca/docs/NanoCore12/Code/SBASIC/Simple/

You'll find several more advanced programs, specifically written for NC12DXC32, to implemet LED light chasers, sound generation, and serial communication in this folder:  http://support.technologicalarts.ca/docs/NanoCore12/Code/SBASIC/For%20NC12DXC32/

More on I/O Ports

Using digital input/output ports with SBASIC is fairly easy, as illustrated above.  Just use the name of the Data Direction Register for the port you are using to set the pins to the direction sense you need.  And use the Port register to write the bits you want to activate or de-activate or read the status of the input pins you are interested in.  You'll find the exact register names to use by looking in the register definitions file in your SBASIC folder, also found here:  http://support.technologicalarts.ca/docs/NanoCore12/Code/SBASIC/REG9S12C.LIB

Here's an excerpt, showing registers related to ports A and B (the register names have been highlighted):

const porta = ioregs + $00 ' i/o port a
const portb = ioregs + $01 ' i/o port b
const ddra = ioregs + $02 ' data direction reg a
const ddrb = ioregs + $03 ' data direction reg b

More on using the Serial Port

When using the serial port, just remember to initialize it first with the baud rate you need.  Here are all the registers associated with the SCI:

const scibdh = ioregs + $c8 ' baud rate registers (high and low)
const scibdl = ioregs + $c9 '
const scicr1 = ioregs + $ca ' control registers
const scicr2 = ioregs + $cb '
const scisr1 = ioregs + $cc ' status registers
const scisr2 = ioregs + $cd '
const scidrh = ioregs + $ce ' data registers (high and low)
const scidrl = ioregs + $cf '

Going Further...

A later article will go into more details of using these features, and introduce ways to make the code easier to write and to read.