External Links

Creative Science Centre


Part 4 Programs & Output

You should have by now a programming environment consisting of BvSerail and the PSPad program editor, if not see Part 3 Getting started or the IDE program editor

There was the obligatory hello world program introduced in Part 3. It should be noted that this could be typed directly into BvSerial (BVS), the black window if required.

We can extend the program a bit to see what happens and explain what is going on.

function hello()
dim j
    for j = 1 to 5
        print "Hello World\n"

Copy (CTRL+C) and paste (CTRL+V) the above function into the editor window and then press F4

You will see this transfer to BVS (only the function name is shown), in BVS type hello()

To explain how this works:

All functions begin with the word ‘function’ and end with ‘endf’. Functions must have a name, in this case its ‘hello’ and the name must end in () (more on that later). In this function a local variable has been defined as j. This is an integer and is used in the for/next loop. All variables must be defined before use and the for/next loop is no exception.

The hello() function has been loaded into RAM by the above procedure. All code is initially loaded into RAM and run from there so there is no danger of ‘spoiling’ the IC. To make a more permanent function it is saved to flash, as we did with rookie.

On this next example the code is collapsed, click the expand source to see the code. It is stored on the server and this will provide an example of how to send external files or ones stored on the PC to BVS.

Do not copy and paste the program this time but 

  1. copy the green URL below into the clipboard(CTRL_C).
  2. On the editor (not the black screen) use Scripts>Open URL in Clip 
  3. click Ok
  4. Press F4 (function key 4)


// Project 2 Basic
// function returns an integer
function area(length, width)
dim a
	a = length * width
	return a

function p1()
	print "The area of a box 5 x 10 is "
	print area(5,10)

This method can be used throughout the tutorial and any project or library files as it will not only give you the program but the opportunity to save it and edit it as well.

In this example the function area will multiply together length and width and return the value. This time the brackets after the function hold parameters that are used for the calculation. The parameters are passed to the function in the function p1. The area function also returns a value by the use of the return statement.

Of course you can call this directly by typing print area(10,10)

From now on you may copy and paste into the editor as shown initially, create a project with the files in or copy the URL. It is up to you.


This deals with output which is the main work of this type of device anyway. Input is dealt with later when the LED display is working. The files can be found in the directory on the website under this heading.

The first thing to do is to connect an LED up to RA0 as shown here:

NOTE: If you have a PCB then use the port names as a reference, in this example, connecting an LED to RA0 may be on a different physical pin but would be the same reference. For devices without the specified port simply use another, for example the PIC32MX3's do not have a PORT A, so, simply use say, PORT D.

The only difference to the basic circuit is the LED. The long lead on the LED goes to pin 2, if you get it the wrong way round no harm will be done, it just won’t work.


// Output to LED on RA1
// #include "http://byvac.com/mBlib/flb/Library/rookie/MX170_rookie3_part1.bas"
// #include "http://byvac.com/mBlib/flb/Library/rookie/MX170_rookie_part2.bas"
constant LED {PORTA(PORT),1}
function go()
    // set up RA0 for output
    io_pinRole(*LED(),OUT,WOFF) // set RA0 as output
    while comkey?(2) = 0
        io_pinSet(*LED(),2) // toggle is 2

How it Works

Functions start with ‘function’ and have some name with brackets on the end, in the case ‘go()’. Because rookie is installed there are functions that will directly manipulate the I/O pins.

It is always a good idea to associate a physical entity with a port pin at the start of the file,it can then be changed later if needed, it also makes more sense in the code.

constant LED {PORTA(PORT),0}

Line 9 sets up RA0 as an output, the 'OUT' using io_pinRole(*LED(),OUT,WOFF).

The LED constant is used, note the syntax *LED(). WOFF does not mean anything for an output, it is just there to be consistent with the io_pinRole() when it is used to set the pin to an input (more later)

  • WPU - is weak pull up
  • WPD - is weak pull down
  • WOFF - is neither pull up or pull down.

The LED is activated at io_pinSet(*LED(),2), the last value '2' is a toggle.

  • 1 makes the pin high
  • 0 makes the pin low
  • 2 if the pin was high it sets it low, if it were low it sets it high

At line 10 ‘comkey?(2)’ will return the number of keys that are in the UART2 buffer. This is the UART (Universal Asynchronous Transmitter and Receiver) we are using to communicate with the IC it is set by default. Placed in a while loop it will stay there until RX is received from the host PC, in other words when you press any key on the keyboard. This enables you to get out of the function.


Page updated July 2016 because of changes to rookie (version 3) and also BvSerial Version 16_b