Skip to main content

PicBasic and PicBasic Pro programming

BASIC is one of the oldest and one of the easiest programming languages to learn. You should be able to learn and program in BASIC in less than an hour. In this chapter, we shall be looking at the principles of programming PIC microcontrollers using the PicBasic and PicBasic Pro lan- guages. Both these languages are very similar to the standard BASIC language but they have some modified and some additional instructions specifically for microcontroller programming.

Both PicBasic and PicBasic Pro languages have been developed by the microEngineering Labs Inc. PicBasic is a lower-cost, simpler language than PicBasic Pro and it is aimed at students and hobbyists. PicBasic Pro is more expensive, aimed at professionals, and includes additional com- mands for more advanced instructions.

Table gives a list of the comparison of PicBasic and PicBasic Pro languages. Before we pro- ceed to the chapter on PIC applications and projects, we shall be looking at how we can program the PIC microcontrollers using these languages. 

PicBasic language

In this section, we shall be looking at the variable types and the commands of the PicBasic lan- guage. A detailed description of all the commands can be found in the PicBasic Compiler man- ual, available from the web site www.melabs.com, or a printed copy can be obtained from the microEngineering Labs Inc. 

PicBasic variables

Variables are used to store temporary data in a program. These variables are stored in the general- purpose area of the RAM memory of a microcontroller.

Variables in PicBasic can be bytes (8 bits), or words (16 bits). Byte variables are named B0, B1, B3, etc., and word variables are named W0, W1, W2, etc. Word variables are made up of two bytes. For example, W0 uses the same memory space as bytes B0 and B1. Similarly, W1 word variable is made up of bytes B2 and B3, and so on. We can access the bit positions of variables B0 and B1 using predefined names Bit0, Bit1,…,Bit15. For example, the least significant bit of B0


Comparison of PicBasic and PicBasic Pro

PicBasic
PicBasic Pro
Low-cost ($99.95)
Higher cost ($249.95)
Limited to first 2 K of program space
No program space limit
Interrupt service routine in assembly language
Interrupt service routine can be in assembly language or in PicBasic Pro
Peek and Poke used to access registers
Registers can be accessed directly by specifying their names
Some commands can be used only for
PORTB, PORTC, or GPIO
Commands can be used for all ports
Clock speed 4 MHz
Any clock speed up to 40 MHz
Most 14-bit Pic microcontrollers supported
All PIC microcontrollers, including 12-bit ones are supported
More code space in memory
5–10% less code space in memory
More difficult to learn and less powerful
Easier to learn and more powerful
No LCD commands
Special LCD control commands (LCDOUT, LCDIN)
No hardware serial communication commands
Special hardware serial communications commands (HSERIN, HSEROUT)
No PWM commands
Special PWM commands for the microcontrollers that have built-in PWM circuit (HPWM)
No Select-Case command
Select-Case command for multi-way selection
No program memory read–write commands
Commands to read and write program memory locations (READCODE, WRITECODE)
No One-wire device interface
One-wire device interface commands (OWIN, OWOUT)
No USB commands
USB commands for microcontrollers that have built-in USB circuits (USBIN, USBOUT)
No X-10 remote control commands
X-10 remote control commands (XIN, XOUT)
No A/D commands
A/D commands for microcontrollers that have built-in A/D converters (ADCIN)


is labelled Bit0, the second bit Bit1, and the most significant bit as Bit7. Similarly, the least significant bit of B1 can be named as Bit8, and the most significant bit of B1 as Bit15.

Variables are stored in the RAM memory of a PIC microcontroller where B0 is the first RAM location, B1 is the second RAM location, and so on. The size of the RAM memory depends on the type of PIC microcontroller used and Table 4.2 gives a list of the variable names for various microcontrollers. For example, if we are using a PIC16F84-type microcontroller, we can define 52 variables from B0 to B51, and the highest variable name must not exceed B51. Note that you can only access RAM locations up to the available RAM. For example, if you try to access a RAM location that does not exist, the compiler does not generate an error and your program may not work as expected.

PicBasic variable names


Microcontroller
Variables (bytes)
Variables (words)
PIC16C61
B0–B21
W0–W10
PIC16C71
B0–B21
W0–W10
PIC16C710
B0–B21
W0–W10
PIC16F83
B0–B21
W0–W10
PIC16C84
B0–B21
W0–W10
PIC16F83
B0–B21
W0–W10
PIC12F629
B0–B47
W0–W23
PIC12F675
B0–B47
W0–W23
PIC16F630
B0–B47
W0–W23
PIC16F676
B0–B47
W0–W23
PIC16C711
B0–B51
W0–W25
PIC16F84
B0–B51
W0–W25
PIC16C554
B0–B63
W0–W31
PIC16C556
B0–B63
W0–W31
PIC16C620
B0–B63
W0–W31
PIC16C621
B0–B63
W0–W31
PIC 12C67X
B0–B79
W0–W39
PIC14C000
B0–B79
W0–W39
PIC16C558
B0–B79
W0–W39
PIC16C558
B0–B79
W0–W39
PIC16C622
B0–B79
W0–W39
PIC16C62
B0–B79
W0–W39
PIC16C63
B0–B79
W0–W39
PIC16C64
B0–B79
W0–W39
PIC16C65
B0–B79
W0–W39
PIC16C72
B0–B79
W0–W39
PIC16C73A
B0–B79
W0–W39
PIC16C74A
B0–B79
W0–W39


The relationships between the byte, word, and bit variables are given in Table 4.3. For example, word W2 is made up of bytes B4 and B5. You will see additional predefined variables in Table 4.3, named Port, Dirs, and Pins. Pins refers to the PORTB hardware, Dirs refers to the port data direction register for PORTB, i.e. TRISB and a 0 sets its associated Pin to an input, and a Dirs of 1 sets its associated Pin to an output.
Port is a word variable that combines Pins and Dirs. The individual pins of a port can be accessed by the variable names Pin0, Pin1,…,Pin7.

