Quick Guide to Getting Started

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Quick Guide to Getting Started

Post by dkinzer »

Getting started using ZBasic and ZX devices involves several elements. Firstly, you need to install the ZX device in a circuit. For the 24-pin devices, this can be as simple as plugging it into a breadboard, connecting 5 volts and ground, wiring up a DB-9 connector for the serial I/O and you're ready on that front. Other hardware options are the Parallax Boe-Bot or virtually any BS-24 compatible board. ZX devices other than the 24-pin type require quite a bit more external circuitry and for that reason they are not recommended for those just getting started.

Once you have the ZX powered up it should be (in most cases) running the program that was installed at the factory, outputting a "Hello, world" message in a loop. You should be able to see this output using a terminal emulator (like TeraTerm or similar) configured for 19.2K baud. Alternately, you can simply fire up the ZBasic IDE, select the PC COM port to which the ZX is attached ("Serial Port Options..." on the Options menu) and observe the output in the Debug window at the bottom of the main window (click on the Debug tab if it is not showing).

Once you've confirmed that the ZX is running and outputting its message, you can move along to creating your first project. Here is an elementary "Hello, world" program in ZBasic:

Code: Select all

Sub Main()
  Debug.Print "Hello, world!"
End Sub
In order to compile this program and download it to your device, follow the steps below. The description assumes that you have a ZX properly connected on a prototyping board of some type, that a serial connector is properly wired and that it is connected to a PC's COM1 port by a serial cable.
  • Launch the ZBasic IDE.
  • Select "New..." from the Project menu
  • In the edit box of the New Project dialog, type the name of the project, for example, hello.pjt. You may want to change the directory where the project file will be located by using the Browse... button on that dialog. Leave the checkboxes checked and click the OK button when ready to proceed.
  • Note that the main source code file, hello.bas for example, is automatically created and an empty Main() subroutine is prepared in that file. Add a new second line containing the Debug.Print command from the example above.
  • From the Options menu, select "Device Options..." and in the resulting dialog, select the device name corresponding to the ZX device that you have, e.g. ZX-24a. Click the OK button when ready to proceed.
  • If the ZX is connected to a COM port on the PC other than COM1, you'll need to select the right COM port. Do this by selecting "Serial Port Options..." from the Options menu and then choosing the right COM port from the dropdown box labeled "Serial Port". Press OK when ready to continue.
  • Compile your program by pressing the F7 key or by selecting Compile from the Project menu. In the Output window at the bottom of the IDE, you should see that the compile completed with no errors. If an error message appears, recheck the program to verify that it matches the sample above and compile it again.
  • Download the compiled program the the ZX by pressing the F5 key or by selecting Go from the Project menu. If the download completes successfully, you should see the "Hello, world!" in the Debug window at the bottom of the IDE.
Once you've successfully compiled and downloaded this simple program, you can start expanding it to do more. For example, the program below adds a loop and delay for periodic output.

Code: Select all

Sub Main()
  Do
    Debug.Print "Hello, world!"
    Call Sleep(1.0)
  Loop
End Sub
If you aren't familiar with the Basic language, you should spend some time reading Chapter 2 of the ZBasic Language Reference Manual. That chapter gives an overview of the ZBasic language syntax, control structures, etc. You should also skim the "Routines by Category" section of the ZBasic System Library Reference Manual. This will give you an overview of the types of subroutines and functions that are available.

A possible next step would be to add toggling of an I/O line to the loop. The code below alternately sets an I/O pin high and low, in this case a pin connected to the on-board red LED.

Code: Select all

Sub Main()
  Dim state as Byte
  state = 0
  Do
    Debug.Print "Hello, world!"
    Call PutPin(Pin.RedLED, state)
    If (state = 0) Then
      state = 1
    Else
      state = 0
    End If
    Call Sleep(1.0)
  Loop
End Sub
- Don Kinzer
Post Reply