You are on page 1of 6

PIC microcontrollers - programming in Basic

14
1
W
o
r
l
d

o
f

M
i
c
r
o
c
o
n
t
r
o
l
l
e
r
s

s
I
n
t
r
o
d
u
c
t
i
o
n
A/D CONVERTER
External signals are usually fundamentally different from those the microcontroller
recognizes (0V and 5V only) and therefore have to be converted into recognizable values.
An analog to digital converter is an electronic circuit which converts continuous signals
to discrete digital numbers. In other words, this circuit converts an analogue value into a
binary number and forwards it to the CPU for further processing. This module is thus used
for input pin voltage (analogue value) measurement.
The result of measurement is a number (digital value) used and processed later in the
program.
PIC microcontrollers - programming in Basic
40
1
W
o
r
l
d

o
f

M
i
c
r
o
c
o
n
t
r
o
l
l
e
r
s

s
M
u
s
t

K
n
o
w

D
e
t
a
i
l
s
On the other hand, programs are always executed at high speeds and in most cases it is not necessary
to know in detail what is going on within the microcontroller. Despite all good attributes of the
assembly language, programmers have always needed a programming language similar to the
language they use in everyday speech. Finally, high-level programming languages, including Basic,
have been created. The main advantage of these languages is a simplicity of program writing. Several
assembly instructions are now replaced by one statement in Basic. The programmer is not required to
be familiar with the instruction set of the microcontroller in use any more. It is no longer possible to
know how each statement is executed, but it doesnt matter anyway. In case it does, the problem is
solved by adding a sequence written in assembly language to the program.
Similar to assembly language, a specialized program installed on the PC is in charge of compiling
program into machine code. Unlike assembler, compilers for high-level programming languages
create an executable code which is not always the shortest possible.
Figure above gives a rough illustration of what is going on during the process of compiling
a program written in Basic into a hex code.
1. - Use the compiler installed on your PC to write a program in one of the high-level
programming languages and select the appropriate option to compile it into a hex code.
The process of writing program in Basic. By selecting the appropriate option, the
program will be compiled into an assembly then a hex code to be loaded into the
microcontroller.
2. - Load the hex code into the programmer (also installed on your PC) and
select the appropriate option to load the program into the microcontroller.
3. - Build the programmed microcontroller into the target
device. From now on, it will be run by this program.
2
P
r
o
g
r
a
m
m
i
n
g

M
i
c
r
o
c
o
n
t
r
o
l
l
e
r
s

s
P
r
o
g
r
a
m
m
i
n
g

L
a
n
g
u
a
g
e
s
PIC MICROCONTROLLERS - PROGRAMMING IN BASIC
53
1
2
3
ADVANTAGES OF HIGH-LEVEL PROGRAMMING LANGUAGES
If you have any experience in writing programs for PIC microcontrollers in assembly
language, then you are probably familiar with the other side of the medal of RISC
architecture - the lack of instructions. For example, there is no appropriate instruction for
multiplying two numbers. Of course, there is a way to solve this issue owing to
mathematics which enables you to perform complex operations by breaking them into a
number of simple ones. Accordingly, multiplication can be easily substituted by
successive addition (a x b = a + a + a + ... + a). And here we are, just at the beginning of
a very long story... Still there is no reason to be worried about as far as you use one of the
high-level programming languages, such as Basic, as the compiler will automatically find
a solution to these and similar issues. Simply write a*b.
2
P
r
o
g
r
a
m
m
i
n
g

M
i
c
r
o
c
o
n
t
r
o
l
l
e
r
s

s
P
r
o
g
r
a
m
m
i
n
g

L
a
n
g
u
a
g
e
s
PIC MICROCONTROLLERS - PROGRAMMING IN BASIC
55
Program written in Basic
The same program compiled into assembly code. As can be seen,
each Basic command is broken into several assembly instructions
during the process of compiling.
BSF STATUS, 5
BCF STATUS, 6
CLRF TRISA
L__main2:
BCF STATUS, 5
CLRF PORTA
MOVLW 11
MOVWF R11
MOVLW 38
MOVWF R12
MOVLW 93
MOVWF R13
L__main6:
DECFSZ R13, 1
GOTO L__main6
DECFSZ R12, 1
GOTO L__main6
DECFSZ R11, 1
GOTO L__main6
NOP
NOP
MOVLW 255
MOVWF PORTA
MOVLW 11
MOVWF R11
MOVLW 38
MOVWF R12
MOVLW 93
MOVWF R13
L__main7:
DECFSZ R13, 1
GOTO L__main7
DECFSZ R12, 1
GOTO L__main7
DECFSZ R11, 1
GOTO L__main7
NOP
NOP
GOTO L__main2
wend ' Endless loop
GOTO $+0
main:
TRISA = 0x00 ' Configure pins as outputs
While TRUE
PORTA = 0x00 ' Turn PORTA LEDs OFF
Delay_ms(1000) ' 1 second delay
PORTA = 0xFF ' Turn PORTA LEDs ON
Delay_ms(1000) ' 1 second delay
wend ' Endless loop
end.
2
P
r
o
g
r
a
m
m
i
n
g

