Uploaded by hambileo

Lecture 3 Introduction to Microcontroller Programming

advertisement
HETT206: Micrprocessors &
Microcontrollers Systems
Design
Unit 4: Introduction to
Microcontroller Programming
Prof E Mashonjowa
Introduction
• A microcontroller is designed to control hardware.
• To this end it has a number of digital I/O ports; it
may also have ADC and DAC devices so that it can
interact with analogue signals.
• It will also contain a number of peripheral devices
such as timer modules, serial I/O, an ADC unit and
perhaps other specialized peripheral modules such
as a CAN controller or serial peripheral controller.
• The instruction set for a microcontroller will
therefore be designed to allow efficient control of
both its internal devices and the surrounding
infrastructure (controlled by its ports).
Prof E Mashonjowa
2
Parallel port I/O: Shown are two 4 bit ports - one input, one
output. To turn the transistor and hence L1 on, its base must
be brought to 0V. Bringing it to 3.3V by writing a logic '1' to the
appropriate bit (b0) causes L1 to turn off. The closure of S1
causes b1 of the input port to be driven high (logic '1' or 3.3V).
S2 is open so the resistor shown pulls b0 low (logic '0' or 0V).
Prof E Mashonjowa
3
The microcontroller instruction set
• The main requirements for the instruction set are:
– Control of the register set of the microcontroller
in an easy manner
– Ability to access ports and other peripheral
control and status registers
– Ability to access individual bits of a port or
register
– Good masking operation ability to test port and
register bits individually
– Good interrupt infrastructure
– A rich set of addressing modes
Prof E Mashonjowa
4
Introduction to programming
• The microcontroller's CPU reads program code
from memory, one instruction at a time, decodes
each instruction, and then executes it.
• All memory content—both program code and
data—is in binary form: strings of 1s and 0s.
• Instructions are binary codes that tell the CPU what
to do; while the data values are the binary
(numerical) values that the CPU adds, subtracts,
handles as address values, or otherwise operates
on or processes in accordance with the instructions.
Prof E Mashonjowa
5
What is a program?
• A program is a series of machinelanguage instructions describing the
sequence of operations to be carried
out.
• To drive the CPU, therefore, you need
to supply it with a machine-language
program.
Prof E Mashonjowa
6
Machine Language: The Only
Language Your CPU Understands
• Machine language is the only language the CPU
understands.
• Human programmers, however, find it very difficult
to program using instructions composed of arbitrary
sequences of 1s and 0s.
• Programmers needing to program at this low level
therefore use assembly language instead—a
language that uses meaningful text strings in place
of arbitrary binary strings.
Prof E Mashonjowa
7
Machine Language: The Only
Language Your CPU Understands
• Consider an instruction that loads a numerical
value of 2 into Register A. (Note: registers are
storage locations located within the CPU.)
0
1
0
1
0
0
0
1
0
0
0
0
0
0
1
0
MOV A, #02
Prof E Mashonjowa
Machine
Language
Instruction
Assembly
Language
8
The Problem with Machine
Language
• While assembly language is clearly more workable (for
humans) than machine language, it is still quite difficult
to work with.
• Another problem is that machine language
implementation is different on each CPU type.
• Since assembly code is closely mapped to machine
code, assembly programmers would need to rewrite
their code each time the CPU type is changed.
• This need for continual rewriting would severely reduce
the programmer's productivity and job satisfaction.
Prof E Mashonjowa
9
The C Programming Language: A
Better Way to Program
• Use of higher-level programming languages, such
as C, resolves these problems.
• Programs written in C are very portable, since they
can generally work on any CPU type without
modification.
• They are also easier (for humans) to write and
read, since they are more compact and use a much
more descriptive set of English words and
abbreviations.
Prof E Mashonjowa
10
Prof E Mashonjowa
11
The C Programming Language: A
Better Way to Program
• While humans find C code relatively easy to work
with, CPUs cannot understand it.
• So it becomes necessary to convert the C code
(source code) into machine code (object code) that
the CPU can use.
• This conversion is carried out by a program called
a compiler.
• The resulting object code must then be written into
appropriate memory locations in order to enable
execution by the CPU.
Prof E Mashonjowa
12
The C Programming Language: A
Better Way to Program
• Because modern programs are quite complex, it is
common practice to divide a programming job into
multiple C programs.
• After compiling these programs into object files, it is
necessary to link the objects together into a single
machine-language program.
• This linkage operation is performed by another
program, called a linker.
Prof E Mashonjowa
13
The Programming Requirements for
a Microcontroller
• The programming requirements for a
microcontroller will normally include:
– A text editor to develop the source program code
– An Assembler to produce a machine executable
program with linked library modules
– Some form of debugging to allow the program to
be tested
Prof E Mashonjowa
14
• An assembly language program is a program written using
labels, mnemonics, and so on, in which each statement
corresponds to a machine instruction. Often called source
code or symbolic code.
• A machine language program is a program containing
binary codes that represent instructions to a computer.
Often called object code. Executable by a computer.
• An assembler is a program that translates an assembly
language program into a machine language program.
Object code may be in "absolute" or in "relocatable" form.
In the latter case, "linking" is required to set the absolute
address for execution.
• A linker is a program that combines relocatable object
programs (modules) and produces an absolute object
program that is executable by a computer. Sometimes
called a "linker/locator“.
15/81
• A segment is a unit of code or data memory. A segment
may be relocatable or absolute. A relocatable segment has
a name, type, and other attributes that allow the linker to
combine it with other partial segments, if required, and to
correctly locate the segment. An absolute segment has no
name and cannot be combined with other segments.
• A module contains one or more segments or partial
segments. A module has a name assigned by the user. The
module definitions determine the scope of local symbols.
An object file contains one or more modules. A module
may be thought of as a "file" in many instances.
• A program consists of a single absolute module, merging all
absolute and relocatable segments from all input modules.
A program contains only the binary codes for instructions
(with addresses and data constants) that are understood by
a computer.
16/81
Stages in the Production of a Program
Prof E Mashonjowa
17
Stages in the Production of a Program
• The job of the Assembler is to convert the source code produced by
the programmer into an object code file. It works by replacing each
line of assembly instruction code with the corresponding machine
code instruction that will be executed on the target microcontroller.
• The object code file cannot be executed on the target microcontroller. It needs to be passed through a program called a Linker.
• The job of the Linker is to take the object file produced by the
Assembler and link it with standard library modules (that have
already been pre-assembled into object files) to produce a complete
working executable file that can be loaded into the memory of the
microcontroller.
• Sometimes the Assembler and Linker are combined into one
program so that assembling a source program automatically links it
as well. This is the case with the PIC Assembler.
Prof E Mashonjowa
18
Stages in the Production of a Program
• Note that the only stage that requires a lot of user input is the initial
production of the user source code file. The rest of the program
production involves using the Assembler and Linker tools.
• 'Filename' is the name of the program that the user is developing,
thus the output of the Editor is the source file 'filename.asm'.
• The output of the Assembler is the object file 'filename.obj'.
• The Linker links the 'filename.obj' with any Library object files that
the program might use. It will also link any user object files that the
user might want to include.
• The Linker output is the executable file to run on the target system.
• A program called a Loader takes your executable .hex file from the
hard disk and loads it onto the target microcontroller.
Prof E Mashonjowa
19
Hex file and the target microcontroller
• The original source file will contain instructions about where to
place the .hex file in the target microcontroller memory. Typically,
this instruction will be:
• ORG 0000H
• This instruction ORGanises the target program to be loaded to the
target microcontroller memory address 0000 hex.
• ie. at the address 0000 0000 0000 0000 binary.
• Other key addresses may also be needed, such as the interrupt
vector addresses for any interrupt service routines (isr) that the
program may use.
• Generally, the Loader program is integrated into the development
environment so that it is performed by the programmer tools.
• For example, in the MPLAB PIC development environment, the
PICSTART menu will deal with loading the target program into the
memory of the EPROM Programmer system which will then
program the target microcontroller which has been placed into the
zero insertion socket of the programmer system.
Prof E Mashonjowa
20
Source Code Writing
• We will look at the way in which an assembly language program is
structured and introduce some of the assembly language
instructions of the PIC microcontroller.
• The source code for a microcontroller can be written in assembly
language or in a high level language such as C (provided) a suitable
compiler is available). For this part of the course we will use
assembly language programming.
• A typical assembly language program will consist of:
– some assembler directives
– sub-routines if they are needed
– the main program code
– Assembler directives are a collection of commands that tell the
assembler such things as the type of microcontroller being
used, its clock speed, etc. They also allow names to be used for
memory locations, ports and registers, so making the program
more readable.
Prof E Mashonjowa
21
Some assembler directives
• The EQU directive associates a name with a physical value such as an
address in memory.
• Name EQU physical_value
• After the equate directives the names can be used instead of register
addresses within the PIC microcontroller,
ie. meaningful names can be used instead of the numerical values.
• eg. CLRF RTCC will clear all the bits in the timer counter register
(which has the physical address of 1, or 0001 hex).
• The instruction CLRF 1 would do the same, but the use of the label
RTCC makes the instruction more readable.
• Some of the equates refer to individual bits within a particular register.
• eg. CARRY EQU 4 refers to the fourth bit in the status register.
• The name CARRY only has meaning when referring to the status
register.
• eg.BCF STATUS,CARRY will clear the carry bit (bit 4) in the status
register,
Prof E Mashonjowa
22
Equate directives for I/O registers
• A program will typically also include equate
directives for the I/O registers.
• More of these register labels can be declared
for other PIC registers.
• We can also declare memory equates such as
Prof E Mashonjowa
23
The PIC Instruction Set
• The instructions used by the PIC microcontroller, the PIC
instruction set falls into three groups:
– Bit operations
– Byte operations
– Literal/Control operations
• Most of these instructions execute in a single clock cycle. The
duration of a single clock cycle depends on the frequency of the
clock oscillator which is usually between 4MHz and 20MHz,
depending upon the PIC device.
• To keep EM levels low most general purpose applications will use a
4MHz clock and the execution time is therefore 1µS per
instruction.
• Each memory cycle is made up of four states so the duration of a
clock cycle (and hence the instruction execution time) is four times
the oscillator period.
• If the oscillator frequency is 4MHz, the execution time will be 1µS
per instruction. If the oscillator frequency is 20MHz, the execution
time will be 200nS per instruction.
Prof E Mashonjowa
24
The Instruction Set and its Use
• The following section looks at the PIC instruction set, giving
examples of how the instructions are used.
• Bit Operations
• One of the principal requirements of a microcontroller is to
control the external environment via the digital I/O
ports. Indeed, the first microcontrollers (Intel 8048/8031,
Motorola 6801) were basically generic microprocessors with
integrated digital I/O and a timer module.
• Controlling digital I/O often involves turning individual bits on
and off, taking care not to affect any other I/O bits.
Traditionally this has been achieved by using ANDing and
ORing mask operations, but most modern microcontrollers
now provide special instructions to selectively set or reset
individual bits of a selected port.
• The PIC has two instructions, one to turn on a selected bit of a
port, the other to turn it off.
TURNING ON or OFF A GROUP OF BITS
• Note that there is no instruction for turning on or off
a group of bits. To do this it is necessary to set or
reset each bit in turn or use a mask.
• For example, assuming that all bits of portB start at
zero, the effect of three such operators in sequence
would be as follows
• BSF portb,0
• BSF portb,1
• BCF portb,0
Prof E Mashonjowa
0000 0001
0000 0011
0000 0010
26
Branching Instructions
• All microcontrollers need an instruction to jump out of the
program sequence. The instruction to do this on the PIC is the
GOTO instruction.
• GOTO K causes the program to jump to the label/address k.
eg. GOTO 0050 causes the program to go to program memory
address 0050 hex
.
• GOTO instructions cause the program to branch to another part
of the program. They are often used to cause the program to
loop back to repeat an earlier section of code.
• For example, the following program code will turn on and off
bit 5 of port A in an endless loop:
agn BSF
porta,5 ; comment
BCF
porta,5 ; comment
GOTO agn
; go back and do it again
Prof E Mashonjowa
27
Typical PIC application
• A typical PIC application may involve testing an input bit and
performing some action when the bit changes state. E.g the
input could be the state of a door switch and the action could
be to turn on a relay which then switches on a motor.
• The PIC provides two instructions, Bit Test and Skip if Set
(BTFSS) and Bit Test and Skip if Clear (BTFSC).
• BTFSS f,b Test bit b in file register f and skip the next .
instruction if it is set (ie.1)
• BTFSC f,b Test bit b in file register f and skip the next .
instruction if it is clear (ie.0)
Prof E Mashonjowa
28
ASSEMBLY LANGUAGE PROGRAM
FORMAT
• Assembly language programs contain the following:
–
–
–
–
Machine instructions
Assembler directives
Assembler controls
Comments
• Machine instructions are the familiar mnemonics of
executable instructions (e.g., ANL).
• Assembler directives are instructions to the assembler
program that define program structure, symbols, data,
constants, and so on (e.g., ORG).
• Assembler controls set assembler modes and direct
assembly flow (e.g., $TITLE).
• Comments enhance the readability of programs by
explaining the purpose and operation of instruction
sequences.
29/81
• Lines containing machine instructions or assembler
directives must be written following specific rules
understood by the assembler.
• Each line is divided into "fields" separated by space
or tab characters.
• The general format for each line is as follows:
[label:]
mnemonic [operand][,operand][.. .][;comment]
30/81
Label Field
• A label represents the address of the instruction
(or data) that follows.
• Term "label" always represents an address.
• The term "symbol" is more general.
• Labels are one type of symbol and are identified
by the requirement that they must terminate with
a colon (:).
• Symbols are assigned values or attributes, using
directives such as EQU, SEGMENT, BIT, DATA, etc.
• Symbols may be addresses, data constants,
names of segments, or other constructs
conceived by the programmer.
31/81
PAR
EQU
500
START: MOV A, #0FFH
;"PAR" IS A SYMBOL WHICH
;REPRESENTS THE VALUE 500
;"START" IS A LABEL WHICH
;REPRESENTS THE ADDRESS OF
;THE MOV INSTRUCTION
• A symbol (or label) must begin with a letter, question
mark, or underscore (_); must be followed by letters,
digit, "?'", or "_"; and can contain up to 31
characters.
• Symbols may use upper- or lowercase characters, but
they are treated the same.
• Reserved words may not be used.
32/81
Mnemonic Field
• Instruction mnemonics or assembler
directives go into mnemonic field, which
follows the label field.
• Examples of instruction mnemonics are ADD,
MOV, DIV, or INC.
• Examples of assembler directives are ORG,
EQU, or DB.
33/81
Operand Field
• The operand field follows the mnemonic field.
• This field contains the address or data used by the
instruction.
• A label may be used to represent the address of the
data, or a symbol may be used to represent a data
constant.
• The possibilities for the operand field are largely
dependent on the operation.
34/81
Comment Field
• Remarks to clarify the program go into
comment field at the end of each line.
• Comments must begin with a semicolon (;).
35/81
Special Assembler Symbols
• Used for the register-specific addressing modes.
• These include A, R0 through R7, DPTR, PC, C, and AB.
• In addition, a dollar sign ($) can be used to refer to
the current value of the location counter.
SETB
C
INC DPTR
JNB TI, $
NOTE: DPTR = data pointer register
36/81
Indirect Address
• For certain instructions, the operand field may
specify a register that contains the address of the
data.
• The commercial "at" sign (@) indicates address
indirection and may only be used with R0, R1, the
DPTR, or the PC, depending on the instruction.
ADD
A, @R0
MOVC
A, @A+PC
37/81
Immediate Data
• Instructions using immediate addressing provide data in the
operand.
• Preceded with a pound sign (#).
CONSTANT EQU 100
MOV A, #0FEH
ORL 40H, #CONSTANT
• Immediate data are evaluated as a 16-bit constant, and then
the low-byte is used.
• All bits in the high-byte must be the same (00H or FFH) or the
error message "value will not fit in a byte" is generated.
38/81
• The following instructions are syntactically correct:
MOV A, #0FF00H
MOV A, #00FFH
• These generate error messages:
MOV A, #0FE00H
MOV A, #01FFH
• If signed decimal notation is used, constants from -256 to +255 may also
be used.
• The following two instructions are equivalent (and syntactically correct):
MOV A, #-256
MOV A, #0FF00H
• Both instructions above put 00H into accumulator A.
39/81
Data Addresss
• Many instructions access memory locations using
direct addressing and require an on-chip data
memory address (00H to 7FH) or an SFR address
(80H to 0FFH) in the operand field.
• Predefined symbols may be used for the SFR
addresses.
MOV
A, 45H
MOV
A, SBUF
;SAME AS MOV A, 99H
40/81
Bit Address
• One of the most powerful features of the 8051 is the ability to access
individual bits without the need for masking operations on bytes.
• A bit address in internal data memory (00H to 7FH) or a bit address in the
SFRs (80H to 0FFH).
• Three ways to specify a bit address:
• (a) explicitly by giving the address,
• (b) using the dot operator between the byte address and the bit position,
and
• (c) using a predefined assembler symbol.
SETB 0E7H ;EXPLICIT BIT ADDRESS
SETB ACC.7 ;DOT OPERATOR (SAME AS ABOVE)
JNB
TI, $
;"TI" IS A PRE-DEFINED SYMBOL
JNB
99H, $ ;(SAME AS ABOVE)
41/81
Code Address
• Used in the operand field for jump
instructions.
• The code address is usually given in the form
of a label.
HERE:
.
.
SJMP HERE
42/81
Generic Jumps and Calls
• ASM51 allows programmers to use a generic JMP or CALL
mnemonic.
• The assembler converts the generic mnemonic to a "real"
instruction following a few simple rules.
• The generic mnemonic converts to the short form (for JMP
only) if no forward references are used and the jump
destination is within -128 locations, or to the absolute form if
no forward references are used and the instruction following
the JMP or CALL instruction is in the same 2K block as the
destination instruction.
• If short or absolute forms cannot be used, the conversion is to
the long form.
43/81
• The conversion is not necessarily the best programming choice.
• For example, if branching ahead a few instructions, the generic JMP
will always convert to LJMP even though an SJMP is probably better.
• The third jump assembles as LJMP because the destination (FINISH) is
not yet defined when the jump is assembled (i.e., a forward reference
is used).
ASSEMBLE-TIME EXPRESSION
EVALUATION
• Values and constants in the operand field may be
expressed three ways:
• (a) explicitly (e.g., 0EFH),
• (b) with a predefined symbol (e.g., ACC), or
• (c) with an expression (e.g., 2 + 3).
• The use of expressions provides a powerful
technique for making assembly language
programs more readable and more flexible.
• All expression calculations are performed using
16-bit arithmetic; however, either 8 or 16 bits are
inserted into the instruction as needed.
45/81
Number Bases
• Constants must be followed with "B" for binary, "O" or "Q" for
octal, "D" or nothing for decimal, or "H" for hexadecimal.
• Following instructions are the same:
MOV
A, #15
MOV
A, #1111B
MOV
A, #0FH
MOV
A, #17Q
MOV
A, #15D
• Note that a digit must be the first character for hexadecimal
constants in order to differentiate them from labels (i.e.,
"OA5H" not "A5H").
46/81
Character Strings
• Strings using one or two characters may be used as
operands in expressions. The ASCII codes are
converted to the binary equivalent by the assembler.
Character constants are enclosed in single quotes (').
CJNE A, #'Q', AGAIN
SUBB A, #'0'
MOV DPTR, #'AB'
MOV DPTR, #4142H
;CONVERT ASCII DIGIT
; TO BINARY DIGIT
;SAME AS ABOVE
47/81
Arithmetic Operators
•
•
•
•
•
•
+
addition
subtraction
*
multiplication
/
division
MOD
modulo (remainder after division)
The following two instructions are the same:
MOV
A, #10 + 10H
MOV
A, #lAH
• The following two instructions are also the same:
MOV
A, #25 MOD 7
MOV
A, #4
• Since the MOD operator could be confused with a symbol,
it must be separated from its operands by at least one
space or tab character, or the operands must be enclosed
in parentheses. The same applies for the other operators
composed of letters.
48/81
Logical Operators
•
•
•
•
•
OR
logical OR
AND
logical AND
XOR
logical Exclusive OR
NOT
logical NOT (complement)
The operation is applied on the corresponding bits in each
operand.
• The following two instructions are the same:
MOV
A, #'9' AND 0FH
MOV
A, #9
• The NOT operator only takes one operand.
• The following three MOV instructions are the same:
THREE
EQU 3
MINUS_THREEEQU -3
MOV A, # (NOT THREE) + 1
MOV A, #MINUS_THREE
MOV A, #11111101B
49/81
Special Operators
•
•
•
•
•
•
SHR
shift right
SHL shift left
HIGH
high-byte
LOW
low-byte
()
evaluate first
The following two instructions are the same:
MOV
A, #8 SHL 1
MOV
A, #10H
• The following two instructions are also the same:
MOV
A, #HIGH 1234H
MOV
A, #12H
50/81
Relational Operators
• When a relational operator is used between two
operands, the result is always false (0000H) or true
(FFFFH).
• EQ =
equals
• NE <>
not equals
• LT <
less than
• LE <=
less than or equal to
• GT >
greater than
• GE >=
greater than or equal to
MOV
A, #5 = 5
MOV
A, #5 NE 4
MOV
A, #'X' LT 'Z‘
MOV
A, #'X' >= 'X‘
MOV
A, #$ > 0
MOV
A, #100 GE 50
51/81
Expression Examples
Expression
'B' - 'A‘
8/3
155 MOD 2
4*4
8 AND 7
NOT 1
'A' SHL 8
LOW 65535
(8 + 1) * 2
5 EQ 4
'A' LT 'B'
3 <= 3
Result
0001H
0002H
0001H
0010H
0000H
FFFEH
4100H
00FFH
0012H
0000H
FFFFH
FFFFH
52/81
Operator Precedence
•
•
•
•
•
•
•
•
()
HIGH LOW
* / MOD SHL SHR
+EQ NE LT LE GT GE = <> < <= > >=
NOT
AND
OR XOR
53/81
ASSEMBLER DIRECTIVES
• Instructions to the assembler program.
• They are not assembly language instructions executable
by the target microprocessor.
• However, they are placed in the mnemonic field of the
program.
• With the exception of DB and DW, they have no direct
effect on the contents of memory.
• Categories of directives:
• Assembler state control (ORG, END, USING)
• Symbol definition (SEGMENT, EQU, SET, DATA, IDATA,
XDATA, BIT, CODE)
• Storage initialization/reservation (DS, DBIT, DB, DW)
• Program linkage (PUBLIC, EXTRN, NAME)
• Segment selection (RSEG, CSEG, DSEG, ISEG, BSEG, XSEG)
54/81
Assembler State Control
• ORG (Set Origin)
• End
• Using
55/81
ORG (Set Origin)
• Format:
ORG
expression
• Alters the location counter to set a new program origin for
statements that follow.
• A label is not permitted.
ORG
ORG
100H
;SET LOCATION
;COUNTER TO 100H
($ + 1000H) AND 0F000H ;SET TO NEXT 4K
;BOUNDARY
• Can be used in any segment type.
• If the current segment is absolute, the value will be an
absolute address in the current segment.
• If a relocatable segment is active, the value of the ORG
expression is treated as an offset from the base address of
the current instance of the segment.
56/81
End
• Format:
END
• Should be the last statement in the source file.
No label is permitted and nothing beyond the
END statement is processed by the assembler.
57/81
Download