Microprocessor Systems Handouts 2013
Microprocessor Systems Handouts 2013
Microprocessor Systems Handouts 2013
DEPARTMENT OF ENGINEERING
ELECTRICAL POWER, BIOMEDICAL,
COMPUTER AND TELECOMMUNICATION
ENGINEERING
1 | Page
Chapter One
Introduction to Microprocessors, Microprocessor-Based
Systems, Microcomputers, and Microcontrollers
1.1 THE MICROPROCESSOR: [1]
The microprocessor is a semiconductor digital integrated circuit. It has
the same structure of a Central Processing Unit (CPU) for a computer. It
consists of a number of digital computing circuits (which constitute the
Arithmetic Logic Unit ALU), a number of control and timing circuits
(which constitute the Control Unit) as well as a number of storage
elements (called the registers).
This structure enables the microprocessor to perform the same
computing and control functions of a CPU. The microprocessor can
execute programs and can control most of the parts of a computer. Any
program consists of a number of microprocessor instructions which are
stored as binary codes (called machine language). These instructions
enable the microprocessor to do different arithmetic, logical, dataprocessing, compute and control operations. Also, the microprocessor
can interact with and control external devices that are connected with
it.
Usually the microprocessor is connected to a main external memory,
external I/O interfaces (ports) and devices, a clock generator circuit and
a dc supply to form a microprocessor-based system.
2 | Page
Intel. Like Pentium 4, Core i3, Core i5, Core i7, Xeon, Itanium 2.
AMD. Like Phenom, Opteron.
Motorola. Like MC6800, MC68020.
IBM. Like PowerPC, POWER 5.
Hitachi. Like SuperH.
ARM. Like SC100, Cortex-A.
4 | Page
1.4.1
1.4.2
1.4.3
It supplies the microprocessor with the clock which synchronizes all its
internal and external operations. Without a clock, the microprocessor
will remain at halt. Also, the clock determines the speed of the
microprocessor operations.
6 | Page
1.4.4
1.4.5
1.4.6
The microprocessor is connected with memory and I/O ports through the
system bus. A bus consists of a number of lines of the same function
over which data or information is transferred from a source to a
destination.
7 | Page
The system bus consists of 3 buses: the address, data & control buses.
Through these buses the microprocessor sends and receives data,
instructions and control signals.
1.4.6.1
1.4.6.2
1.4.6.3
Input control lines carry request signals from external parts to the
microprocessor to do some operations them.
Ex: INTR, RESET, BUS REQEST.
are the
external
its input
with the
9 | Page
WAIT:
This request causes the microprocessor to delay its operations not
stopping them. This request used by slow I/O devices and memory in
order to synchronize their operations with the microprocessor.
11 | P a g e
Atmel
AT89 series (Intel 8051 architecture)
AT90, ATtiny, ATmega, ATxmega series (AVR architecture)
AT91SAM (ARM architecture)
12 | P a g e
8-bit
o
MCS-51 (8051 family)
16-bit
o
MCS-96 (8096 family)
Microchip Technology
Microchip produces microcontrollers with 3 very different
architectures:
a. 8-bit (8-bit data bus) PIC microcontrollers:
o
o
Chapter Two
Assembly Language Programming of Intel 8086/8088
Microprocessors
2.1 THE PROGRAMMING MODEL OF INTEL 8086/8088
MICROPROCESSORS: [1]
13 | P a g e
Accumulator Register
Base Index Register
Count Register
Data Register
SP (16-bit)
BP (16-bit)
SI (16-bit)
DI (16-bit)
Destination Index
Register
IP (16-bit)
Instruction Pointer
Register
FLAGS (16-bit)
Flags Register
CS (16-bit)
DS (16-bit)
SS (16-bit)
Stack Segment
Register
ES (16-bit)
General
Purpose
Registers
Special
Purpose
Registers
2.1.1
2.1.2
CF (Carry Flag). It sets (CF=1) when a carry out of the MSB (Most
Significant Bit) results from addition instruction or borrow from
subtraction. Otherwise it will reset (CF=0).
Ex:
10111101
+ 11101001
Carry (CF=1)
1 10100110
15 | P a g e
Default Use
To store program
instructions.
Segment
address
In CS
register.
Offset Address
In IP register.
16 | P a g e
b. Data
Segment.
(A data
memory)
c. Stack
Segment
(A data
memory)
d. Extra
Segment
(A data
memory)
In DS
register.
In SS
register.
In ES
register.
A direct 16-bit
address or an
address in BX or
SI or DI registers.
In SP or BP
registers.
In DI register.
Next:
OPCODE
MOV
OPERANDS
AX , BX
Destination Operand
COMMENTS
; Copy BX to AX
Source Operand
Both the LABEL and COMMENTS are optional, used in some cases, but each
instruction must have its opcode which specify the type of operation. The
OPERANDS are register names, memory addresses or values of data on
which the instruction will operate. Some 8086/8088 instructions have no
operand, others have one or two operands.
17 | P a g e
INSTRUCTIONS: [1]
The addressing mode is the method used in the instruction to specify the
location of data (the operands) on which the instruction will operate.
The followings are some of the addressing modes used with 8086/8088
instructions.
2.4.1
MOV SI , BX
MUL CX
MOV AH , BL
Source operand and destination operand registers must be of the same
size.
2.4.2
2.4.3
One of the operands is a memory location which contains the data that
will be used by the instruction. The offset address of this memory
location is written directly in the instruction. By default data is stored in
data segment memory. Ex:
MOV AX, [0070]
register AX. The
MOV [002B], CL
memory. The
2.4.4
One of the operands is a memory location which contains the data that
will be used by the instruction. The offset address of this memory
location will be in a register. The registers
that will be used to hold the offset address are BX, SI or DI only. By
default data is stored in data segment memory. Ex:
MOV BX, 0070
; First the offset address of memory must be
stored in a register.
MOV AX, [BX] ;Copy one data word from memory to register AX.
The
;offset address of the memory is taken from
register BX.
MOV DI, 002B
stored in a register.
MOV [DI], CL
The
;offset address of the memory is taken from
register DI.
Note: Register indirect addressing is very useful in case the program
deals with a large quantity of data.
2.4.5
One of the operands is a memory location which contains the data that
will be used by the instruction. The offset address of this memory
location will be in a register. Also, a displacement will be added to or
subtracted from the offset address. The registers
that will be used to hold the offset address are BX, SI or DI only. By
default data is stored in data segment memory. Ex:
MOV BX, 0070
stored in a register.
MOV AX, [BX-20]
AX. Offset
2.4.6
This mode is used with instructions JMP, LOOP and CALL which cause the
jump in the program from one part to another in the Code Segment. The
offset jump address is written directly in the instruction. This address
will replace the current address of IP to cause the jump. Ex:
20 | P a g e
JMP
address 0020H.
2.4.7
0020
This mode is used with JMP instruction which cause the jump in the
program from one part to another in the Code Segment. The offset jump
address will be in any 16-bit register (AX, BX, CX, DX, SP, BP, SI, DI).
When the microprocessor executes JMP instruction it replaces the
current address of IP by the address specified by the register and jump.
Ex:
MOV
CX, 0020
register.
JMP
CX
address which is
2.4.8
This mode is used with instructions IN and OUT which are used to
transfer data between the microprocessor and Input / Output ports. The
port address is written directly in the instruction and it must be 8-bit
only. Ex:
IN AL,50 ; Input one byte from the input port to AL. The port
address is 50H.
OUT 70,AX ; Output one word (two bytes) from AX to the output
port. The
; port address is 70H.
2.4.9
This mode is used with instructions IN and OUT which are used to
transfer data between the microprocessor and Input / Output ports. The
port address will be 16-bt in register DX only. Ex:
MOV DX, 1F00
IN AX, DX
to register AX.
OUT DX,AL
port address
2.5.1
Data transfer instructions are used in the program to move (copy) data
from one location to another. The data may be in groups of 8-bits or 16bits. All these instructions do not affect the FLAGS. We will study some
instructions in this group.
2.5.1.1
2.5.1.2
2.5.1.3
It is used to return a word only from the top of the stack segment
memory to a register or a data segment memory.
POP [SI]
memory to the data
register SI.
POP BX
2.5.1.4
IN Instruction: [1]
2.5.1.5
OUT DX, AL
IN DX, AX
2.5.2
2.5.2.1
Result
Result
Result
destination - source
CH
Result
; destination - source - CF
Result
SP
Register, Register
2.
ADD CX,AX
CMP SI,DI
Register, Data in Memory or Data in Memory, Register
24 | P a g e
3.
2.5.2.2
DX,0090
DS,DX
AX,0
; AX will be used to store the sum.
CL,0
; CL will be used to store the carry.
AX,[0] ; add the first word.
AX,[2] ; add the second word.
CL,0
; take the first carry.
AX,[4] ; add the third word.
25 | P a g e
ADC CL,0
; take the second carry.
ADD AX,[6] ; add the fourth word.
ADC CL,0
; take the third carry.
MOV [10],AX ; store the sum in data memory 0010H.
MOV [12],CL ; store the total carry in data memory 0012H.
INT 7
0090:0
DW F0C0, CAB6, 2D50, FF56
2.5.2.3
Unsigned bytes and words can be multiplied and divided using MUL
and DIV instructions. The results will be unsigned values. Signed
bytes and words can be multiplied and divided using IMUL and IDIV
instructions. The results will be signed values in this case.
a. Byte Multiplication:
The multiplicand must be a byte in AL register only. The multiplier is a
byte either in an 8-bit register or in memory (the multiplier cannot be
an immediate byte). The result will be a word in AX (The high byte in
AH and the low byte in AL).
Byte in
in AX
AL
Byte in
*
AL
AH
BL
BH
CL
CH
Result (Word)
AH
AL
High Byte
Low Byte
DL
DH
memo
ry
MUL BL
; AL * BL
AX
MUL AL
; AL * AL
AX
AX
b. Word Multiplication:
The multiplicand must be a word in AX only. The multiplier is a word
either in a 16-bit register or in memory (the multiplier cannot be an
26 | P a g e
Word in
MUL SI
word)
AX
BX
CX
DX
SI
DI
SP
BP
memo
ry
DX
High
; AX *SI
Result (Double
AX
Low Word
;AX * [BX]
c. Byte Division:
The dividend must be a word in register AX only. The divider is a
byte either in an 8-bit register or in a memory. The result (the
quotient) will be a byte in AL and the remainder will be a byte in AH.
Word in
Result (quotient)
AX
DIV BH
Byte in
AL
AH
BL
BH
CL
CH
DL
DH
memo
ry
; AX BH
Remainder
AH
AL
d. Word Division:
27 | P a g e
AX
Low Word
Word in
AX
BX
CX
DX
SI
DI
SP
BP
memo
ry
Remainder
DX
AX
DIV CX
; DX AX CX
AX (The quotient) DX (The
remainder).
DIV WORD PTR[30] ; DX AX[0030]
AX(The quotient) DX(The
remainder)
Programming Example 2: Assume that the variables A, B, and C are
unsigned byte valued numbers stored in data memory starting with address 0090:10.
The assigned values for these variables are:
A= 7AH , B=3DH, C=4EH
Write a program that calculates the value of the variable X from the following
equation:
X= (B2 -2FC0 + AC)/ (C+ 65)
Assume that no carry will result from the addition operation and no borrow will result
from the subtraction operations. The program stores the result and the remainder in the
data memory starting with address 0090:20.
Solution:
MOV DX,0090
MOV DS,DX
MOV AL, [0011]
MUL AL
; Calculate B2
MOV DX, AX
SUB DX, 2FC0
; Calculate B2-2FC0
MOV AL,[0010]
MUL BYTE PTR [0012]
; Calculate AC
ADD DX, AX
;Calculate B2-2FC0+AC
28 | P a g e
MOV BL,[0012]
ADD BL,65
MOV AX,DX
DIV BL
MOV [0020], AX
INT 7
0090:10 DB 7A
0090:11 DB 3D
0090:12 DB 4E
2.5.3
; Calculate C+65
; Calculate X
; Store result and remaider
;A
;B
;C
2.5.3.1
; DH . CL
b. OR (Logical OR):
OR DX, AX
; DX + AX
Result
DH
Result
DX
CH
2.5.3.2
; BX
; DH
BX
DH
29 | P a g e
[0050]
2.5.4
2.5.4.1
It is without any condition. The jump will occur always when the
microprocessor executes JMP instruction. JMP instruction is used with
two addressing modes:
30 | P a g e
MOV BX,FFFF
DEC BX
0020
JMP 001E
2.5.4.2
JMP
AX
Jump if:
Zero
Not Zero
Carry
No Carry
Overflow
No Overflow
Sign is Set ( sign negative)
Remark
Jump if ZF=1
Jump if ZF=0
Jump if CF=1
Jump if CF=0
Jump if OF=1
Jump if OF=0
Jump if SF=1
31 | P a g e
JNS
JP/JPE
JNP/JPO
JA
JAE
JB
JBE
JE
JNE
Above
Above or Equal
Below
Below or Equal
Equal
Not Equal
Used
when
operations
are
performed
on
unsigned
numbers.
JG
JGE
JL
JLE
JE
Greater
Greater or Equal
Less
Less or Equal
Equal
Used
when
operations
are
performed
on
signed numbers.
Table 2.2The
microprocessors.
conditional
jump
instructions
of
Intel
8086/8088
MOV CX,0090
MOV DS,CX
MOV AX,[0000]
CMP AX,[0002]
JA DISP
MOV AX,[0002]
DISP:
INT 10
INT 7
0090:0 DW 34FD, C694
2.5.4.3
SUM:
MOV AX,0
MOV BX,4
MOV CX,200
ADD AX,BX
32 | P a g e
LOOP SUM
LOOP will decrement CX by 1 each time it repeats the loop. If CX 0,
it will jump again to the specified label. If CX=0, it will not jump and
the program continues from the next instruction.
The above part of the program can be written as shown without using
LOOP:
MOV AX,0
MOV BX,4
MOV CX,200
SUM:
ADD AX,BX
DEC CX
JNZ SUM
Note: All loop instructions require direct address, so we must use
labels or write the value of the address in practical exercises.
Programming Example 5: The data words 45FDH, CD08H, 7FF0H,
6CB8H, AA22H, 9945H, E0FAH, 9034H, 0F10H, ADBCH, 2189H,
CF94H, BD5FH, 8832H and 7A1BH are stored in data memory starting
with address 0090:0. Write a program that calculates the sum of
these words and stores the result in data memory starting with
0090:40.
Solution: The sum of all these words will be a word, but the total
carry will be a byte.
MOV DX,0090
MOV DS,DX
MOV SI,0000
; starting offset address of data.
MOV CX, 0F
; Counter to the number of
additions.
MOV AX,0
; AX will be used to store the
sum.
MOV BL,0
; BL will be used to store the carry.
SUM:
ADD AX,[SI]
; add a word.
ADC BL,0
;take the carry
INC SI
INC SI
; get the address of next word.
LOOP SUM
; add second word.
MOV [0040], AX
MOV [0042], BL
INT 7
0090:0
DW 45FD, CD08, 7FF0, 6CB8, AA22, 9945, E0FA
0090:E
DW 9034, 0F10, ADBC, 2189, CF94, BD5F, 8832, 7A1B
33 | P a g e
0090:0
0090:40
2.5.4.4
INT 7
DW 45FD, CD08, .
DB 90, 34,
FUNCTION:
MOV DX,1500
MOV BX, 2800
CALL FUNCTION
MOV AX,DX
INT 7
ADD DX, BX
RET
2.5.4.5
INT Vector-Number
INT E
Intel P
INTR
INTA
7D-0D
External
Device
Chapter Three
36 | P a g e
Figure 3.1 The General Pins-Out for ROMs and SRAMs Memories.
Notes:
1. Memory storage capacity (the number of storage locations in the
memory) C= 2n where n is the number of address lines.
Ex: a 1 K memory has 10 address lines (A0-A9) because: 1K=1024=210 .
2. Each memory has a number of data lines equal to the size of each
storage location which can be 1, 2, 4, 8 or 16-bit.
Ex: A memory with each storage location is 4 bits has 4 data lines (D 0-D3).
Usually the total memory size is written like: 1Kx4, 1Kx8, 2Kx8, 16Kx1 ..
etc.
Ex: A memory 4Kx8 has:
4K= 4x1024=4096= 212
12 address lines A 0- A11. It has 8 data lines: D 0D7.
3. A SRAM and a ROM has at least one CS (Chip Select) or CE ( Chip Enable
) input. It enables all the memory chip and it is usually connected to one of
the address decoder outputs.
4. A ROM has one control input OE , usually connected with MEMR line of
the system bus.
5. A SRAM has two control inputs OE (connected to MEMR) and WE
(connected to MEMW) lines of the system bus.
37 | P a g e
3.1.1
39 | P a g e
40 | P a g e
41 | P a g e
3.2.1
selection:
1000H for the 8-bit input port.
2000H for the 4-bit output port.
3000H for the 8-bit output port.
3.2.2
is
Intel
8255A
Programmable
D0 D7
RESET
Peripherals
PA0 PA7
A0
RD
PC0 PC3 A1
PC4 PC7 WR
PB0 PB7
Intel 8255A
45 | P a g e
PPI
CS
Table 3.1
A1
0
0
1
1
A0
0
1
0
1
The Register
Port A
Port B
Port C
Control Register
Addressing of Intel
46 | P a g e
1.
2.
47 | P a g e
3.2.3
48 | P a g e
3.2.3.1
3.2.3.2
Figure 3.9 Connection of an array of 8 switches or keys through an 8bit input port.
I/O Devices Connection Example 4: Connection of a matrix of 4x4
keys using a 4-bit input port and a 4-bit output port. In this
connection the microprocessor uses a method called "Matrix Scan"
to detect which keys are pressed. The rows are connected through
the input port and the columns through the output port. In this
examples lines PC0-PC3 of port C of Intel 8255A are programmed as
inputs, while PC4-PC7 line are programmed as output shown in figure
3.10.
51 | P a g e
3.2.3.3
3.2.3.4
3.3.1
Chapter Four
Introduction to PIC Microcontrollers
4.1 THE PIC MICROCONTROLLER FAMILY: [4]
The PIC microcontroller family of microcontrollers is manufactured by Microchip Technology
Inc. Currently they are one of the most popular microcontrollers used in many commercial
and industrial applications. The PIC microcontroller architecture is based on a modified
Harvard RISC (Reduced Instruction Set Computer) instruction set from only 6 pins to 80 pins,
and from 384 bytes to 128 Kbytes of program memory.
PIC microcontrollers are available with many different specifications
depending on:
Memory Type
Flash
OTP (One-time-programmable)
ROM (Read-only-memory)
ROMless
InputOutput (I/O) Pin Count
418 pins
2028 pins
3244 pins
45 and above pins
Memory Size
54 | P a g e
0.51K
24K
816K
2432K
4864K
96128K
Special Features
CAN
USB
LCD
Motor Control
Radio Frequency
The models of PIC microcontrollers are upward compatible with each other
and a program developed for one model can very easily, and in many
cases with no modifications, be run on other models of the family. This is
because the basic assembler instruction set of PIC microcontrollers
consists of only 33 instructions and most of the family members (except
the newly developed devices) use the same instruction set.
All PIC microcontrollers offer the following features:
RISC instruction set.
Digital I/O ports
On-chip timer with 8-bit prescaler
Power-on reset
Watchdog timer
Power saving SLEEP mode
High source and sink current
Direct, indirect, and relative addressing modes
External clock interface
RAM data memory
EPROM or Flash program memory
Some devices offer the following additional features:
Analogue input channels
Analogue comparators
Additional timer circuits
EEPROM data memory
External and internal interrupts
Internal oscillator
Pulse-width modulated (PWM) output
USART serial interface
Some even more complex devices in the family offer the following
additional features:
CAN bus interface
55 | P a g e
56 | P a g e
57 | P a g e
member of the family is chosen. For some large projects even this may not be enough and the
designer may have to sacrifice the I/O ports to interface
an external data memory, or to choose a microcontroller from a different manufacturer.
The program memory is where your PicBasic or MikroBasic Pro or MikroC Pro program
resides. PIC microcontrollers are based on the flash technology where the memory chip
can be erased or re-programmed using a programmer device. Most PIC microcontrollers
can also be programmed without removing them from their circuits. This process (called
in-circuit serial programming or ISP). Although the program memory is mainly used to
store a program, there is no reason why it cannot be used to store constant data used in
programs. PIC microcontrollers can have program memories from 0.5 to over 16 K.
It is interesting to note that PICs are known as 8-bit microcontrollers. This is actually true
as far as the width of the data memory is concerned, which is 8-bits wide.
When power is applied to the microcontroller or when the MCLR input is lowered to logic
0, execution start from the Reset Vector, which is the first word located at address 0 the
program memory.
4.2.2
The data memory is used to store all of the program variables. All data will be lost when
power is removed. The width of the data memory is 8-bits wide. The data memory in a PIC
microcontroller consists of banks where some models may have only 2 banks, some
models 4 banks, and so on.
4.2.3
4.2.3.1
4.2.3.2
These registers are used for the I/O control. Every I/O port in the PIC
microcontroller has two registers: port data register and port
direction control register.
Port data register has the same name as the port it controls. For
example, PIC16F84 microcontroller has two port data registers PORTA
and PORTB. A PIC16F877 microcontroller has 5 port data registers
PORTA, PORTB, PORTC, PORTD, and PORTE. An 8-bit data can be sent
to any port, or an 8-bit data can be read from the ports. It is also
possible to read or write to individual port pins. For example, any bit
of a given port can be set or cleared, or data can be read from one or
more port pins at the same time.
Ports in a PIC microcontroller are bi-directional. Thus, each pin of a port can be used as
an input or an output pin. Port direction control register configures the port pins as
either inputs or outputs. This register is called the TRIS register and every port has a
TRIS register named after its port name. For example, TRISA is the direction control
register for PORTA. Similarly, TRISB is the direction control register for PORTB and
so on.
Setting a bit in the TRIS register makes the corresponding port register pin an input.
Clearing a bit in the TRIS register makes the corresponding port pin an output. For
60 | P a g e
example, to make bits 0 and 1 of PORTB input and the other bits output, we have to
load the TRISB register with the bit pattern:
00000011
Figure 4.3 shows the TRISB register and the direction of PORTB pins.
4.2.3.3
4.2.3.4
4.2.3.5
61 | P a g e
4.2.4
63 | P a g e
4.2.5
4.2.6
4.2.7
A PIC microcontroller port can source and sink 25 mA of current. When sourcing current,
the current is flowing out of the port pin, and when sinking current, the current is flowing
into the pin. When the pin is sourcing current, one pin of the load is connected to the
microcontroller port and the other pin to the ground (see figure 4.7a). The load is then
energized when the port output is at logic 1. When the pin is sinking current, one pin of the
load is connected to the supply voltage and the other pin to the output of the port (see
figure 4.7b). The load is then energized when the port output is at logic 0.
Chapter Five
Programming PIC Microcontrollers using mikroBasic Pro
Language
5.1 MIKROBASIC PRO PROGRAMMING LANGUAGE: [5]
The mikroBasic PRO for PIC is a powerful, feature-rich development tool for
PIC microcontrollers. It is designed to provide the programmer with the
easiest possible solution to developing applications for embedded
systems.
5.1.1
The global variables are visible throughout the whole module, from the place of
declaration. Local variables, parameters, function results are within the block in which
they are declared.
5.1.2
Variables: [5]
5.1.3
Constants: [5]
Constant is a data whose value cannot be changed during the runtime. Constants are
declared in the declaration part of a program or routine. You can declare any number of
constants after the keyword const:
const constant_name [as type] = value
Every constant is declared under unique constant_name which must be a valid
identifier. It is a tradition to write constant names in uppercase. Constant requires you to
specify value, which is a literal appropriate for the given type. type is optional. type
must be used only when declaring a constant array.
Here are a few examples:
const
const
const
const
const
5.1.4
Identifiers: [5]
5.1.5
5.1.5.1
Simple types represent types that cannot be divided into more basic. Simple types in
mikroBasic PRO for PIC are listed in table 5.1.
Type
bit
sbit
byte, char
short
word
integer
longword
longint
float
Size
1bit
1bit
8bit
8bit
16bit
16bit
32bit
32bit
32bit
Range
0 or 1
0 or 1
0 .. 255
-127 .. 128
0 .. 65535
-32768 .. 32767
0 .. 4294967295
-2147483648 .. 2147483647
1.17549435082 * 10-38 .. 6.80564774407 * 1038
5.1.5.2
These are the types that are constructed from the simple data types.
We will study only two of these types which are arrays and strings.
a. Arrays:
An array represents an indexed collection of elements of the same
type. Each element has a unique index. The array can be one
dimensional or multidimensional array. Variable array declaration has
the following syntax:
dim identifier as type [array_length1] [array_length2]
Indexing is zero based. So, each of the elements of an array is
numbered from 0 through array_length - 1. Every element of an
array is of type and can be accessed by specifying array name
followed by elements index within brackets. Here are a few examples
of array declaration:
dim weekdays as byte[7]
dim samples as word[50]
dim m as integer[5][10] ' 2-dimensional array of size 5x10
main:
' Now we can access elements of array variables, for example:
samples[0] = 1
weekdays[5] = 4
m[0][4]=-150
Constant array is initialized by assigning it a comma-delimited
sequence of values within parentheses. For example:
const
MONTHS
as
(31,28,31,30,31,30,31,31,30,31,30,31)
byte[12]
b. Strings:
A string represents a sequence of characters equivalent to an array of char. It is
declared like this:
string[string_length]
The specifier string_length is a number of characters a string consists of. The
string is stored internally as the given sequence of characters plus a final null
character (zero). A null string ("") is stored as a single null character. You can assign
string literals or other strings to string variables. The string on the right side of an
assignment operator has to be shorter than another one, or of equal length. For
example:
dim msg1 as string[20]
dim msg2 as string[19]
69 | P a g e
main:
msg1 = "This is some message"
msg2 = "Yet another message"
msg1 = msg2 ' this is ok, but vice versa would be illegal
Alternately, you can handle strings elementbyelement. For example:
dim s as string[5]
' ...
s = "mik"
' s[0] is char literal "m"
' s[1] is char literal "i"
' s[2] is char literal "k"
' s[3] is zero
' s[4] is undefined
' s[5] is undefined
5.1.6
Literals: [5]
5.1.6.1
5.1.6.2
You can omit either decimal integer or decimal fraction (but not both).
Here are some examples:
0.
-1.23
23.45e6
2e-5
3E+10
.09E34
5.1.6.3
'
'
'
'
'
'
=
=
=
=
=
=
0.0
-1.23
23.45 * 10^6
2.0 * 10^-5
3.0 * 10^10
0.09 * 10^34
Character literal is one character from the extended ASCII character set, enclosed with
quotes (for example, "A"). Character literal can be assigned to variables of byte and
char type (variable of byte will be assigned the ASCII value of the character). Also,
you can assign character literal to a string variable.
5.1.6.4
String literal is a sequence of characters from the extended ASCII character set,
enclosed with quotes. Length of string literal is a number of characters it consists of.
String is stored internally as the given sequence of characters plus a final null
character. String literal with nothing in between the quotes (null string) is stored as a
single null character. You can assign string literal to a string variable or to an array of
char. Here are several string literals:
"Hello world!"
"Temperature is stable"
" "
"C"
""
5.1.7
'
'
'
'
'
Operators: [5]
Operators are tokens that trigger some computation when being applied to variables and
other objects in an expression.
There are four types of operators in mikroBasic PRO for PIC:
Arithmetic Operators
Relational Operators
Boolean Operators
Bitwise Operators
5.1.7.1
71 | P a g e
Arithmetic
operators
are
used
to
perform
mathematical
computations. They have numerical operands and return numerical
results. Since the char operators are technically bytes, they can be
also used as unsigned operands in arithmetic operations.
All arithmetic operators associate from left to right. Arithmetic
operators used in mikroBasic PRO for PIC are listed in table 5.2.
Operator
+
*
/
div
mod
Operation
Operands
byte, short, word, integer,
addition
longint, longword, float
byte, short, word, integer,
subtraction
longint, longword, float
byte, short, word, integer,
multiplication
longint, longword, float
byte, short, word, integer,
division, floating-point
longint, longword, float
division, rounds down to byte, short, word, integer,
nearest integer
longint, longword
modulus, returns the
remainder of integer
byte, short, word, integer,
division (cannot be used longint, longword
with floating points)
Result
byte, short, word, integer,
longint, longword, float
byte, short, word, integer,
longint, longword, float
word, integer, longint,
longword, float
float
byte, short, word, integer,
longint, longword
byte, short, word, integer,
longint, longword
5.1.7.2
Operation
equal
not equal
greater than
less than
greater than or equal
less than or equal
5.1.7.3
Although mikroBasic PRO for PIC does not support boolean type, Boolean operators
are used for building complex conditional expressions. These operators conform to
72 | P a g e
standard Boolean logic and return either TRUE or FALSE. Boolean operators used
in mikroBasic PRO for PIC are listed in table 5.4.
Operator
and
or
xor
not
Operation
logical AND
logical OR
logical exclusive OR (XOR)
logical negation
5.1.7.4
5.1.8
Statements: [5]
Statements define algorithmic actions within a program. In the absence of specific jump
and selection statements, statements are executed sequentially in the order of appearance in
the source code.
5.1.8.1
5.1.8.2
If Statement: [5]
statements
[else
other statements]
end if
When expression evaluates to true, statements execute. If expression is
false, other statements execute. The expression must convert to a boolean
type; otherwise, the condition is ill-formed. The else keyword with an alternate block
of statements (other statements) is optional. Examples:
if x<>y then
x= y/2
y= x*3
else
x= y*3
y=x/2
end if
if (a>7) and (a<1) then
a= a + 4
end if
5.1.8.3
Nested if statements require additional attention. A general rule is that the nested
conditionals are parsed starting from the innermost conditional, with each else bound
to the nearest available if on its left:
if expression1 then
if expression2 then
statement1
else
statement2
end if
end if
The compiler treats the construction in this way:
if expression1 then
if expression2 then
statement1
else
statement2
end if
74 | P a g e
end if
In order to force the compiler to interpret our example the other way around, we have
to write it explicitly:
if expression1 then
if expression2 then
statement1
end if
else
statement2
end if
5.1.8.4
Use the select case statement to pass control to a specific program branch, based
on a certain condition. The select case statement consists of selector expression
(condition) and list of possible values. The syntax of the select case statement is:
select case selector
case value_1
statements_1
...
case value_n
statements_n
[case else
default_statements]
end select
selector is an expression which should evaluate as integral value. values can be
literals, constants or expressions and statements can be any statements. The case
else clause is optional.
First, the selector expression (condition) is evaluated. The select case
statement then compares it against all available values. If the match is found, the
statements following the match evaluate, and the select case statement
terminates. In case there are multiple matches, the first matching statement will be
executed. If none of the values matches the selector, then
default_statements in the case else clause (if there is one) are executed.
Here is a simple example of the select case statement:
select case operator
case "*"
75 | P a g e
res = n1 * n2
case "/"
res = n1 / n2
case "+"
res = n1 + n2
case else
res = 0
end select
Also, you can group values together for a match. Simply separate the items by commas:
select case reg
case 0
opmode = 0
case 1,2,3,4
opmode = 1
case 5,6,7
opmode = 2
end select
5.1.8.5
The for statement implements an iterative loop and requires you to specify the
number of iterations. The syntax of the for statement is:
for counter = initial_value to final_value [step
step_value]
statement_list
next counter
Note that the parameter step_value may be negative, allowing you to create a
countdown. Examples:
s = 0
for i = 0 to 9
s = s + a[i] * b[i]
next i
sum=0
for J = 10 to 50 step 2
sum = sum + J
next J
5.1.8.6
Use the while keyword to conditionally iterate a statement. The syntax of the while
statement is:
while expression
76 | P a g e
statements
wend
statements are executed repeatedly as long as expression evaluates true. The
test takes place before statements are executed. Thus, if expression evaluates
false on the first pass, the loop does not execute. Example:
s = 0
i = 0
while i < 10
s = s + a[i] * b[i]
i = i + 1
wend
Probably the easiest way to create an endless loop is to use the statement:
while TRUE
' ...
Wend
5.1.8.7
Do Statement: [5]
The do statement executes until the condition becomes true. The syntax of the do
statement is:
do
statements
loop until expression
statements are executed repeatedly until expression evaluates true.
expression is evaluated after each iteration, so the loop will execute statements
at least once.
Here is an example of calculating scalar product of two vectors, using the do
statement:
s = 0
i = 0
do
s = s + a[i] * b[i]
i = i + 1
loop until i = n
77 | P a g e
5.1.8.8
Break is used to stop (for, while, or do) loop from within its body. Example:
s = 0
i = 0
while TRUE
s = s + a[i] * b[i]
i = i + 1
if i=10 break
wend
s=s+55
5.1.8.9
asm
78 | P a g e
5.2.2
Displaying the Numbers 0-9 Continuously on an LED 7Segment Display:
In this application, a Common Cathode LED 7-Segment Display is
connected to RB0-RB7 of PIC 16F627. The program is continuously
displaying the numbers 0-9 on the display with a time delay of one
second between successive numbers.
program DisplayingDigits
' Declarations section
Const
Digits
as
Byte[10]=(0x3F,0x06,0x5B,0X4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F)
Dim x as Byte
79 | P a g e
main:
' Main program
TRISB=0x00
While TRUE
For x=0 To 9
PORTB=Digits[x]
Delay_ms(1000)
Next x
Wend
end.
5.2.3
end.
Chapter Six
Some Applications of Microprocessors and Microcontrollers in
Different Fields
6.1 EXAMPLES OF APPLICATIONS OF MICROPROCESSORS AND
MICROCONTROLLERS IN ELECTRICAL POWER SYSTEMS:
6.1.1 Microcontroller-Based Electrical Machine Drive
Systems: [3]
One application is a microcontroller-based variable-frequency power
inverter. This inverter is used to convert the dc voltage supply into a
variable-frequency single-phase or 3-phase ac voltage supply in
order to control the speed of a single-phase or a 3-phase induction
motor. Figure 6.1 shows the block diagram of microcontroller-based
81 | P a g e
82 | P a g e
References:
1. The 8086 Microprocessor: Programming & Interfacing The PC,
Kenneth J. Ayala, Delmar Publishers, USA, 1995. ISBN: 978-0-3140-12425.
.
84 | P a g e
85 | P a g e