Relationship between byte, word, and bit variables

Word variable
Byte variable
Bit variable
W0
B0
Bit7, Bit6,…Bit0
B1
Bit15, Bit14,…Bit8
W1
B2

B3

W2
B4

B5

W3
B6

B7




W39
B78

B79

Port
Pins
Pin7, Pin6,…Pin0
Dirs
Dir7, Dir6,…Dir0


Symbols

In order to make programs more readable, we can assign meaningful names to variables, instead of using B0, B1, etc. The PicBasic statement symbol is used for this purpose. For example, we can assign variable name count to location B0 with the instruction:

Symbol count     B0

Symbols must be declared at the top of a program. Symbols can also be used to assign constants to names. For example, the following statement assigns the decimal value 20 to the name total. Note that this statement does not occupy any location in the microcontroller RAM memory. The number is simply represented with a name.

Symbol total     20

Command names in PicBasic are case insensitive and can be written in upper case, lower case, or with a mixture of the two. Thus, all the variables below are the same:

TOTAL Total toTal
 
Comments

Comments are useful in programs to describe the operation performed in a line or in a block of lines. A comment starts with either the keyword REM or the single quote character (‘). All the characters following a comment character are ignored. Examples of comments are:

REM           This is a simple test program
LOW 0       ‘ Clear Pin 0 to 0
HIGH 1      REM Set Pin 1 to 1

Numeric Values

In PicBasic, numeric values can be specified in three ways: decimal, binary, and hexadecimal. Decimal values are the default and require no prefix. Binary values are specified using the prefix “%” followed by the number. Hexadecimal values are specified using the prefix “$” followed by the number. Some examples are:

REM A has the same value in all the following three statements
A     10
A     001010
A     $0A

ASCII Values

Character constants can be converted into their ASCII values by enclosing them in double quotes. Only one character must be specified. For example,

A”              ASCII value of decimal 65 “1”               ASCII value of decimal 49

String Constants

Although PicBasic does not provide string-handling functions, we can define strings of characters by enclosing them in double quotes. For example,

“COMPUTER”

The above string is treated as a string of ASCII characters with values “C”, “O”, “M”, “P”, “U”, “T”, “E”, “R”.

Line Labels

In PicBasic programs, we often want to jump to different parts of a program, or to jump to a sub- routine. A line in PicBasic is referred by a line label. A line label can be a valid identifier (a valid name in PicBasic), followed by a colon character (:). For example,

LOOP:

Multi-statement Lines

It is possible to use more than one statement on a line to make the program more readable. A colon (:)
character should be used to separate more than one statement in a line. The size of the code does
not change when more than one statement is written on the same line. For example, consider the following statements:

B0     3
B1     5
B2     8

The above statements can all be written on the same line as

B0     3    :    B1     5    :    B2     8











Comments

Popular posts from this blog

ANALOGUE to DIGITAL (A/D) Converter in PIC16F87X Series

ANALOGUE to DIGITAL (A/D) Converter What is a Sample & Sample rate? A "sample" is a single measurement of amplitude. Sample rate is simply the number of samples (or measurements of amplitude) taken per second. Sampling Intervals Quantization The samples are assigned a binary number approximating their sampled value. Quantizing divides up the sampled voltage range into 2n-1 quantizing intervals, where “n” is the number of bits per sample (the sampling resolution). For example, an 8-bit system can identify 28 (256) discrete sampled signal values (255 quantizing intervals). The amplitude of such a signal can occupy the entire quantizing range. A/D Reference Voltages Successive Approximation A/D Example 1 Example 2  Example 3 A/D Converter Modules in PIC16F87X series 28-pin devices has 5 modules. (AN0-AN4)         PIC16F873        PIC16F876 4...

Other PicBasic commands 01

W e shall n o w brief l y look at the remaining PicBasic commands in alphabetical order w hich are useful during the pr o g ram d e v elopment. More details about these commands can be obtained from the PicBasic manual. EEP R OM EEP R OM Location, (constant, constant,….., constant) Thi s comman d store s constant s i n consecut i v e b yte s i n on-chi p EEP R O M memo r y . Th e command on l y w ork s wit h th e PI C microcontroller s tha t h a v e EEP R OM , suc h a s th e PIC16F84 , PIC16F877, etc . Locatio n i s optional , an d i f omitte d th e f irs t EEP R O M locatio n i s assumed . Constant s ca n be numeri c constant s o r strin g constants . String s ar e store d a s consecut i v e b yte s o f ASCI I v alues . An e xampl e i s g i v e n bel o w . EEP R OM 3, (5, 2, 8)        ‘ Store 5 in location 3, ‘ 2 in location 4, and 8 in ‘ location 5 END END St...

Voltmeter

This code to read an analog temperature sensor, convert the reading to a digital value using the ADC, and display the temperature on a 4-digit 7-segment display. The display is updated periodically based on a timer interrupt. 1.Initialization: Setting up ports, configuring ADC, and Timer0. 2.Infinite Loop: Continuously reads analog input and updates the display. 3.ADC Conversion: Reads analog voltage from a sensor. 4.Voltage Calculation: Converts ADC value to voltage. 5.Digit Extraction: Extracts individual digits from the temperature/voltage value. 6.Display Function: Displays the digits on a 4-digit 7-segment display with slight delays. --------------------------------------------- #include <pic.h> // Include the header file for PIC microcontroller family. unsigned char PORTB_value[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; // Array containing values for 7-segment display. unsigned int a2d_value,temp; // Variables for analog to digital conversion and temporary sto...