PIC Basic Course
PIC Basic Course
Table of Contents
Class #1: Introduction to mikroBASIC ....... 1
Class #2: Introduction to Programming .. 13
Class #3: Arithmetic and Logic Operators .. 24
Class #4: Control Statements .. 28
Class #5: Arrays and ADC Library . 35
Class #6: Pulse Width Modulation (PWM) Library 40
Class #7: Math Library 42
Class #8: Functions, Procedures and Modules 44
Class #9: Timers and Interrupt procedure ... 49
Class #10: Serial Communication (USART) .. 53
PIC Microcontroller
Class #1
PIC Microcontroller
Class #1
Why BASIC?
Originally devised as an easy-to-use tool, BASIC became widespread on home
microcomputers in the 1980s, and remains popular to this day in a handful of heavily
evolved dialects. BASICs name, coined in classic, computer science tradition to produce
a nice acronym, stands for Beginners All-purpose Symbolic Instruction Code.
BASIC is still considered by many PC users to be the easiest programming
language to use. Nowadays, this reputation is being shifted to the world of
microcontrollers. BASIC allows faster and much easier development of applications for
PIC compared to the Microchips assembly language MPASM. When writing the code
for MCUs, programmers frequently deal with the same issues, such as serial
communication, printing on LCD display, generating PWM signals, etc. For the purpose
of facilitating programming, BASIC provides a number of built-in and library routines
intended for solving these problems.
As far as the execution and program size are in question, MPASM has a small
advantage in respect with BASIC. This is why there is an option of combining BASIC
and assembly code assembly is commonly used for parts of program in which
execution time is critical or same commands are executed great number of times. Modern
microcontrollers, such as PIC, execute instructions in a single cycle. If microcontroller
clock is 4MHz, then one assembly instruction requires 250ns x 4 = 1us. As each BASIC
command is technically a sequence of assembly instructions, the exact time necessary for
its execution can be calculated by simply summing up the execution times of constituent
assembly instructions.
PIC Microcontroller
Class #1
MCL file (mikro compile library) is created for each module you have included in
the project. In the process of compiling, .mcl files will be linked together to output asm,
lst and hex files. If you want to distribute your module without disclosing the source
code, you can send your compiled library (file extension .mcl). User will be able to use
your library as if he had the source code. Although the compiler is able to determine
which routines are implemented in the library, it is a common practice to provide routine
prototypes in a separate text file.
PIC Microcontroller
Class #1
Oscillator can be 4MHz crystal and either two 22pF capacitors or the ceramic
resonator of the same frequency (ceramic resonator already contains the mentioned
capacitors, but unlike oscillator has three termination instead of only two). The rate at
which the microcontroller operates, i.e. the speed, at which the program runs, depends
heavily on the oscillator frequency. During the application development, the easiest thing
to do is to use the internal reset circuit MCLR pin is connected to +5V through a 10K
resistor
PIC Microcontroller
Class #1
As with any modern environment, you may customize layout of mikroBasic to best
suit your needs toolbars are fully dockable and your arrangement of windows is saved
upon exit.
By default, the largest part of the screen is taken by Code Editor which contains the
program code. This is the advanced text editor where you will be writing your programs.
It features Syntax Highlighting to help you differentiate code elements. Try typing one of
BASICs keywords and watch it go bold, or type an apostrophe to see the comments go
italic and change color.
Code Editor features many other advanced features such as Code & Parameter
Assistants, Auto Correct for common typos, Code Templates, etc.
PIC Microcontroller
Class #1
If you had no previous experience with advanced IDEs, you may wonder what
Code and Parameter Assistants do. These are utilities which facilitate the code writing.
For example, if you type first few letter of a word in Code Editor and then press
CTRL+SPACE, all valid identifiers matching the letters you typed will be prompted to
you in a floating panel. Now you can keep typing to narrow the choice, or you can select
one from the list using keyboard arrows and Enter.
You dont have to memorize the exact order or type of the routine parameters. Type
CTRL+SHIFT+SPACE to activate the Parameter Assistant which will guide you in
filling the appropriate parameters. This feature is automatically invoked upon opening a
parenthesis.
To the left of the Code Editor, you will see a treeview of declared program
elements. This is the Code Explorer which allows you to monitor variables, constants,
routines, etc. Double click the element to jump to its declaration in code. You can click
tabs over Code Explorer to change view to Keyboard shortcut browser for a list of all
IDE shortcuts or to Quick Help browser with complete list of available built-in and
library routines.
For setting other IDE features, click Tools > Options. In the Options window you
can customize the look and feel of mikroBasic, enter your own list of recognized typos,
write your own templates, etc.
PIC Microcontroller
Class #1
Step 1
From a drop-down menu, select: Project > New Project, or click New Project icon
Step 2
Fill the New Project Wizard dialog with correct values to set up your new project.
PIC Microcontroller
Class #1
After you have set up your project, click OK. mikroBasic will create project for you and
automatically open the program file in Code Editor. Now we can write the source code.
Step 3
Upon creating a new project, Code Editor will display an empty program file, named
same as your project. This is shown in the following figure.
PIC Microcontroller
Class #1
Now we are about to write the code for this simple example. We want to make LED
diode blink once per second. Assuming the configuration given in the following figure,
LED diodes are connected to PIC16F877 PORTB pins (it can be any other PIC that has
PORTB).
In this configuration, LED will emit light when voltage on pin is high (5V), and will be
off when voltage on pin is low (0V). We have to designate PORTD as output, and change
its value every second. Listing of the program is given below. Once you are comfortable
with each line, feel free to experiment with it and improve the code.
9
PIC Microcontroller
Class #1
Step 4
Before compiling, it is recommended to save your project (menu choice File > Save All).
Now you can compile your code by clicking CTRL+F9, or by selecting menu Run >
Compile, or by clicking the Compile icon
mikroBasic will generate list and assembly file, and a hex file which can be used to
program PIC MCU. But before trying out our program on PIC, let's test it with the
Debugger.
Step 5
After successful compiling, we can use mikroBasic Debugger to check our program
behavior before we feed it to the device (PIC16F877 or other). For a simple program such
as this, simulation is not really necessary, but it is a requirement for more complex
projects.
To start the Debugger, select Run > Debug, or click the Debug icon
F9.
, or simply hit
10
PIC Microcontroller
Class #1
Upon starting the Debugger, Watch Window appears, and the active line in Code Editor
marks the instruction to be executed next. We will set the breakpoint at line 9 by
positioning the cursor to that line and toggling the breakpoint (Run > Toggle Breakpoint
or F5). See the image below:
We will use the Step Over option (Run > Step Over or F8) to execute the current
program line. Now, you can see the changes in variables, SFR registers, etc, in the Watch
Window items that have changed are marked red, as shown in the image below.
11
PIC Microcontroller
Class #1
We could have used Run/Pause (F6) option to execute all the instructions between the
active line and the breakpoint (Run > Run/Pause Debugger).
12
PIC Microcontroller
Class #2
Introduction to Programming
Learning how to program is not complicated, but it requires skill and experience to
write code that is efficient, legible, and easy to handle. First of all, program is supposed
to be comprehensible, so that the programmer himself, or somebody else working on the
application, could make necessary corrections and improvements. A code sample written
in a clear and manifest way is provided to give you an idea how programs could be
written:
Through clever use of comments, symbols, labels and other elements supported by
BASIC, program can be rendered considerably clearer and more understandable, offering
programmer a great deal of help.
Also, it is advisable to divide larger programs into separate logical entities (such as
routines and modules) which can be addressed when needed. This also increases
reusability of code. Names of routines and labels indicating a program segment should
make some obvious sense. For example, program segment that swaps values of 2
variables could be named "Swap", etc.
13
PIC Microcontroller
Class #2
Identifiers
Identifiers are names used for referencing the stored values, such as variables and
constants. Every program, module, procedure, and function must be identified (hence the
term) by an identifier.
Valid identifier:
1. Must begin with a letter of English alphabet or possibly the underscore (_)
2. Consists of alphanumeric characters and the underscore (_)
3. May not contain special characters:
~ ! @ # $ % ^ & * ( ) + ` - = { } [ ] : " ; ' < > ? , . / | \
4. Can be written in mixed case as BASIC is case insensitive; e.g. First, FIRST,
and fIrST are an equivalent identifier.
Elements ignored by the compiler include spaces, new lines, and tabs. All these
elements are collectively known as the white space. White space serves only to make
the code more legible it does not affect the actual compiling.
Several identifiers are reserved in BASIC, meaning that you cannot use them as
your own identifiers (e.g. words function, byte, if, etc). For more information, please
refer to the list of reserved words. Also, BASIC has a number of predefined identifiers
which are listed in Chapter 4: Instructions.
Operators
BASIC language possesses set of operators which is used to assign values, compare
values, and perform other operations. The objects manipulated for that purpose are called
operands (which themselves can be variables, constants, or other elements).
Operators in BASIC must have at least two operands, with an exception of two
unary operators. They serve to create expressions and instructions that in effect compose
the program.
There are four types of operators in BASIC:
1.
2.
3.
4.
Arithmetic Operators
Boolean Operators
Logical (Bitwise) Operators
Relation Operators (Comparison Operators)
14
PIC Microcontroller
Class #2
Expressions
Expression is a construction that returns a value. BASIC syntax restricts you to
single line expressions, where carriage return character marks the end of the expression.
The simplest expressions are variables and constants, while more complex can be
constructed from simpler ones using operators, function calls, indexes, and typecasts.
Here is one simple expression:
A = B + C
You need to pay attention that the sum must be within the range of variable A in
order to avoid the overflow and therefore the evident computational error. If the result of
the expression amounts to 428, and the variable A is of byte type (having range between
0 and 255), the result accordingly obtained will be 172, which is obviously wrong.
Instructions
Each instruction determines an action to be performed. As a rule, instructions are
being executed in an exact order in which they are written in the program. However, the
order of their execution can be changed by means of jump, routine call, or an interrupt.
if Time = 60 then
goto Minute
end if
15
PIC Microcontroller
Class #2
Data Types
Type determines the allowed range of values for variable, and which operations
may be performed on it. It also determines the amount of memory used for one instance
of that variable.
Simple data types include:
Type
Size
Range of values
byte
8-bit
0 .. 255
char*
8-bit
0 .. 255
word
16-bit
0 .. 65535
short
8-bit
-128 .. 127
integer
16-bit
-32768 .. 32767
longint
32-bit
-2147483648 .. 2147483647
float
32-bit
1.17549435082 * 10-38 ..
6.80564774407 * 1038
0 .. 255
0 .. 65535
-128 .. 127
-32768 .. 32767
-2147483648 .. 2147483647
16
PIC Microcontroller
Class #2
Constants
Constant is data whose value cannot be changed during the runtime. Every constant
is declared under unique name which must be a valid identifier. It is a good practice to
write constant names in uppercase.
If you frequently use the same fixed value throughout the program, you should
declare it a constant (for example, maximum number allowed is 1000). This is a good
practice since the value can be changed simply by modifying the declaration, instead of
going trough the entire program and adjusting each instance manually. As simple as this:
const MAX = 1000
Constants can be declared in decimal, hex, or binary form. Decimal constants are
written without any prefix. Hexadecimal constants begin with a sign $, while binary
begin with %.
const A = 56
const B = $0F
const C = %10001100
' 56 decimal
' 15 hexadecimal
' 140 binary
It is important to understand why constants should be used and how this affects the
MCU. Using a constant in a program consumes no RAM memory. This is very important
due to the limited RAM space (PIC16F877 has 368 locations/bytes).
Variables
Variable is data whose value can be changed during the runtime. Each variable is
declared under unique name which has to be a valid identifier. This name is used for
accessing the memory location occupied by the variable. Variable can be seen as a
container for data and because it is typed, it instructs the compiler how to interpret the
data it holds.
In BASIC, variable needs to be declared before it can be used. Specifying a data
type for each variable is mandatory. Variable is declared like this:
dim identifier as type
where identifier is any valid identifier and type can be any given data type.
For example:
dim temperature as byte
dim voltage as word
Individual bits of byte variables (including SFR registers such as PORTA, etc) can be
accessed by means of dot, both on left and right side of the expression. For example:
Data_Port.3 = 1
17
PIC Microcontroller
Class #2
Symbols
Symbol makes possible to replace a certain expression with a single identifier alias.
Use of symbols can increase readability of code.
BASIC syntax restricts you to single line expressions, allowing shortcuts for
constants, simple statements, function calls, etc. Scope of symbol identifier is a whole
source file in which it is declared.
For example:
symbol MaxAllowed = 234
symbol PORT = PORTC
symbol DELAY1S = Delay_ms(1000)
...
if teA > MaxAllowed then
teA = teA - 100
end if
PORT.1 = 0
DELAY1S
...
'
'
'
'
Directives
Directives are words of special significance for BASIC, but unlike other reserved
words, appear only in contexts where user-defined identifiers cannot occur. You cannot
define an identifier that looks exactly like a directive.
Directive
Meaning
Absolute
Org
Absolute specifies the starting address in RAM for variable (if variable is multi-
18
PIC Microcontroller
Class #2
Org specifies the starting address of routine in ROM. For PIC16 family, routine
must fit in one page otherwise, compiler will report an error. Directive org is appended
to the declaration of routine:
sub procedure test org $200
' Procedure will start at address $200
...
end sub
Comments
Comments are text that is added to the code for purpose of description or
clarification, and are completely ignored by the compiler.
' Any text between an apostrophe and the end of the
' line constitutes a comment. May span one line only.
It is a good practice to comment your code, so that you or anybody else can later
reuse it. On the other hand, it is often useful to comment out a troublesome part of the
code, so it could be repaired or modified later. Comments should give purposeful
information on what the program is doing. Comment such as Set Pin0 simply explains the
syntax but fails to state the purpose of instruction. Something like Turn Relay on might
prove to be much more useful.
Specialized editors feature syntax highlighting it is easy to distinguish comments
from code due to different color, and comments are usually italicized.
Labels
Labels represent the most direct way of controlling the program flow. When you
mark a certain program line with label, you can jump to that line by means of instructions
goto and gosub. It is convenient to think of labels as bookmarks of sort. Note that the
label main must be declared in every BASIC program because it marks the beginning of
the main module.
Label name needs to be a valid identifier. You cannot declare two labels with same
name within the same routine. The scope of label (label visibility) is tied to the routine
where it is declared. This ensures that goto cannot be used for jumping between routines.
Goto is an unconditional jump statement. It jumps to the specified label and the
statement this will exit the subroutine and return to the first program line following the
caller gosub instruction.
19
PIC Microcontroller
Class #2
Note: Although it might seem like a good idea to beginners to program by means of
jumps and labels, you should try not to depend on it. This way of thinking strays from the
procedural programming and can teach you bad programming habits. It is far better to use
procedures and functions where applicable, making the code structure more legible and
easier to maintain.
20
PIC Microcontroller
Class #2
par1 and par2 are passed to the procedure by the value, but variables marked by keyword
byref are passed by the address.
This means that the procedure call
pr1_procedure(tA, tB, tC, tD)
passes tA and tB by the value: creates par1 = tA; and par2 = tB; then manipulates par1
and par2 so that tA and tB remain unchanged;
passes tC and tD by the address: whatever changes are made upon vp1 and vp2 are also
made upon tC and tD.
Function declaration is similar to procedure declaration, except it has a specified
return type and a return value. Function declaration has the form:
sub function functionName(parameterList) as returnType
localDeclarations
statements
end sub
As functions return values, function calls are technically expressions. For example,
if you have defined a function called Calc, which collects two integer arguments and
returns an integer, then the function call Calc(24, 47) is an integer expression. If I and
J are integer variables, then I + Calc(J, 8) is also an integer expression.
21
PIC Microcontroller
Class #2
Modules
Large programs can be divided into modules which allow easier maintenance of
code. Each module is an actual file, which can be compiled separately; compiled modules
are linked to create an application. Note that each source file must end with keyword end
followed by a dot.
Modules allow you to:
1. Break large code into segments that can be edited separately,
2. Create libraries that can be used in different programs,
3. Distribute libraries to other developers without disclosing the source code.
In mikroBasic IDE, all source code including the main program is stored in .pbas
files. Each project consists of a single project file, and one or more module files. To build
a project, compiler needs either a source file or a compiled file for each module.
Every BASIC application has one main module file and any number of additional
module files. All source files have same extension (pbas). Main file is identified by
keyword program at the beginning, while other files have keyword module instead. If
you want to include a module, add the keyword include followed by a quoted name of
the file.
For example:
program test_project
include "math2.pbas"
dim tA as word
dim tB as word
main:
tA = sqrt(tb)
end.
Keyword include instructs the compiler which file to compile. The example above
includes module math2.pbas in the program file. Obviously, routine sqrt used in the
example is declared in module math2.pbas.
If you want to distribute your module without disclosing the source code, you can
send your compiled library (file extension .mcl). User will be able to use your library as if
he had the source code. Although the compiler is able to determine which routines are
implemented in the library, it is a common practice to provide routine prototypes in a
separate text file.
Module files should be organized in the following manner:
module unit_name
include ...
symbol ...
22
PIC Microcontroller
Class #2
const ...
dim ...
end.
Note that there is no body section in the module module files serve to declare
functions, procedures, constants and global variables.
23
PIC Microcontroller
Class #3
Priority
not
first (highest)
second
+, -, or, xor
third
fourth
(lowest)
Arithmetic Operators
Overview of arithmetic operators in BASIC:
Operator
Operation
Operand
types
Result type
addition
byte, short,
byte, short,
integer,
integer,
words, longint words, longint
subtraction
byte, short,
byte, short,
integer,
integer,
words, longint words, longint
div
division
byte, short,
byte, short,
integer, words integer, words
mod
remainder
byte, short,
byte, short,
integer, words integer, words
A div B is the value of A divided by B rounded down to the nearest integer. The
mod operator returns the remainder obtained by dividing its operands. In other words,
X mod Y = X - (X div Y) * Y.
If 0 (zero) is used explicitly as the second operand (i.e. X div 0), compiler will
report an error and will not generate code. But in case of implicit division by zero : X div
Y , where Y is 0 (zero), result will be the maximum value for the appropriate type (for
example, if X and Y are words, the result will be $FFFF).
24
PIC Microcontroller
Class #3
If number is converted from less complex to more complex data type, upper bytes
are filled with zeros. If number is converted from more complex to less complex data
type, data is simply truncated (upper bytes are lost).
If number is converted from less complex to more complex data type, upper bytes
are filled with ones if sign bit equals 1 (number is negative). Upper bytes are filled with
zeros if sign bit equals 0 (number is positive). If number is converted from more complex
to less complex data type, data is simply truncated (upper bytes are lost).
BASIC also has two unary arithmetic operators:
Operator
Operation
Operand
types
Result type
+ (unary)
sign identity
- (unary)
Boolean Operators
Boolean operators are not true operators, because there is no Boolean data type
defined in BASIC. These operators conform to standard Boolean logic. They cannot be
used with any data type, but only to build complex conditional expression.
Operator
Operation
not
negation
and
conjunction
or
disjunction
For example:
if (astr > 10) and (astr < 20) then
PORTB = 0xFF
end if
25
PIC Microcontroller
Class #3
Operation
Operand
types
Result type
not
bitwise
negation
byte, word,
byte, word,
short, integer, short, integer,
long
long
and
bitwise and
byte, word,
byte, word,
short, integer, short, integer,
long
long
or
bitwise or
byte, word,
byte, word,
short, integer, short, integer,
long
long
xor
bitwise xor
byte, word,
byte, word,
short, integer, short, integer,
long
long
<<
byte, word,
byte, word,
short, integer, short, integer,
long
long
>>
byte, word,
byte, word,
short, integer, short, integer,
long
long
<< : shift left the operand for a number of bit places specified in the right operand (must
26
PIC Microcontroller
Class #3
Operation
Operand
types
Result type
equality
All simple
types
True or False
<>
inequality
All simple
types
True or False
<
less-than
All simple
types
True or False
>
greater-than
All simple
types
True or False
<=
less-than-orequal-to
All simple
types
True or False
>=
greater-than-orequal-to
All simple
types
True or False
27
PIC Microcontroller
Class #4
Control Statements
Statements define algorithmic actions within a program. Simple statements - like
assignments and procedure calls - can be combined to form loops, conditional statements,
and other structured statements.
Simple statement does not contain any other statements. Simple statements include
assignments, and calls to procedures and functions.
Structured statements are constructed from other statements. Use a structured
statement when you want to execute other statements sequentially, conditionally, or
repeatedly.
Conditional Statements
Conditional statements are used for change the flow of the program execution upon
meeting a certain condition. The BASIC instruction of branching in BASIC language is
the IF instruction, with several variations that provide the necessary flexibility.
if expression then
statements1
[ else
statements2 ]
end if
Description
Example
The simplest form of the instruction is shown in the figure below. Our
example tests the button connected to RB0 - when the button is pressed,
program jumps to the label "Add" where value of variable "w" is
increased. If the button is not pressed, program jumps back to the label
"Main".
28
PIC Microcontroller
Class #4
dim j as byte
Main:
if PORTB.0 = 0 then
goto Add
end if
goto Main
Add: j = j + 1
end.
29
PIC Microcontroller
Class #4
dim j as byte
Main:
if PORTB.0 = 0 then
j = j + 1
else
j = j - 1
endif
goto Main
end.
Description
Example
select case W
case 0
B = 1
PORTB = B
case 1
30
PIC Microcontroller
Class #4
A = 1
PORTA = A
case else
PORTB = 0
end select
...
select case Ident
case testA
PORTB = 6
Res = T mod 23
case teB + teC
T = 1313
case else
T = 0
end select
goto Label
Description
Example
program test
main:
' some instructions ...
goto myLabel
' some instructions...
myLabel:
' some instructions...
end.
31
PIC Microcontroller
Class #4
Loops
Loop statements allow repeating one or more instructions for a number of times.
The conducting expression determines the number of iterations loop will go through.
Description
For statement requires you to specify the number of iterations you want
the loop to go through.
Counter is variable; initialValue and finalValue are expressions
compatible with counter; statement is any statement that does not change
the value of counter; step_value is value that is added to the counter in
each iteration. Step_value is optional, and defaults to 1 if not stated
otherwise. Be careful when using large values for step_value, as overflow
may occur.
Every statement between for and next will be executed once per
iteration.
Example
Here is a simple example of a FOR loop used for emitting hex code on
PORTB for 7-segment display with common cathode. Nine digits should
be printed with one second delay.
for i = 1 to 9
portb = i
delay_ms(1000)
next i
do
statement_1
...
statement_N
loop until expression
Description
PIC Microcontroller
Class #4
I = 0
do
I = I + 1
' execute these 2 statements
PORTB = I
'
until i equals 10 (ten)
loop until I = 10
while expression
statement_0
statement_1
...
statement_N
wend
Description
while I < 90
I = I + 1
wend
...
while I > 0
I = I div 3
PORTA = I
wend
33
PIC Microcontroller
Class #4
asm
statementList
end asm
Description
Example
asm
movlw 67
movwf TMR0
end asm
34
PIC Microcontroller
Class #5
days_of_the_week
months
AD_Conversion_result
as byte[7]
as byte[12]
as word[10]
First declaration above generates 7 variables of byte type. These can be accessed
by array name followed by number in the square brackets [] (this number is also known
as index). Indexing is zero based, meaning that in our example, index spans numbers
from 0 to 6. Instead of byte, you can define array of any other simple type (word,
short, integer or longint).
Note that:
dim something as integer[10]
When you declare an array, mikroBasic allocates a certain amount of RAM for it.
Elements of array consume consecutive RAM locations; in case of array of bytes, if the
address of m[0] is 0x23, m[1] will be at 0x24, and so on.
Accessing these elements is almost as fast as accessing any variable of simple type.
Instead of byte you can define array of any other simple type (word, short, integer
or longint). Don't forget that you are restricted by the amount of free space in PIC RAM
memory.
For example :
dim size as longint[10]
PIC Microcontroller
Class #5
Constant Arrays
Constant array is initialized by assigning it a comma-delimited sequence of values
within parentheses. For example:
' Declare a constant array which holds number of days in each month:
const MONTHS as byte[12] = (31,28,31,30,31,30,31,31,30,31,30,31)
Note that indexing is zero based; in the previous example, number of days in
January is MONTHS[0], and number of days in December is MONTHS[11].
The number of assigned values must not exceed the specified length. Vice versa is
possible, when the trailing excess elements will be assigned zeros.
36
PIC Microcontroller
Class #5
Adc_Read
Prototype sub function Adc_Read(dim channel as byte) as word
Returns 10-bit unsigned value read from the specified channel.
Description Initializes PICs internal ADC module to work with RC clock. Clock
determines the time period necessary for performing AD conversion (min
12TAD).
Parameter channel represents the channel from which the analog value is to
be acquired. Refer to the appropriate datasheet for channel-to-pin mapping.
Requires Nothing.
Example tmp = Adc_Read(1) ' Read analog value from channel 1
Library Example
This code snippet reads analog value from channel 2 and displays it on PORTB
(two most significant bits) and PORTD.
program Adc_Test
dim temp_res as word
main:
ADCON1
TRISA
TRISB
TRISD
=
=
=
=
$80
$FF
$3F
$0
while TRUE
temp_res = Adc_Read(2)
PORTD = temp_res
PORTB = word(temp_res >> 2)
wend
end.
'
'
'
'
37
PIC Microcontroller
Class #5
38
PIC Microcontroller
Class #5
39
PIC Microcontroller
Class #6
Library.
Requires You need a CCP module on PORTC to use this library.
Example Initialize PWM module at 5KHz:
Pwm_Init(5000)
Pwm_Change_Duty
Prototype sub procedure Pwm_Change_Duty(dim duty_ratio as byte)
Returns Nothing.
Description Changes PWM duty ratio. Parameter duty takes values from 0 to 255, where
0 is 0%, 127 is 50%, and 255 is 100% duty ratio. Other specific values for
duty ratio can be calculated as (Percent*255)/100.
Requires You need a CCP module on PORTC to use this library. To use this function,
module needs to be initialized see Pwm_Init.
Example Set duty ratio to 75%:
Pwm_Change_Duty(192)
Pwm_Start
Prototype sub procedure Pwm_Start
Returns Nothing.
Description Starts PWM.
Requires You need a CCP module on PORTC to use this library. To use this function,
module needs to be initialized see Pwm_Init.
Example Pwm_Start
40
PIC Microcontroller
Class #6
Pwm_Stop
Prototype sub procedure Pwm_Stop
Returns Nothing.
Description Stops PWM.
Requires You need a CCP module on PORTC to use this library. To use this function,
module needs to be initialized see Pwm_Init.
Example Pwm_Stop
41
PIC Microcontroller
Class #7
Math Library
Pow
Prototype sub function Pow(dim x, y as float) as float
Description Function returns the value of x raised to the power of y (i.e. xy). If the x is
negative, function will automatically cast the y into longint.
Sqrt
Prototype sub function Sqrt(dim x as float) as float
Description Function returns the non negative square root of num.
Floor
Prototype sub function Floor(dim x as float) as float
Description Function returns value of parameter num rounded down to the nearest
integer.
Ceil
Prototype sub function Ceil(dim x as float) as float
Description Function returns value of parameter num rounded up to the next whole num
Fabs
Prototype sub function Fabs(dim x as float) as float
Description Function returns the absolute (i.e. positive) value of x.
Exp
Prototype sub function Exp(dim x as float) as float
Description Function returns the value of e the base of natural logarithms raised to
the power of x (i.e. ex).
Sin
Prototype sub function Sin(dim x as float) as float
Description Function returns the sine of x in radians. The return value is from -1 to 1.
42
PIC Microcontroller
Class #7
Cos
Prototype sub function Cos(dim x as float) as float
Description Function returns the cosine of x in radians. The return value is from -1 to 1.
CosE3
Prototype sub function CosE3(dim x as word) as integer
Description Function takes parameter x which represents angle in degrees, and returns its
cosine multiplied by 1000 and rounded up to the nearest integer:
result = round_up(cos(x)*1000)
SinE3
Prototype sub function SinE3(dim x as word) as integer
Description Function takes parameter x which represents angle in degrees, and returns its
sine multiplied by 1000 and rounded up to the nearest integer:
result = round_up(sin(x)*1000)
Asin
Prototype sub function Asin(dim x as float) as float
Description Function returns the arc sine of parameter x; that is, the value whose sine is
x. Input parameter x must be between -1 and 1 (inclusive). The return value
is in radians, between -/2 and /2 (inclusive).
Acos
Prototype sub function Acos(dim x as float) as float
Description Function returns the arc cosine of parameter x; that is, the value whose
cosine is x. Input parameter x must be between -1 and 1 (inclusive). The
return value is in radians, between 0 and (inclusive).
43
PIC Microcontroller
Class #8
Functions
Function is declared like this:
sub function function_name(parameter_list) as return_type
[ local declarations ]
function body
end sub
The function_name represents a functions name and can be any valid identifier.
The return_type is the type of return value and can be any simple type. Within
parentheses, parameter_list is a formal parameter list similar to variable declaration. In
mikroBasic, parameters are always passed to function by value; to pass argument by the
address, add the keyword byref ahead of identifier.
Local declarations are optional declarations of variables and/or constants, local
for the given function. Function body is a sequence of statements to be executed upon
calling the function.
Calling a function
A function is called by its name, with actual arguments placed in the same sequence
as their matching formal parameters. The compiler is able to coerce mismatching
arguments to the proper type according to implicit conversion rules. Upon function call,
all formal parameters are created as local objects initialized by values of actual
arguments. Upon return from a function, temporary object is created in the place of the
call, and it is initialized by the expression of return statement. This means that function
call as an operand in complex expression is treated as the function result.
Use the variable result (automatically created local) to assign the return value of a
function.
Function calls are considered to be primary expressions, and can be used in
situations where expression is expected. Function call can also be a self-contained
statement, in which case the return value is discarded.
44
PIC Microcontroller
Class #8
Example
Heres a simple function which calculates xn based on input parameters x and n (n > 0):
sub function power(dim x, n as byte) as longint
dim i as byte
i = 0
result = 1
if n > 0 then
for i = 1 to n
result = result*x
next i
end if
end sub
Procedures
Procedure is declared like this:
sub procedure procedure_name(parameter_list)
[ local declarations ]
procedure body
end sub
Calling a procedure
A procedure is called by its name, with actual arguments placed in the same
sequence as their matching formal parameters. The compiler is able to coerce
mismatching arguments to the proper type according to implicit conversion rules. Upon
procedure call, all formal parameters are created as local objects initialized by values of
actual arguments.
Procedure call is a self-contained statement.
45
PIC Microcontroller
Class #8
Example
Heres an example procedure which transforms its input time parameters, preparing
them for output on LCD:
sub procedure time_prep(dim byref sec, min,
sec = ((sec and $F0) >> 4)*10 + (sec and
min = ((min and $F0) >> 4)*10 + (min and
hr
= ((hr and $F0) >> 4)*10 + (hr and
end sub
hr as byte)
$0F)
$0F)
$0F)
Modules
In mikroBasic, each project consists of a single project file, and one or more
module files. Project file, with extension .pbp contains information about the project,
while modules, with extension .pbas, contain the actual source code.
Modules allow you to:
Break large programs into encapsulated modules that can be edited separately.
Create libraries that can be used in different projects.
Distribute libraries to other developers without disclosing the source code.
Each module is stored in its own file and compiled separately; compiled modules
are linked to create an application. To build a project, the compiler needs either a source
file or a compiled module file for each module.
Include Clause
MikroBasic includes modules by means of include clause. It consists of the
reserved word include, followed by a quoted module name. Extension of the file should
not be included.
You can include one file per include clause. There can be any number of include
clauses in each source file, but they all must be stated immediately after the program (or
module) name.
Heres an example:
program MyProgram
include "utils"
include "strings"
include "MyUnit"
...
46
PIC Microcontroller
Class #8
Given a module name, compiler will check for the presence of .mcl and .pbas
files, in order specified by the search paths.
If both .pbas and .mcl files are found, compiler will check their dates and
include the newer one in the project. If the .pbas file is newer than the .mcl, new
library will be written over the old one;
If only .pbas file is found, compiler will create the .mcl file and include it in the
project;
If only .mcl file is present, i.e. no source code is available, compiler will include
it as found;
If none found, compiler will issue a File not found warning.
Main Module
Every project in mikroBasic requires single main module file. Main module is
identified by the keyword program at the beginning; it instructs the compiler where to
start.
After you have successfully created an empty project with Project Wizard, Code
Editor will display a new main module. It contains the bare-bones of a program:
program MyProject
' main procedure
main:
' Place program code here
end.
Other than comments, nothing should precede the keyword program. After the
program name, you can optionally place the include clauses.
Place all global declarations (constants, variables, labels, routines) before the label main.
Note: In mikroBasic, the end. statement (the closing statement of every program) acts as
an endless loop.
Other Modules
Modules other than main start with the keyword module. Newly created blank
module contains the bare-bones:
module MyModule
implements
end.
47
PIC Microcontroller
Class #8
Other than comments, nothing should precede the keyword module. After the
module name, you can optionally place the include clause.
Interface Section
Part of the module above the keyword implements is referred to as interface
section. Here, you can place global declarations (constants, variables, and labels) for the
project.
You do not define routines in the interface section. Instead, state the prototypes of
routines (from implementation section) that you want to be visible outside the module.
Prototypes must match the declarations exactly.
Implementation Section
Implementation section hides all the irrelevant innards from other modules,
allowing encapsulation of code.
Everything declared below the keyword implements is private, i.e. has its scope
limited to the file. When you declare an identifier in the implementation section of a
module, you cannot use it outside the module, but you can use it in any block or routine
defined within the module.
By placing the prototype in the interface section of the module (above the
implements) you can make the routine public, i.e. visible outside of module. Prototypes
must match the declarations exactly.
48
PIC Microcontroller
Class #9
'
'
'
'
' If cnt is 200, then toggle PORTB LEDs and reset cnt
do
if cnt = 200 then
PORTB = not(PORTB)
cnt = 0
end if
loop until 0 = 1
end.
Prescaler is set to 32, so that internal clock is divided by 32 and TMR0 increments
every 31 microseconds. If TMR0 is initialized at 96, overflow occurs in (256-96)*31 us =
5 ms. We increase cnt every time interrupt takes place, effectively measuring time
according to the value of this variable. When cnt reaches 200, time will total 200*5 ms =
1 second.
49
PIC Microcontroller
Class #9
TMR1 Timer
TMR1 timer is a 16-bit special function register with working range of 65536.
Assuming that 4MHz oscillator is used, TMR1 can measure 0-65535 microseconds range
(at 4MHz, TMR1 increments by one microsecond). This period can be increased if
prescaler is used. Prescaler divides clock in a certain ratio (prescaler settings are made in
T1CON register).
Before the main program, TMR1 should be enabled by setting the zero bit in
T1CON register. First bit of the register defines the internal clock for TMR1 we set it to
zero. Other important registers for working with TMR1 are PIR1 and PIE1. The first
contains overflow flag (zero bit) and the other is used to enable TMR1 interrupt (zero
bit). With TMR1 interrupt enabled and its flag cleared, we only need to enable global
interrupts and peripheral interrupts in the INTCON register (bits 7 and 6, respectively).
Our following program example shows how to generate 10 seconds using TMR1
timer. For visual purposes, program toggles LEDs on PORTB every 10 seconds.
program Timer1_10sec
dim cnt as byte
sub procedure interrupt
cnt = cnt + 1
pir1.0 = 0
' Clear TMR1IF
end sub
main:
TRISB = 0
T1CON = 1
PIR1.TMR1IF = 0
PIE1 =
1
PORTB = $F0
cnt =
0
INTCON = $C0
' If cnt is 152, then toggle PORTB LEDs and reset cnt
do
if cnt = 152 then
PORTB = not(PORTB)
cnt = 0
end if
loop until 0 = 1
end.
Prescaler is set to 00 so there is no dividing the internal clock and overflow occurs
every 65.536 ms. We increase cnt every time interrupt takes place, effectively measuring
time according to the value of this variable. When cnt reaches 152, time will total
152*65.536 ms = 9.96 seconds.
50
PIC Microcontroller
Class #9
Interrupt Mechanism
Interrupts are mechanisms which enable instant response to events such as counter
overflow, pin change, data received, etc. In normal mode, microcontroller executes the
main program as long as there are no occurrences that would cause an interrupt. Upon
interrupt, microcontroller stops the execution of main program and commences the
special part of the program which will analyze and handle the interrupt. This part of
program is known as the interrupt (service) routine.
In BASIC, interrupt service routine is defined by procedure with reserved name
interrupt. Whatever code is stored in that procedure, it will be executed upon interrupt.
51
PIC Microcontroller
Class #9
end if
end if
end if
end if
end sub
main:
TRISB = %00111111
OPTION_REG = %10000000
INTCON = %10010000
PORTB = 0
eloop:
in endless loop:
LED_run = 1
LED_int = 0
goto eloop
'
'
'
'
'
'
LED_run is on
LED_int is off
end.
Now, what happens when we push the button? Our interrupt routine first analyzes
the interrupt by checking flag bits with couple of if..then instructions, because there
are several possible interrupt causes. In our case, an external interrupt took place (pin
RB0/INT state changes) and therefore bit INTF in INTCON register is set.
Microcontroller will change LED states, and provide a half second delay for us to
actually see the change. Then it will clear INTF bit in order to enable interrupts again,
and return to executing the main program.
In situations where microcontroller must respond to events unrelated to the main
program, it is very useful to have an interrupt service routine. Perhaps, one of the best
examples is multiplexing the seven-segment display if multiplexing code is tied to
timer interrupt, main program will be much less burdened because display refreshes in
the background.
52
PIC Microcontroller
Class #10
Description
Example
USART_Init(2400)
Description
Example
USART_Data_Ready
Description
Example
USART_Read
53
PIC Microcontroller
Class #10
Description
Example
USART_Write(dat)
Description
Example
Soft_UART_Init(PORTB, 1, 2, 9600)
Description
Example
Received_byte = Soft_UART_Read(Rec_ok)
54
PIC Microcontroller
Class #10
Description
Example
Soft_UART_Write(Received_byte)
55