PIC BASIC

Beginners All Purpose Symbolic Instruction Code for PICS. 

BASIC is a good choice 
when you are
starting out...

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 InterpreterPros, Cons.

Jump to CompilerPros, 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.

I expect that later on you can move onto other languages as the only disadvantage of PIC BASIC is that sometimes, for advanced programming, it limits what you can do.

In the past at work I have been asked to program in an ancient version of BASIC and it is extremely frustrating as you tend to rely on the features of the program i.e. you start programming because it claims to be able to do something.  Once you get there you find you would like to add a tiny extra operation and you can not!  There is simply not enough functionality in the functions provided and you can not add any more.  

C tends to let you do more as most of the functions are themselves written in C - this basically means that if a function is not available you just write it yourself.

This is why the 'C' language is so popular you can do virtually anything you need to do in 'C'.  Also you can structure the program more easily and this lets you more easily understand how the program works but PIC BASIC is still powerful and modern versions let you do much more - even adding your own assembler code.

Note: Even with advanced programming limitations you can still do an enormous amount using PIC BASIC.




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.


PIC BASIC : Interpreted or compiled - What is the difference ?

There are two flavors of BASIC for PIC Micros:

  • Interpreted PIC BASIC
  • Compiled PIC BASIC

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

Interpreted PIC BASIC

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 :

  • controlling program.
  • on-the-fly.
  • ease of use.

Interpreted PIC BASIC : Controlling program

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 .

Interpreted PIC BASIC : On-the-fly

The process of interpreting a token takes the following steps:

  1. Get token from the memory store (could be external serial EEPROM).
  2. Translate to machine code (stored within the interpreter).
  3. Jump to and execute the interpreter code.
  4. Return to fetch the next token.

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.

Interpreted PIC BASIC : Ease of use

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:  

You only need a serial cable to program it (e.g. picaxe).


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.

Interpreted PIC BASIC : Disadvantage ?

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).

Interpreted PIC BASIC : Advantage ?

A PIC BASIC interpreter is the easiest way to start programming with microcontrollers.

Compiled PIC BASIC

A PIC BASIC compiler, although the front end tools look virtually identical to a PIC BASIC interpreter, outputs either assembler code or a hex file that you can directly load into your PIC Micro.  There are no tokens and no intermediate steps so you can drive the Microcontroller at the fastest possible speed fetching instructions from internal memory and executing them immediately.


The compiler directly outputs executable code.


The compiler is the favored method of program generation in industry as it gives you complete control over the microcontroller butthe Clanguageusually used.

PIC BASIC Compiler : Disadvantage ?

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.

PIC BASIC Compiler : Advantage ?

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.




Privacy Policy | Contact | About Me

Site Map | Terms of Use