M
i
c
r
o
c
o
n
t
r
o
l
l
e
r
s

s
T
h
e

B
a
s
i
c
s

o
f

t
h
e

B
a
s
i
c

PIC MICROCONTROLLERS - PROGRAMMING IN BASIC
57
Figure below illustrates the structure of a simple program written in Basic, pointing out
the parts it consists of. This is an example of how you should write a program. Differences
are more than obvious
' **********************************************************
' program LED_Blinking
' MCU: PIC16F887
' Dev.Board: EasyPIC6
' Oscillator: HS, 08.0000 MHz
' **********************************************************
program LED_blink 'Define frequency oscillator
symbol outputs = PORTB 'Port PORTB is assigned the name 'outputs'
const TURN_OFF as byte =$00 'Define constant 'TURN_OFF'
const TURN_ON as byte =$FF 'Define constant 'TURN_ON'
dim k as byte 'Define auxiliary variable 'k'
TRISB=$00 'All port PORTB pins are configured as outputs
k=0 'Initial value of variable k
Main: 'Start of program
for k=1 to 10 'Increment variable k 10 times
gosub Blink 'Jump to subroutine 'Blink'
next k 'Increment variable k
goto Main 'Jump to start of the program
Blink: 'Start of subroutine
outputs=TURN_ON 'Turn on outputs (PORTB=$FF)
delay_ms(1000) '1 second delay
outputs=TURN_OFF 'Turn off outputs (PORTB=$00)
delay_ms (1000) '1 second delay
return 'Return from subroutine
end. 'End of program
H
e
a
d
e
r
S
u
b
r
o
u
t
i
n
e
Symbol outputs
Program name
ConstantTURN_OFF
Variable k
Statement
Label Main
Jump to
subroutine Blink
Text following the apostrophe sign (') represents a
comment and is not compiled into an executable code.
Comments
R
INTERRUPT SYSTEM
The first thing to be done by the microcontroller, when an interrupt request arrives, is to
execute the current instruction, then to stop the regular program execution. The current
program memory address is automatically pushed onto the stack and the default address
(predefined by the manufacturer) is written to the program counter. The location from
where the program proceeds with execution is called an interrupt vector. For the
PIC16F887 microcontroller, the address is 0004h. As seen in figure below, the interrupt
vector should be skipped during regular program execution.
A part of the program to be executed when an interrupt request arrives is called an interrupt
routine (it is a subroutine in fact). The first instruction of the interrupt routine is located at
the interrupt vector. How long will it take to execute the subroutine and what it will be like,
depends on the skills of the programmer as well as on the interrupt source itself. Some
microcontrollers have a couple of interrupt vectors (every interrupt request has its vector),
whereas this microcontroller has only one. This is why the first part of every interrupt
routine should be interrupt source detection. When the interrupt source is known and
interrupt routine is executed, the microcontroller reaches the RETFIE instruction, pops the
address from the stack and proceeds with program execution from where it left off.
MikroBasic recognizes an interrupt routine to be executed by means of the interrupt
keyword. The interrupt routine should be written by the user.
3
P
I
C
1
6
F
8
8
7

M
i
c
r
o
c
o
n
t
r
o
l
l
e
r

s
T
h
e

P
I
C
1
6
F
8
8
7

K
e
y

F
e
a
t
u
r
e
s
PIC MICROCONTROLLERS - PROGRAMMING IN BASIC
123
Generation of interrupt
Interrupt execution
Return to the main program
sub procedure interrupt ' Interrupt routine
cnt = cnt + 1 ; ' Interrupt causes variable cnt to be incremented by 1
end sub
Example

You might also like