PIC32 Getting Started

Introduction

With the introduction of BV-Basic V2.xx some changes have been made that effect this page, if you need to refer to the old page it  has been archived here.

This is the interface foundation for the BV513 which is a PIC32 based microcontroller board with built in SD card and filing system. There is a separate hardware guide for this device and that will be necessary to refer to to follow the projects in this guide.

The purpose of the guide is to help the user become familiar with the PIC32-Basic system and also how to connect the BV513 to the real world.

Getting Started

At this point it is assumed that you have a working BV513 connected to BV-Com and have the ok prompt thus: See also the Movies section for a demonstration of this

Sign on screenshot
PIC32-Basic Prompt (version numbers of course will be different)

If not go to the BV513 Hardware Guide and follow the instructions for getting to here.

The BV513 will run with or without an SD card, however in the above screenshot it can be seen that the card is available for this board.

The Language Basics

PIC32-Basic roughly follows BASIC syntax and so most of what you or may have learnt using other BASIC variants will apply here. To get something out for example use ‘print’ and to get something from the user, ‘input is used. All built in basic words are called statements, keywords or commands and are entered in lower case. The case is important as this language is case sensitive, if ‘Print’ is entered for the ‘print’ statement an error will occur.

The language is also fully interactive, try this: (NOTE: 'PI' is in upper case)
print PI*(3^2)
result: 28.274333
or
a#=log10(25)
print 10^a#
result: 25.000000

The first result is the area of a circle 6 in diameter. Most statements can be simply entered at the keyboard to get an immediate result.

Program One

The basic element of programming in this language is the function. The hello world example is here:

function hw
    print “Hello World”
endf

If the above is carefully entered it will create a function called ‘hw’. To run the function simply type hw.

sub hellow world

This screenshot shows how it is done, note how the prompt changes form ‘ok’ to ‘>’ when in the middle of writing a function.
This method of programming, entering text at the console is a bit impractical as one mistake means that the function needs re-writing. A better method is to write the program on an attached PC using say Notepad or another program editor PSPad is very good. See the PSPad section on how to set up this code editor for PIC32-Basic.

Downloading Programs

It is recommended that PSPpad editor be used for programming PIC32-Basic as there is a syntax file called BV-BASIC.INI that should be placed in the syntax subdirectory where PSPad is installed. This colours the program vey nicely and also indicates at an early stage if any of the keywords are wrong.

syntax highlighting
This is what it looks like in PSPad. As you can see from this: // is used to comment the code which is a slight departure form the normal REM or single quote.

See the videos on how to use tload (Text Load) which may be simper then the next section.

Down load the above program to RAM using tload and run by simply typing in the function name 'hw;

Program 2

This is a slightly more complex program called ‘circle.bas’:

function circle
dim dia#,area#,c#,rad#
    input "Circle diameter: " dia#
    rad#=dia#/2
    area#=PI*(rad#^2)
    c#=PI*dia#
    print "Dia",dia#
    print "Area",area#
    print "Circumference",c#
endf

This program introduces variables. Variables do not need to be created with ‘dim’ before use but it is good practice to do so. Using dim as above means that the variables used are completely local to this function. There are a few different types of variable depending on what you wish to store. Working with microcontrollers, most of the time this will be whole numbers (integers) however in a mathematical calculation as above floating point numbers are more appropriate.

The type of variable is designated by the post fix symbol as follows:

Variable Symbol Examples
Character & a& fred& ar&[3]
Float # a# fred# r#[3]
Unsigned % a% fred% r%[3]
String $ a$ fred$ r$[3]
Integer   a fred r[3]

In this case ‘#’ is used to designate the variable being of type floating point. One further observation is that this language is case sensitive and ‘pi’ is in upper case ‘PI’. If lower case ‘pi’ were used the result would be 0 as it would be interpreted as a new variable.

Parameters

The previous program asks the user for the diameter but suppose this was part of another larger program where the diameter was already obtained.

function circle1(dia#)
dim area#,c#,rad#
    rad#=dia#/2
    area#=PI*(rad#^2)
    c#=PI*dia#
    print "Dia",dia#
    print "Area",area#
    print "Circumference",c#
endf

Here in ‘circle1’ notice that the input line has been removed and the function definition is:
function circle1(dia#)

The function ‘circle1’ now expects the diameter to be in the input stream, this will be collected and fed into the variable dia#. To use this function:
circle1 5

The above is used, 5 will be the diameter of the circle.

As well as receiving data a function can also return data.

function area#(dia#)
    dim rad#
    rad#=dia#/2
    result PI*(rad#^2)
endf

The ‘result’ keyword is used to return a value but also notice that the name of the function ‘area#’ has the postfix ‘#’. This is important as it will return a floating point value. The name of functions are given the same postfix as variables when returning values. – Try re-writing the function it with a name of ‘area’ (no postfix) and it will return a whole number.
To use this function you need the ‘print’ keyword to see what is returned.

print area# 5

Or alternatively assigning the returned value to a variable:

a#=area# 5

Using functions like this form the basis of the language. By writing ‘functions’ it is possible to extend the language and create a whole set of new keywords for your application. A robotics application may have keywords like move, arm, shoulder etc. These can be used in a wider program. Writing code this way may improve reliability as each element could be tested. Also a new keyword can be made of several functions, it is not necessary to restrict it to one.

Welcome.bas

Try loading this file (in the downloads section) using any method. The main function is 'go', after loading type 'go' and see what happens.

Some Useful Words

NOTE ALL KEYWORDS ARE IN lower case THE LANGUAGE IS CASE SENSITIVE.
new – clears that ram ready to start again
tload - load text into the RAM program area
load and run program from SD Card to RAM
dir – displays SD Card directory
cd – changes to a subdirectory
type “file name” – lists the contents of a file to screen
sys – displays system information
dump – displays contents of memory