You are here: Home RESOURCES SBASIC Tutorial

NanoCore12

Your Shopping Cart

Your Cart is currently empty.

SBASIC Tutorial

Introduction

As of this writing, the only BASIC compiler available for the 9S12 microcontroller is SBASIC, a freeware compiler 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.

Before starting, make sure you have read the NanoCore12 Manual or Using Your NanoCore12 QuickStart Package.  In addition to SBASIC, which runs in MS-DOS, you will be using uBug12 and TeraTermPro, which are Windows applications, so make sure that you have installed these programs on your PC.

Getting Everything Set Up

If you haven't already done so, download SBASIC.  You'll find it here: http://www.seanet.com/%7Ekarllunt/sbasic.htm.

  • Create a folder for your programs (let's call it SB4NC12) in the My Documents folder
  • On the Start menu:
    • Choose Programs
    • Choose Accessories
    • Right-click on Command Prompt
    • Choose Copy
  • Open your code folder:
    • double-click the SB4NC12 folder in My Documents
  • Right-click and choose Paste Shortcut. A new Command Prompt shortcut will appear.
  • Right-click the new shortcut and choose Properties
  • Click on the Shortcut tab, and set the following values:
    • Start in: Set this to the path of your SB4NC12 folder (for example: C:\Documents and Settings\Carl\My Documents\SB4NC12)
    • Run: Choose Normal Window
  • Click OK to close the Properties window.
  • Open the SB4NC12 folder you just created and unzip all the SBASIC files into that folder
  • Unzip the contents of as12.zip into the SB4NC12 folder as well (you'll find as12.zip here:  http://www.seanet.com/%7Ekarllunt/assem.htm)
  • Finally, copy the three files inkey12.lib, outch.lib, and reg9s12c.lib from this folder http://support.technologicalarts.ca/docs/NanoCore12/Code/SBASIC/ into your SB4NC12 folder.  Now you're ready to use SBASIC.

Creating Your First Program

The absolutely simplest program you can write will not use subroutines, interrupts, or 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.

Open your text editor (e.g. Edit or Notepad) and enter the following SBASIC program (labels Main and end should be in the first column, commands in the second or later column--  uses spaces or the TAB key):

 
Main:
 pokeb    $242,$ff    'declare all port t pins outputs
 pokeb    $240,$ff    'turn on LED
 do                   'loop forever
 loop
end

Save the above program in the SB4NC12 folder you created earlier, giving it the file name simple1.bas. (Note that file names must be in the "8.3" format: a maximum of 8 characters for the name (before the dot), and a maximum of 3 characters for the extension (after the dot).)

Double-click the Command Prompt shortcut in the
SB4NC12 folder.

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.  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 for connecting, erasing flash, and loading flash, as outlined in the NanoCore12 manual.  Verify that the LED D2 lights up when you run the program (i.e. reset NanoCore12 with the switch in Run mode) before continuing to the next example.

Below is a slightly more complicated program.  It takes advantage of the port and register definition file we have 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).  This makes it easier to write code, and also makes it easier to read later.  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 & constants for 9S12C family
 declare n 'defines a 16-bit variable which we'll use as temporary storage for a character'
Main:
 rem  'Initialize the serial port and transmit message.'
 poke  scibdh, 26   ' Set up the SCI for 9600 baud
 pokeb    scicr2,$0c   ' Enable receiver and transmitter
 print "Hello World!\n\r"  'send message to terminal screen, followed by line feed & carriage return
 do                'echo keys typed on 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

 
Click on the Command Prompt window you opened previously, 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.  The file generated by the assembler is called simple2.s19 and it is this file that you will load into NanoCore12 via uBug12, as you did before.  Then launch TeraTerm from uBug12 by entering the TERM command in uBug12's command window.   A TeraTerm window will open.  Use the pulldown menu titled Setup to configure the serial port for 9600 baud and select the COM port you are using.  Then reset NanoCore12 in Run Mode and 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.

One more example:

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

 include "reg9s12c.lib"    ' PORT definitions & constants for 9S12C family
declare    n

Main:
 pokeb    ddrt,$ff    'make all port t pins outputs
 do
 pokeb    ptt,$01        'turn on LEDs
  for n=1 to* 65000    'waste time
 next
  pokeb    ptt,$02        'toggle LEDs
  for n=1 to* 65000    'waste time
  next
 loop
end

Click on the Command Prompt window, 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 wired an LED and resistor on each of PT0 and PT1 to Ground so that you can see the results (by the way, always disconnect power before adding or modifying circuits).

By now, you should be familiar enough with using SBASIC to begin modifying the supplied examples, and begin writing your own programs.  For your convenience, there are several more examples in the Support Library, here:  http://support.technologicalarts.ca/docs/NanoCore12/Code/SBASIC/For%20NC12DXC32/

They have been written for the 32-pin module, but should be easily adaptable to the 24-pin or 40-pin modules, if necessary.

If you write some programs you'd like to share with other users, please send them to us (info_at_nanocore12_dot_com).  For each program submitted, please include a description of what it does, including details of the specific hardware configuration used.  We'll review all submissions for inclusion in a public code library.

Last Updated ( Tuesday, 15 April 2014 15:08 )  

Latest Products

USB BDM Pod for S08,HC12,S12(X),CF V1
USB BDM Pod for S08,HC12,S12(X),CF V1
USD $39.00

breakout board, SSOP28 to 28-pin DIP
breakout board, SSOP28 to 28-pin DIP
USD $1.65

breakout board, TSSOP or SOIC to 14-pin DIP
breakout board, TSSOP or SOIC to 14-pin DIP
USD $2.25

16-channel I/O Expander Chip, SPI
16-channel I/O Expander Chip, SPI
USD $2.40

16-channel I/O Expander Chip, I2C
16-channel I/O Expander Chip, I2C
USD $2.40

8-channel I/O Expander Chip, SPI
8-channel I/O Expander Chip, SPI
USD $2.20

8-channel I/O Expander Chip, I2C
8-channel I/O Expander Chip, I2C
USD $2.00

switch, DIP, 8-pos, rocker
switch, DIP, 8-pos, rocker
USD $2.00