BASIC is the simplest language and must not be dismissed because it is old.
It's true it has some limitations (modern compilers do not have many
limitations) but it is excellent when you are starting out...
There's even a free one called GCBASIC.
BASIC was the first language that I ever used and PIC BASIC is BASIC optimized
for the PIC Micro. OK I'll date myself a bit here - MZ80K, Spectrum, Amstrad -
the heady days of the Z80 based computer which all used BASIC.
BASIC has improved over the years getting rid ofline number based listing and using the
procedure based method common to other languages.
This fact alone makes PIC BASIC much easier to use e.g. instead of GOSUB 9810 (a
line number) you can use GOSUB abc where abc is the name of a subroutine.
But it has not changed that much and this explains its popularity - you can
use the BASIC that you learnt years ago and program advanced PIC Micros.
There are
several PIC BASIC systems and these are either interpreter or compiler
based.
Jump to Interpreted or
Compiled ?
Jump to Interpreter, Pros, Cons.
Jump to Compiler, Pros, Cons.
If you are
new to programming, PIC BASIC is also a very easy introduction to
microcontroller programming as the language is not difficult to master.
Sidebar : GOTO
Note: Use of
GOTO is not considered good programming practice.
The reason is that when you use
it you have no way of getting back to where you started. The program's flow
is completely disrupted and you are dependent on the routine that you are in to
get you to the next program operation. (This is ok for a small test program
but hopeless for anything large).
You need to use GOSUB - better to just not use GOTO at all. A procedure that
you have called using GOSUB will let you go back to the caller at any time
using RETURN. This method is equivalent to procedure/function calls in all
other languages (Yes C does have a goto statement but everyone avoids it at all
cost).
Using subroutines structures your code for easy reading and maintenance as you
can use your main procedure at the top level - if you think of your program as
a root system that branches down. Each branch represents a single
subroutine and side branches off of these represent more subroutines.
There are two
flavors of BASIC for PIC Micros:
Here's a
comparison between Interpreted and Compiled PIC BASIC :
PIC BASIC Type | Interpreted | Compiled |
Output | Tokens | Hex file |
Speed | Slow | Fast |
Ease of use | Very easy. | More difficult. |
Executer Size | Depends. | Zero |
Program Size | Should be expandable to any size (if using external eeprom). | Limited to microcontroller internal memory size. |
Cost | High (if doing lots of projects) | Low (if doing lots of projects) |
Examples | picaxe, Basic Stamp | PICBasic, MikroBasic |
An interpreted
language uses a controlling program to read a set of tokens and translate these
tokens into machine code on-the-fly. As each token is interpreted the machine
code is executed.
For each instruction e.g. FOR, RETURN, GOSUB etc. a unique code is assigned
to
the token -
this is simply a number. These are read from memory and interpreted
sequentially.
Note: Special methods are used to distinguish
between 'Tokens' and a number e.g. a WORD.
The key points here are :
The interpreter
is a
controlling program that interprets the tokens and it has to be present in the
microcontroller and it will be quite large - so you loose some of the internal program memory of the PIC.
The amount of memory you loose depends on the commands supported and in some
cases this does not matter as the tokens are loaded into a separate external
memory (serial eeprom) while in others only internal memory is used .
The process of
interpreting a token takes the following steps:
The important
point is that there are many steps and each one takes time so with an
interpreted basic you will never be able to drive it as fast as
using assembler (machine code) or using a compiled language.
The saving grace
for a BASIC interpreter is that it is easy to use and the one feature that gives
it an edge over a compiler based language is:
They work using the PIC feature of self programming where the PIC can write to
its own internal (Flash) program memory (not the EEPROM).
Note: This is effectively a bootloader with added 'BASIC' functions. It
bootloads the program into internal memory or I2C serial EEPROM and executes
it.
Its slow and in
the long run it costs you more.
Also some features may not be available e.g. interrupts - you need to carefully
check the documentation before buying it.
Note: picaxe may be an exception as it is
priced a little bit more than the cost of the chip (ok it still costs more than
the chip).
A PIC BASIC
interpreter is the easiest way to start
programming with microcontrollers.
The compiler
is the favored method of program generation in industry as it gives you
complete control over the microcontroller butthe Clanguageusually used.
Separate
programmer and programming software running on the PC.
This adds more complexity to programming the chip. i.e. you have either a
serial or parallel cable (or USB) to the programmer circuit and then an ICSP connector to your target board and you use PC programming software to stuff the
data into the microcontroller.
Note: There is a way around this - by using a
bootloader - this consumes a small amount of internal memory (but is not as big
as the interpreter). But you still need a programmer to get the bootloader into
the Micro in the first place - this is what you buy when you buy a BS2 or
picaxe i.e. a bootloader and interpreter combined.
A PIC BASIC
compiler gives the maximum speed and best use of internal program memory and
resources.
Also, once you have bought the compiler you do not need to buy any other module
(usually a costly component) you just buy the raw microcontroller chip.
Note: Costs can mount up if you use
interpreted BASIC modules. e.g. if an interpreter module costs $50 each and
you do ten projects it will cost $500 (just for the module). If you buy a
compiler $200 say and do ten projects (with a $10 microcontroller) then you
spend a total of $300. So if you intend to do a lot of project work it's
worth investing in a good compiler (but check picaxe price).
There's a free open source PIC BASIC
compiler here.
Jump from PIC BASIC page to
Best-Microcontroller-Projects Home Page.
Comments
Have your say about what you just read! Leave me a comment in the box below.
Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.