Chapter 3, microcontrollers
Chapter 3, microcontrollers
PROGRAMMING MICROCONTROLLERS
1|Page
finds it the best to start with.
The figure below illustrates what happens during the process of compiling a program from
higherlevel to low level programming language.
In assembly language, numbers can be represented in decimal, hexadecimal, or binary form. We will
illustrate this with a number 240. Decimal numbers start with a dot, hexadecimal with Ox, and binary start
with b with the number itself under quotes ‘. The basic elements of assembly language are: Labels,
instructions, operands, directives, comments.
.24O decimal
OxFO hexadecimal
b'1111OOOO' binary
4.2.1. Labels
A label is a textual designation (generally an easy-to-read word) for a line in a program, or section of
a program where the microcontroller can jump to- or even the beginning of set of lines of a program.
It can also be used to execute program branching (such as Goto ) and the program must have a
condition that must be met for the Goto instruction to be executed. It is important for a label to start
with a letter of the alphabet or with an underline "_". The length of the label can be up to 32 characters.
It is also important that a label starts in the first column.
2|Page
4.2.2. Instructions
An instruction is a statement that becomes executable when a program is assembled. Instructions are
translated by the assembler into machine language bytes, which are loaded and executed by the CPU at
runtime. An instruction contains four basic parts:
• Label (optional)
• Instruction mnemonic (required)
• Operand(s) (usually required)
• Comment (optional)
The way we write an instruction is also called instruction "syntax". The basic instruction syntax:
[label:] mnemonic [operands] [;comment]
An instruction mnemonic is a short word that identifies an instruction. In English, a mnemonic is a device
that assists memory. Similarly, assembly language instruction mnemonics such as mov, add, and sub provide
hints about the type of operation they perform. Following are examples of instruction mnemonics:
• mov: Move (assign) one value to another
• add: Add two values
• sub: Subtract one value from another
• mul: Multiply two values
• jmp: Jump to a new location
• call: Call a procedure
In the following example, we can recognize a mistake in writing because instructions movlp and
gotto do not exist for the PIC16F84 microcontroller
3|Page
4.2.3. Operands
Assembly language instructions can have between zero and three operands, each of which can be a
register, memory operand, constant expression, or input-output port. A memory operand is specified
by the name of a variable or by one or more registers containing the address of a variable. A variable
name implies the address of the variable and instructs the microcontroller to reference the contents of
memory at the given address. The following are examples of assembly language instructions having
varying numbers of operands.
In a two-operand instruction, the first operand is called the destination. The second operand is the
source. In general, the contents of the destination operand are modified by the instruction. In a MOV
instruction, for example, data is copied from the source to the destination.
The IMUL instruction has 3 operands, in which the first operand is the destination, and the
following 2 operands are source operands:
imul eax,ebx,5
In this case, EBX is multiplied by 5, and the product is stored in the EAX register.
4.2.4. Comments
Comments are an important way for the writer of a program to communicate information about the
program’s design to a person reading the source code. The following information is typically
included at the top of a program listing:
4|Page
• Description of the program’s purpose
• Names of persons who created and/or revised the program
• Program creation and revision dates
• Technical notes about the program’s implementation
Comments can be specified in two ways: Single-line comments, beginning with a semicolon
character (;). All characters following the semicolon on the same line are ignored by the assembler.
Mov eax, 5; I am a comment.
Block comments, beginning with the COMMENT directive and a user-specified symbol. All
subsequent lines of text are ignored by the assembler until the same user-specified symbol appears.
For example,
COMMENT !
This line is a comment.
This line is also a comment.
!
other symbol can be used:
COMMENT &
This line is a comment.
This line is also a comment.
&
4.2.5. Directives
A directive is similar to an instruction, but unlike an instruction it is independent on the
microcontroller model, and represents a characteristic of the assembly language itself. Directives are
usually given purposeful meanings via variables or registers. For example, LEVEL can be a
designation for a variable in RAM memory at address ODh. In this way, the variable at that
address can be accessed via LEVEL designation. This is far easier for a programmer to
understand than for him to try to remember address ODh contains information about LEVEL.
5|Page
The following example illustrates a sample program written in assembly language respecting the
basic rules. When writing a program, beside mandatory rules, there are also some rules that are not
written down but need to be followed. One of them is to write the name of the program at the
beginning, what the program does, its version, date when it was written, type of microcontroller it
was written for, and the programmer's name. Since this data isn’t important for assembly translator,
it is written as comments. It should be noted that a comment always begins with a semicolon and
it can be placed in a new row or it can follow an instruction.
After the opening comment has been written, the directive must be included. This is shown in the
example below. In order to function properly, we must define several microcontroller parameters
such as:
• type of oscillator
• whether watchdog timer is turned on, and
• whether internal reset circuit is enabled.
All this is defined by the following directive:
_CONFIG _CP_OFF&_WDT_OFF&PWRTE_ON&XT_OSC
When all the needed elements have been defined, we can start writing a program. First, it is
necessary to determine an address from which the microcontroller starts, following a power supply
start-up. This is (org OxOO). The address from which the program starts if an interrupt occurs is
(org OxO4). Since this is a simple program, it will be enough to direct the microcontroller to the
beginning of a program with a "goto Main" instruction.
6|Page
4.4. Some Assembly Language Control Directives
Syntax:
#define<text> [<another text>]
Description:
Each time <text> appears in the program, it will be exchanged for <another text >.
Example:
#define turned_on 1
#define turned_off 0
Syntax:
7|Page
#include <file_name> #include "file_name”
Description:
An application of this directive has the effect as though the entire file was copied to a place where
the “include” directive was found. If the file name is in the square brackets, we are dealing with a
system file, and if it is inside quotation marks, we are dealing with a user file. The directive
“include” contributes to a better layout of the main program.
Syntax:
Constant <name>=<value>
Description:
Each time that <name> appears in program, it will be replaced with <value>.
Example:
ConstantMAXIMUM = 100
Constant Length = 30
Syntax:
Variable<name>=<value>
Description:
By using this directive, textual designation changes with particular value.
It differs from CONSTANT directive in that after applying the directive, the
value of textual designation can be changed.
Example:
Variablelevel = 20 variable
time = 13
Syntax:
<name_variable>set<value>
8|Page
Description:
To the variable <name_variable> is added expression <value>. SET directive
is similar to EQU, but with SET directive name of the variable can be
redefined following a definition.
Example:
level set 0
length set 12
level set 45
Syntax:
<name_constant> equ <value>
Description:
To the name of a constant <name_constant> is added value <value>
Example:
five equ 5
six equ 6
seven equ 7
9|Page