programming(1).pdf

(2353 KB) Pobierz
EMCH 367
Fundamentals of Microcontrollers
367pck S01.doc
PROGRAMMING THE MICROCONTROLLER
ASSEMBLY LANGUAGE
Assembly language is of higher level than machine language and hence easier to use.
An assembly language code consists of
a) Program statement lines
b) Comment lines
A program statement is a line that contains 4 fields in the following format:
[<LABEL>]
[<OPCODE MNEMONIC>]
[<OPERANDS>]
[;<comments>]
or
[<LABEL>]
[<DIRECTIVE MNEMONIC>]
[<OPERANDS>]
[;<comments>]
where [ ] indicates an optional field that may not be always required. The fields are separated by a tab
or space. (Tab is recommended, since it ensures an orderly appearance to your code. For the same
reason, when a field is not used, the tab or blank should still to be used, such that the fields of the same
type stay aligned in same columns.) When writing <LABEL>, <OPCODE MNEMONIC> or <DIRECTIVE
MNEMONIC>, and <OPERANDS>, use upper case characters. When writing <comments>, use lower
case.
The <OPCODE MNEMONICS> correspond to the microcontroller opcodes. These mnemonics are
found in the Motorola MC68HC11 programming reference guide and related literature.
The <DIRECTIVE MNEMONICS> are native to the Assembly language. A list of directives is given in
Table 1. The directives that you will use often are shown in bold .
Table 1
Assembler directives
Name of Assembler directive
what it does
Alias for
END
end program
DB
define bytes
FCB
DW
define words
FDB
DS
define storage
RMB
EQU
equate
FCB
form constant byte
FCC
form constant characters
FDB
form double bytes
ORG
set origin
RMB
reserve memory bytes
#INCLUDE
include source file
$INCLUDE
include source file
#INCLUDE
The <OPERAND> contains a value, an expression, an address, or a label that the opcodes or the
directives need. The operand could be up to 4 bytes long, separated by commas. Some opcodes or
directives do not require operands (inherent mode).
Dr. Victor Giurgiutiu
Page 18
1/17/01
1007752727.050.png 1007752727.053.png 1007752727.054.png 1007752727.055.png 1007752727.001.png 1007752727.002.png 1007752727.003.png 1007752727.004.png 1007752727.005.png 1007752727.006.png 1007752727.007.png 1007752727.008.png 1007752727.009.png 1007752727.010.png 1007752727.011.png 1007752727.012.png 1007752727.013.png 1007752727.014.png 1007752727.015.png
EMCH 367
Fundamentals of Microcontrollers
367pck S01.doc
The constants used in the <OPERAND> can be hex, decimal, binary, or octal numbers. Table 2 gives
the assembler symbols used to this purpose.
Table 2
Assembler symbols for constants
Symbol
Meaning
Example
$<number>
hex number
$A1
<number>
decimal number
20
%<number>
binary number
%11001010
@<number>
octal number
@73
‘<string>’, ‘<string>
ASCII string
‘A’ or ‘A (the latter does not work with #INCLUDE)
The expressions used in the <OPERAND> can use any of the operators listed in Table 3
Table 3 Assembler symbols for expressions
Symbol
Meaning
Example
-
unary minus
-4
&
binary AND
%11111111&%10000000
!
binary OR
%11111111!%10000000
multiplication
*
3 * $2A
/
division
$7E/3
+
addition
1+2
-
subtraction
3-1
( )
parentheses used for grouping
3 * (1+2)
Important conventions used in the <OPERAND> are given in Table 4:
Table 4 Other important conventions
Symbol
Meaning
Example
#
immediate mode (IMM)
#$A3
;
start of comment line and of comment inside
a program statement
LDAA #$FF ; Load accA
*
alternate sign for start of comment line only
* This is a comment
, X
index X mode (IND,X)
LDAA TFLG1,X
, Y
index X mode (IND,Y)
LDAA TFLG2,Y
Dr. Victor Giurgiutiu
Page 19
1/17/01
1007752727.016.png 1007752727.017.png 1007752727.018.png 1007752727.019.png 1007752727.020.png 1007752727.021.png 1007752727.022.png 1007752727.023.png 1007752727.024.png 1007752727.025.png 1007752727.026.png 1007752727.027.png 1007752727.028.png 1007752727.029.png 1007752727.030.png 1007752727.031.png 1007752727.032.png 1007752727.033.png 1007752727.034.png 1007752727.035.png 1007752727.036.png 1007752727.037.png 1007752727.038.png 1007752727.039.png 1007752727.040.png 1007752727.041.png 1007752727.042.png 1007752727.043.png 1007752727.044.png 1007752727.045.png
 
EMCH 367
Fundamentals of Microcontrollers
367pck S01.doc
The <LABEL> is a very powerful concept that can greatly simplify the programmer’s task. The
<LABEL> consists of a string of alphanumeric characters that make up a name somehow meaningful to
the programmer. The placement of the <LABEL> can be in one of the following positions:
a) In the first column and terminates with a tab or blank character
b) In any column and terminates with a colon (:)
There are 3 different usages of the <LABEL>:
1) To assign the name inserted in the <LABEL> to a location in a program. The <LABEL>
will be assigned the address of that location
2) To assign the value of an expression or constant to the name inserted in the <LABEL>
using the EQU (equate) or SET directives.
3) To define the name of a subroutine (macro). Essentially, this is the same as 1), since an
address (the subroutine starting address) is assigned to the label.
When labels are assigned to certain addresses, one can tell the program to go to that address by
referring to the label (case 1 above). Alternatively, one can use the contents of a certain address by
referring to its label, just like when using variables (case 2 above).
A comment is prefixed by semicolon (;).When the assembler detects an semicolon, it knows that the
rest of the line is a comment and does not expect any executable instructions from it. A comment can
be a separate line (comment line) or can be inserted in a program statement. A comment line can be
also prefixed by an asterisk ( * ). The comments, either in the comment field or as a separate comment
line, are of great benefit to the programmer in debugging, maintaining, or upgrading a program. A
comment should be brief and specific, and not just reiterate its operation. A comment that does not
convey any new information needs not be inserted. When writing a comment, use lower case
characters.
A program written in Assembly language is called source file . Its extension is .ASM. When the source
file is assembled, two files are generated:
a) Object file that can be run in the microcontroller. The Motorola object file is in ASCII-HEX
format. Its generic name is “S19 file’. Its extension is .S19
b) List file, extension .LST, that contains the original code in Assembly language and the
corresponding hex codes resulting from the Assembly process. The list file is used by the
programmer to verify and debug his/her coding of the program.
The .ASM files can be opened, viewed, edited and saved in the THRSIM11 application. Alternatively, all
three file types (.ASM, .LST, .S19) can be also processed in a text editor, e.g., the Notepad application.
Examples of .ASM and .LST files follow.
Addressing Modes
Inherent Mode is implied and requires no programming action.
Immediate Mode means that the number contained in the operand will be immediately used.
Direct and Extended Modes use the number contained in the operand to signify an address where the
required information should be retrieved from or deposited to. The Extended mode is automatically
used for addresses greater than FF.
Index Mode is used by adding the operand to the value already existing in the Index X or Y, as
selected. In this case, the operand acts as an offset.
Relative Mode uses the operand as an offset relative to the present Program Counter value.
Dr. Victor Giurgiutiu
Page 20
1/17/01
EMCH 367
Fundamentals of Microcontrollers
367pck S01.doc
MICROCONTROLLER COMMANDS
(Section 6 and Section A of M68HC11 Reference Manual)
The 6811 microcontroller has 145 different commands. These commands can be grouped into several
categories. The categories and the commands in those categories are listed below:
1) Arithmetic operations:
a) Addition:
ABA, ABX, ABY, ADCA, ADCB, ADDA, ADDB, ADDD, INC, INCA, INCB, INS, INX, INY
b) Subtraction:
SBA, SBCA, SBCB, SUBA, SUBB, SUBD, DEC, DECA, DECB, DES, DEX, DEY
c) Multiplication: MUL
d) Division: FDIV, IDIV
2) Logical operations: (note: logical operations are carried out on a bit by bit basis)
a) Standard logical operations: ANDA, ANDB, EORA, EORB, ORAA, ORAB, COM (Boolean
inverse), COMA, COMB
b) Operations that shift the location of the bits in the register:
ASL, ASLA, ASLB, ASLD, ASR, ASRA, ASRB, LSL, LSLA, LSLB, LSLD, LSR, LSRA, LSRB,
LSRD, ROL, ROLA, ROLB, ROR, RORA, RORB
c) Operations that compare two numbers:
BITA, BITB, CBA, CMPA, CMPB, CPD, CPX, CPY
3) Branching commands: BCC, BCS, BEQ, BGE, BGT, BHI, BHS, BLE, BLO, BLS, BLT, BMI, BNE,
BPL, BRA, BRCLR, BRN, BRSET, BSR, BVC, BVS, JMP, JSR, RTS, RTI, WAI
4) Memory/Register Functions
a) Move data into / out of memory: LDAA, LDAB, LDD, LDS, LDX, LDY, STAA, STAB, STD, STS,
STX, STY
b) Change the values in memory/registers: BCLR, BSET, CLC, CLI, CLR, CLRA, CLRB, CLV,
COM, COMA, COMB, NEG, NEGA, NEGB, SEC, SEI, SEV
c) Transfer data from one register to another: TAB, TAP, TBA, TPA, TSX, TSY, TXS, TYS,
XGDX, XGDY
5)
Stack Pointer Functions: PSHA, PSHB, PSHX, PSHY, PULA, PULB, PULX, PULY
6)
Misc.: NOP, SWI
Note: Boolean inversion commands: COM, COMA, COMB
Dr. Victor Giurgiutiu
Page 21
1/17/01
EMCH 367
Fundamentals of Microcontrollers
367pck S01.doc
SAMPLE PROGRAM IN ASSEMBLY LANGUAGE WITH MCU COMMANDS
P ROBLEM S TATEMENT
This simple program is an example of addition. It performs the operation:
VAR0 + VAR1 ã SUM
In addition, the program checks if an overflow happened during the addition process, and sets the flag
OVERFL accordingly.
P ROGRAM D ESCRIPTION
The variables are defined in lower memory starting with $0000, in the order VAR0, VAR1, SUM,
OVERFL.
LDAB with zero is used to reset the initial value of the overflow flag (optimistic!).
LDAA is used to load VAR0 into AccA
ADDA is used to add accA with VAR1. Result of addition stays in accA
BVC is used to branch over the next instruction, i.e. to LABEL1, if no overflow occurred
If an overflow occurred during the addition process, this instruction is reached and COMB is
used to invert accB from $00 to $FF.
Label1: STAA is used to store the result of addition from accA into SUM
STAB is used to store accB ($00 or $FF, depending on the logic just discussed) into the
overflow flag OVERFL
F LOWCHART
FLOWCHART
Initialize variables:
VAR0
ã
$0000
ã
VAR1
$0001
SUM
ã
$0002
OVERFL ã
$0003
Load $00 into accB as the initial (optimistic)
guess for the overflow status
Load first variable into accA
Add second variable to accA
(result stay in accA)
Y
Brach if overflow bit
is clear
N
Since overflow bit was not clear, Invert accB
LABEL1
Store result of addition from accA into SUM
Store current value of overflow flag from
accB into OVERFL
SWI
Dr. Victor Giurgiutiu
Page 22
1/17/01
1007752727.046.png 1007752727.047.png 1007752727.048.png 1007752727.049.png 1007752727.051.png 1007752727.052.png
Zgłoś jeśli naruszono regulamin