Beruflich Dokumente
Kultur Dokumente
The AVR documentation and assembler use several notations for numbers, and we will
follow these in the lecture notes.
Upper nibble
Lower nibble
Use SBI and CBI to set and clear bits in I/O registers
(PORTS, timers, etc.)
In some cases I/O register lie outside of 063 I/O space,
and one has to use LDS and STS instructions, e.g., TMSK0
on the ATmega88
DEC Decrement
Subtracts one -1- from the contents of register Rd and places the result in the destination
register Rd.
The C Flag in SREG is not affected by the operation, thus allowing the DEC instruction to be
used on a loop counter in multiple- precision computations.
When operating on unsigned values, only BREQ and BRNE branches can be expected to
perform consistently. When operating on twos complement values, all signed branches are
available.
INC Increment
Adds one -1- to the contents of register Rd and places the result in the destination register
Rd.
The C Flag in SREG is not affected by the operation, thus allowing the INC instruction to be
used on a loop counter in multiple- precision computations.
When operating on unsigned numbers, only BREQ and BRNE branches can be expected to
perform consistently. When operating on twos complement values, all signed branches are
available.
CP Compare
This instruction performs a compare between two registers Rd and Rr. None of the registers
are changed. All conditional branches can be used after this instruction.
Clears a specified bit in an I/O Register. This instruction operates on the lower 32 I/O
Registers addresses 0-31.
Sets a specified bit in an I/O Register. This instruction operates on the lower 32 I/O Registers
addresses 0-31.
Sets specified bits in register Rd. Performs the logical ORI between the contents of register
Rd and a constant mask K and places the result in the destination register Rd.
Clears the specified bits in register Rd. Performs the logical AND between the contents of
register Rd and the complement of the constant mask K. The result will be placed in register
Rd.
Stack Pointer or SP
SP on ATtiny45
The stack is implemented in SRAM, and SP always points to the top of the stack and decrements with a
push instruction.
Thus, to utilize the stack using the push and pop instructions, one must initialize SP properly.
On AVRs, SP is initialized to the end of SRAM (see diagram above)
Other AVR documentation suggest that at reset, SP is initialized to 0x00, which is where the 32 general
purpose registers startsee the AVR memory map, so programmer has to initialize SP.
; Point the stack to end of SRAM.
ldi
r23,LOW(RAMEND)
; Load LSB of last SRAM address in r23
out
spl,r23
ldi
r23,HIGH(RAMEND) ; Load MSB of last SRAM address in r23
out
sph,r23
.include "m88padef.inc"
SP is located
in I/O space so
we use in and
out instructions
EOR Exclusive OR
Performs the bitwise logical EOR between the contents of register Rd and register Rr and
places the result in the destination register Rd.
Example: Assume R18 holds 0xB8 and R19 holds 0x58. Then, after the instruction
EOR R18, R19, register R19 will hold the value 0xE0:
0xB8
0b1011 1000
0x58
0b0101 1000
Bitwise Exclusive OR
0b1110 0000
= 0xE0
OR Logical OR
Performs the logical OR between the contents of register Rd and register Rr and places the
result in the destination register Rd. of register Rd and register Rr and places the result in
the destination register Rd.
Example: Assume R18 holds 0xB8 and R19 holds 0x58. Then, after the instruction
OR R18, R19, register R19 will hold the value 0xF8:
0xB8
0b1011 1000
0x58
0b0101 1000
Bitwise Exclusive OR
0b1111 1000
= 0xF8
Example: Assume R18 holds 0xB8 and R19 holds 0x58. Then, after the instruction
AND R18, R19, register R19 will hold the value 0x18:
0xB8
0b1011 1000
0x58
0b0101 1000
Bitwise AND
0b0001 1000
= 0x18
Adds an immediate value (0 - 63) to a register pair and places the result in the register pair.
This instruction operates on the upper four register pairs, and is well suited for operations on
the pointer registers. This instruction is not available in all devices. Refer to the device
specific instruction set summary
Example
ldi
ldi
lpm
r30,LOW(2*msg)
r31,2*HIGH(msg)
zl,1
; Increment Z pointer
adiw
Example: Assume R18 holds 0xB8 and R19 holds 0x58. Then, after the instruction
SUB R18, R19, register R19 will hold the value 0x60:
0xB8
0b1011 1000
0x58
0b0101 1000
0xB8-0x58
0b0110 0000
= 0x60
Example: Assume R18 holds 0xB8 and R19 holds 0x58. Then, after the instruction
SUB R18, R19, register R19 will hold the value 0xA0:
0x58
0b0101 1000
0xB8
0b1011 1000
2s complement of 0xB8
Add 2s complement of 0xB8 to 0x58
0b0100 1000
0b1010 0000
= 0xA0
After ROL:0x71
After ROR:0xDC
Embedded Systems and Software, 55:036. The University of Iowa, 2012
I/O address
Sign Bit
SRAM Address
Negative Flag
2s Complement
Overflow Flag
Carry Flag
Zero Flag
Example: Assume R18 holds the value 0x71. Then, after the instruction
neg r18
R18 will hold 0x8F. How? Recall to get 2s complement, invert bits and add 1
0x71
0b0111 0001
Bits inverted
0b1000 1110
Add 1
+0b0000 0001
2s Complement
0b1000 0001
=0x8F
Before swap r1
After swap r1
We will use swap instruction when working with LCDs later in this class.
SLEEP
This instruction sets the circuit in sleep mode defined by the MCU Control Register.
One can configure the microcontroller so that it wakes up when there is a high-low or lowhigh (i.e., pin change) on some of the pins.
One can configure the microcontroller so that it wakes up when WDT times out
Example: In the snippet below, for LPM a destination register is not supplied, so R0 is
implied
ldi
ldi
lpm
r30,LOW(2*msg)
r31,2*HIGH(msg)
zl,1
; Increment Z pointer
adiw
displayC:
ldi
r30,LOW(2*msg)
ldi
r31,2*HIGH(msg)
L20:
lpm
r2,Z+
tst
r2
breq done
rjmp L20
done:
ret
For Further information on the LPM instruction, please see the Atmel
Application Note AVR108: Setup and Use of the LPM Instruction on the
companys website.
The assemblers text substitution allows one to use symbols rather than numbers
.equ
.equ
PORTB
DDRB
= 0x18
= 0x17
.include "tn45def.inc"
.cseg
.include "tn45def.inc"
sbi
DDRB,1
loop:
sbi
cbi
rjmp
PORTB,1
PORTB,1
loop
sbi
0x17,1
loop:
These are program addresses
000001
000002
000003
000004
sbi
cbi
cbi
rjmp
0x17,1
0x18,2
0x18,1
0x01
The assemblers text substitution allows one to use symbols rather than numbers
.include "tn45def.inc"
.cseg
sbi
sbi
sbi
DDRB,1
loop:
sbi
cbi
rjmp
sbi
000000
000001
sbi
cbi
cbi
rjmp
PORTB,1
PORTB,1
loop
0x17,1
loop:
000001
000002
000003
000004
DDRB,1
DDRB,2
loop:
sbi
cbi
rjmp
PORTB,1
PORTB,1
loop
000000
.include "tn45def.inc"
.cseg
0x17,1
0x18,2
0x18,1
0x01
sbi 0x17,1
sbi 0x17,2
loop:
000002
000003
000004
000005
sbi
cbi
cbi
rjmp
0x17,1
0x18,2
0x18,1
0x02
Assembler Directives
Assembler directives instruct the assembler what to do
The directives are not translated directly into opcodes.
Instead, they are used to adjust the location of the program in memory, define
macros, initialize memory and so on.
.include "tn45def.inc"
.cseg
sbi
DDRB,1
loop:
sbi
cbi
rjmp
PORTB,1
PORTB,1
loop
ASM Directive
The BYTE directive reserves memory resources in the SRAM. In order to be able to refer to
the reserved location, the BYTE directive should be preceded by a label. The directive takes
one parameter, which is the number of bytes to reserve.
The directive can only be used within a Data Segment. Note that a parameter must be given.
The allocated bytes are not initialized.
ASM Directive
The CSEG directive defines the start of a Code Segment. An Assembler file can consist
of several Code Segments, which are concatenated into one Code Segment when
assembled. The BYTE directive can not be used within a Code Segment.
The default segment type is Code. The Code Segments have their own location counter
which is a word counter.
ASM Directive
The DSEG directive defines the start of a Data Segment. An Assembler file can consist of
several Data Segments, which are concatenated into one Data Segment when assembled. A
Data Segment will normally only consist of BYTE directives (and labels).
The ORG directive (later) can be used to place the variables at specific locations in the
SRAM. The directive does not take any parameters.
ASM Directive
The ESEG directive defines the start of an EEPROM Segment. An Assembler file can consist
of several EEPROM Segments, which are concatenated into one EEPROM Segment when
assembled. The BYTE directive can not be used within an EEPROM Segment.
The EEPROM Segments have their own location counter which is a byte counter. The ORG
directive (later) can be used to place constants at specific locations in the EEPROM memory.
ASM Directive
The DB directive reserves memory resources in the program memory or the EEPROM
memory. In order to be able to refer to the reserved locations, the DB directive should be
preceded by a label.
The expression list is a sequence of expressions, delimited by commas. Each expression
must evaluate to a number between -128 and 255. If the expression evaluates to a negative
number, the 8 bits two's complement of the number will be placed in the program memory or
EEPROM memory location.
ASM Directive
The DW directive reserves memory resources in the program memory or EEPROM memory.
In order to be able to refer to the reserved locations, the DW directive should be preceded by
a label.
The expression list is a sequence of expressions, delimited by commas. Each expression
must evaluate to a number between -32768 and 65535. If the expression evaluates to a
negative number, the 16 bits two's complement of the number will be placed in the program
memory location.
ASM Directive
The DEF directive allows the registers to be referred to through symbols. A defined symbol
can be used in the rest of the program to refer to the register it is assigned to. A register can
have several symbolic names attached to it. A symbol can be redefined later in the program.
A symbol can be redefined later in the program.
ASM Directive
The EQU directive assigns a value to a label. This label can then be used in later
expressions.
A label assigned to a value by the EQU directive is a constant and can not be changed or
redefined.
ASM Directive
The SET directive assigns a value to a label. This label can then be used in later
expressions.
A label assigned to a value by the SET directive can be changed later in the program.
ASM Directive
The DEVICE directive allows the user to tell the Assembler which device the code is to be
executed on. If this directive is used, a warning is issued if an instruction not supported by
the specified device occurs in the code. If the size of the Code Segment or EEPROM
Segment is larger than supported by the specified device, a warning is issued.
ASM Directive
The INCLUDE directive tells the Assembler to start reading from a specified file. The
Assembler then assembles the specified file until end of file (EOF) or an EXIT directive is
encountered. An included file may itself contain INCLUDE directives.
Contents of iodefs.asm
Include this in another AM program, and one can use symbols such as sreg
rather than 0x3f:
ASM Directive
The EXIT directive tells the Assembler to stop assembling the file. Normally, the Assembler
runs until end of file (EOF). If an EXIT directive appears in an included file, the Assembler
continues from the line following the INCLUDE directive in the file containing the INCLUDE
directive.
ASM Directive
The ORG directive sets the location counter to an absolute value. The value to set is
given as a parameter.
If an ORG directive is given within a Data Segment, then it is the SRAM location counter
which is set, if the directive is given within a Code Segment, then it is the Program memory
counter which is set and if the directive is given within an EEPROM Segment, then it is the
EEPROM location counter which is set.
ASM Directive
ASM Directive
;
;
;
;
.CSEG
SUBI16 0x1234,r16,r17
Assembler Functions
ldi r19,low(0x07F3)
ldi r20,high(0x07F3)
ldi r19,0xF3
ldi r20,0x07
Generate opcodes
Assembler Functions
LOW and HIGH are often used, others less frequently, and we will not cover these
in this course
Assembler Operators
Assembler function and operators are related to assembler directives
The functions and operators are not translated directly into opcodes.
Instead, they are used to do calculations on the assembly language source file
.EQU c1 = 0b11011010
.equ c2 = 0b10101010
ldi r18,c1^c2
Text substitution & apply functions
ldi r18,0x70
Generate opcodes
Division
Subtraction
Shift right
Bitwise OR
Logical OR
Bitwise NOT
Equal
Greater Than
Assembler Preprocessor
The AVRASM2 preprocessor is modeled after the C preprocessor, with some exceptions
(see AVRASM2 documentation).
#define
#if
#pragma
Operators:
#elif
#ifdef
#undef
# (stringification
#else
#ifndef
#warning
## (concatenation)
#endif
#include
# (empty directive)
#error
#message
.EQU c1 = 0b11011010
.equ c2 = 0b10101010
#define DEBUG
#ifdef DEBUG
ldi r18,low(c1|c2)
rcall delay
#else
ldi r18,low(c1^c2)
#endif
Assembler Preprocessor
The AVRASM2 preprocessor is modeled after the C preprocessor, with some exceptions
(see AVRASM2 documentation).
#define
#if
#pragma
Operators:
#elif
#ifdef
#undef
# (stringification
#else
#ifndef
#warning
## (concatenation)
#endif
#include
# (empty directive)
#error
#message
.EQU c1 = 0b11011010
.equ c2 = 0b10101010
#define DEBUG
#ifdef DEBUG
ldi r18,low(c1|c2)
rcall delay
#else
ldi r18,low(c1^c2)
#endif
Microcontroller Configuration/Fuses
AVR ATtiny45 8-Pin Microcontroller
Except for GND and VCC all other pins can perform at least 4 functions
PB5 can be hardware RESET or an ADC input
PB3 & PB4 can be ADC inputs or where crystal for external oscillator
goes
How does one configure controller for the external environment?
Microcontroller Configuration/Fuses
The Configuration Fuses (Configuration Bits) are the settings
that configure a microcontroller for the external environment
it is expecting to find
Typical settings include