PT 2
PT 2
Octal constants
Lowercase q or uppercase Q suffix:
1234q, 1347Q
Lowercase q or uppercase Q prefix to
single-quoted value: q'11224', Q'7654321'
Decimal constants
No suffix or prefix
Lowercase d or uppercase D prefix to
single-quoted value:
d'2345', D'9843210'
Hexadecimal constants
Lowercase h or uppercase H as suffix to value
preceded by a 0:
0ABCDEh, 0A8B30H
0x as a prefix:
0x1234, 0xFFFC
Lowercase h or uppercase H prefix to
single-quoted value:
h'3344', H'FFFC'
Class types:
code memory, data memory, and EEPROM
memory
Segment within the memory space:
data segment, program segment, or EEPROM
segment: dseg, cseg, eseg
Location counter assigned to each segment
during assembly process so assembler
knows next place to use
. .equ TRUE=1
// set directive assigns a value to a label, which can be changed
. .set my_offset = 0x10
.macro add3
add @0, @1
adc @2,@0
.endmacro
Add three number together.
@0,@1,@2 are the first,second and third marro
parameter.
Add3 r15,r16,r17
.include <m2560def.inc>
Example Write a program to .cseg
.org 0x00
multiply the 8-bit numbers stored
rjmp start
at data memory locations 0x200 .org 0xF6
and 0x204, respectively, and store start: lds r2,0x200 ; fetch the number
the sum at 0x208-0x209. lds r3,0x204 ; "
mul r2,r3 ; multiply them
sts 0x208,r0 ; save the lsb of sum
sts 0x209,r1 ; save the msb of sum
// end of program
Note:
To access data in program memory, we need to use z register.
Since the location counter of program memory counts words instead
of bytes, a label in the program memory represents a word
address(=byte address/2). We should multiply a word address by 2
to translate it to byte address (num<<1)
.include <m2560def.inc>
.cseg
Example: Write a program to .org 0x00
rjmp start
load data from program memory .org 0xF6
and store at 0x208 start: ldi ZL, low(Parray <<1)
ldi ZH, high(Parray <<1)
// setpointer ZL,ZH, (parrary<<1)
lpm r0,Z
sts 0x208,r0
Loop: ......
......
rjmp loop
Note:
- logical expression
C
- statement S
Example on Board
in r16, PORTB
ori r16, 0x02
out PORTB, r16
SER Rd
Table 3.8 A summary of AVR Boolean instructions
Example:
in r16, portB ;read portB
andi r16,0x0F ;clear upper 4bit
out portB, r16 ;store back
Overview
CPU clock controls AVR instruction execution
Each AVR instruction is completed in one to five CPU
clock cycles (See Appendix A)
A time delay can be created by repeating a sequence
of instructions for a certain number of times.
Run to cursor:
quickly find out if program executes correctly up to the
cursor position
Stepping over instructions
Step into: causes the MCU to execute one instruction
at a time
Step over: if the instruction being stepped is not a call
instruction, the MCU only executes one instruction and
stops
i. Introduction
ii. Subroutine calls
iii. Stacks
iv. Examples
Converted to a subroutine:
Parameter passing
Caller of the subroutine may pass parameters, using
registers (r0 to r31 used with the AVR), stack, or
global memory
Local variable allocation and deallocation
Use CPU registers for local variables when possible;
use stack if more space is needed
Subroutine should deallocate stack space used by
local variables before returning to the caller
in r18,SPL
in r19,SPH ;Get the SP
sbiw r18,k ;allocate K byte
out SPL, r18
out SPH,r19
Result returning
Subroutine may return its computation result in
registers, stack, or global memory
Accessing local variables in the stack
Data-indirect-with displacement addressing mode
Register usage issue
Must avoid caller-callee interference
Callee: subroutine being called
P.Q. Y
-divisor
>=0?
N Y
Done? stop
Divisor
P Q 0
01011 100010010
Set Q0 M=01011
M Subtr Shift
Control Logic
n n+1
B A
A-B
Bout
Algorithm
Step 1: Let num, k, and isprime represent the number
to be tested, the loop index, and the flag to indicate if
num is a prime number
Step 2: isprime ← 0; tlimit ← square root of num;
Step 3: for k = 2 to tlimit do
if ((num % k) == 0) then return;
isprime ← 1;
return;
Example : eg05_06.asm
Example :
eg05_08.asm
i. Introduction to C
ii. Types, Operators, and Expressions
iii. Control Flow
iv. Input and Output
v. Functions and Program Structure
vi. Pointers, Arrays, Structures, Unions, and Type
Definition
vii. Using the AVR Studio IDE to Develop C Programs
(Lab1 & Lab2)
Example:
Precedence of
operators: order in
which operators are
processed
Operators at the same
level are evaluated from
left to right
Switch (index){
sum=0;
Case 10:
Out=100;
for(i=1;i<100;i++)
Case 20: sum=sum+i;
Out=200;
Case 30:
Out=400;
default:
Out=0;
}
Electrical and Computer Engineering
Dalhousie University 122
Control Flow (cont’d.)
while (expression) do
statement statement
while(expression)
How about this
one?
while(1)
int y;
int z;
};
Automatic/external/static/volatile
Automatic variables: come into existence when the
function is entered and disappear when it is left
External variables: defined outside of any function and
thus are available potentially to many functions
Pointer to functions
Example: int (*funcPtr) (int kx);
Variable funcPtr of type “pointer to function that
returns an int and that takes one int argument”
funcPtr = primeTest; // assign function name to pointer
y = CUBIC(x);
#define XX 24
#else
#define XX 23
#endif
EVACT2,EVACT1,EVACT0, EVDLY,
EVSEL3,EVSEL2,EVSEL1,EVSEL0