0% found this document useful (0 votes)
2 views

Chapter 3, microcontrollers

Chapter 3 discusses programming microcontrollers, focusing on various programming languages such as Assembly, C/C++, and BASIC. It explains the structure of assembly language, including elements like labels, instructions, operands, comments, and directives. The chapter also provides guidance on writing assembly programs and includes control directives for better code management.

Uploaded by

Jauvenel Jauvi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter 3, microcontrollers

Chapter 3 discusses programming microcontrollers, focusing on various programming languages such as Assembly, C/C++, and BASIC. It explains the structure of assembly language, including elements like labels, instructions, operands, comments, and directives. The chapter also provides guidance on writing assembly programs and includes control directives for better code management.

Uploaded by

Jauvenel Jauvi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Chapter 3

PROGRAMMING MICROCONTROLLERS

4.1. Programming Language


A programming language itself is nothing, but a collection of words, called commands or
statements and a group of rules to use them. Just like any other language. Like English has
words, also called vocabulary and a set of rules. The rest of story lies on you, the software
developer how you use these commands and grammar to make anything useful. A number of
programming languages are available, these include Assembly, C / C++, BASIC, PASCAL,
JAL and many others. All of these languages differ in the set of commands.
• Assembly Language: Assembly is the most generic programming language, and until
recently this was the only language available for microcontrollers. Assembly language has
command set which matches one to one with the processor understandable instructions.
How ever you write those instructions in English like manner, like MOV AX, 2 tells the
processor to write a value of 2 into AX register. This language is very powerful in terms of
control over the processor. Essentially you have total control over the processor. Writing
applications in assembly is difficult however, as you have to remember the functions of
specific registers, and memory locations etc. the program lacks structured approach and is
quite lengthy. Any way the source program which you write in assembly has an extension
.ASM this text file containing assembly language instructions is assembled using a software
called assembler intothe .hex file.
• C/C++ Languages: C is the language of professionals. This is a high-level language, and
contains many powerful commands, which would otherwise require lots of commands in
assembly. A number of compilers using C as a programming language are available. Their
light-versions can also be downloaded to work with them. Differences among various
compilers is in the quality and types of libraries offered by the manufacturer. Libraries are
pre- compiled codes, in other words commands available to us. The more extensive is the
library, more easily it will be for you to write software. (review EEEE3106, programming
C++)
• BASIC Language: BASIC stands for beginner’s All Purpose Symbolic Instruction Code.
This is a very popular programming language, both for microcontrollers as well as
PCs. The
commands and syntax of language is fairly simple, and English like. The beginner therefore

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.

Figure 3.1. The Compilation progress of a program

4.2. Assembly Language Elements

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.

The STC and NOP instruction, for example, has no operands:


stc ; set Carry flag and
NOP; no operation
The INC instruction has one operand:

inc eax ; add 1 to EAX


The MOV instruction has two operands:
mov count,ebx ; move EBX to count

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.

4.3. Writing a Sample Program

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

#DEFINE Exchange one part of text for another

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

Similar directives: #UNDEFINE, IFDEF,IFNDEF


INCLUDE Include an additional file in a program

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.

CONSTANT Gives a constant numeric value to the textual designation

Syntax:
Constant <name>=<value>

Description:
Each time that <name> appears in program, it will be replaced with <value>.

Example:
ConstantMAXIMUM = 100
Constant Length = 30

Similar directives: SET, VARIABLE

VARIABLE Gives a variable numeric value to textual designation

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

Similar directive: SET, CONSTANT

SET Defining assembler variable

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

Similar directives: EQU, VARIABLE

EQU Defining assembler constant

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

Similar instructions: SET

4.5. PIC Programming in Assembly

Redirected to the Lecture Notes on “PIC16F87-Programming-assembly”

To install MPLAP use the link “https://www.pololu.com/docs/0J62/10”

9|Page

You might also like