Upload
shashanksltor
View
228
Download
0
Embed Size (px)
Citation preview
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
1/29
AVR Microcontroller Family
Assembly Language ProgrammingUniversity of Akron
Dr. Tim Margush
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
2/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
2
AVR
Atmel AVR 8-Bit Processors come in a
variety of configurations and packages
They all share a common core registers,instructions, basic I/O capabilities
Our focus is the ATMega16
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
3/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
3
ATMega16 Specs
131 Instructions
32 8-bit GP registers
Throughput up to 16 MIPS
16K programmable flash (instructions)
512Bytes EEPROM 1K internal SRAM
Timers, serial and parallel I/O, ADC
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
4/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
4
AVR CPU
PC: address of next
instruction
IR: prefetchedinstruction
ID: current instruction
GPR: R0-R31 ALU: Note internal
data path
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
5/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
5
AVR Memory
Flash: Machine
instructions go here
SRAM: For runtime data
Note bus independence for
data and instructions
EEPROM: Secondary
storage
EEPROM and Flashmemories have a limited
lifetime of erase/write cycles
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
6/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
6
Flash Memory
Programs reside in word addressable flash storage Word addresses range from 0000-1FFF (PC is 13 bits)
Byte addresses range 0000-3FFF (0x4000=16K)
Harvard Architecture It is possible to use this storage area for constant data as
well as instructions, violating the true spirit of thisarchitecture
Instructions are 16 or 32-bits Most are 16-bits and are executed in a single clock
cycle
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
7/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
7
SRAM
The ATMega16 has 1K (1024 bytes) of byte
addressable static RAM
This is used for variable storage and stack spaceduring execution
SRAM addresses start at $0060 and go through
$045F The reason for not starting at zero will be covered
later
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
8/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
8
EEPROM
Electrically Erasable Programmable Read
Only Memory
Programs can read or write individual bytes
This memory is preserved when power is
removed
Access is somewhat slow; it serves as a form ofsecondary storage
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
9/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
9
Clock
All processors are pushed through their fetchexecute cycle by an alternating 0-1 signal, called aclock
The ATMega16 can use an internal or externalclock signal Clock signals are usually generated by an RC oscillator
or a crystal The internal clock is an RC oscillator programmable to 1, 2, 4,
or 8 MHz
An external clock signal (crystal controlled) can be moreprecise for time critical applications
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
10/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
10
AVR Machine Language
AVR instructions are 16 or 32-bits
Each instruction contains an opcode
Opcodes generally are located in the initial bits of aninstruction
Some instructions have operands encoded in the
remaining bits
Opcode and operands are numbers, but their containers
are simply some of the bits in the instruction
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
11/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
11
Load Immediate
LDI Rd, K Load a constant into a register (Rd = K)
1110 bbbb rrrr bbbb Limitations: 16
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
12/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
12
LDI Examples
LDI R16, $2C 1110 0010 0000 1100 or 0xE20C
LDI R27, $0F 27 is 11011
1110 0000 1011 1111 or 0xE0BF
Note that this instruction always
starts with E, has the two nybbles of K at positions 2 and 0,
and encodes the register in nybble 1
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
13/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
13
Add Registers
ADD Rd, Rr
Add the contents of two registers, store result in
Rd (Rd = Rd + Rr) 0000 11rd dddd rrrr
Any registers: 0
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
14/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
14
ADD Examples
ADD R16, R3 ddddd is 10000 and rrrrr is 00011
0000 1101 0000 0011 or 0x0D03
ADD R27, R17 ddddd is 11011, rrrrr is 10001
0000 1111 1011 0001 or 0x0FB1
Note that this instruction always starts with 0, followed by C, D, E, or F
and can have any values in the second byte
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
15/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
15
Expanding Opcodes
The following are not
legal opcodes 1, 11, 111
No opcode longer than
4 bits starts 1110
Except ser Rd
1110 1111 dddd 1111
Similarly, no prefix of
000011 is an opcodeand no opcode longer
than 6 begins with this
sequence
Except lsl Rd
0000 11dd dddd dddd
LDI: 1110011110110011
ADD: 0000111100101101
How does theprocessor"know" where
the opcodeportion stops?
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
16/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
16
A Machine Language Program
Load program into memory At address 0 we place the word $E20C
At address 1 we place the word $E01F At address 2 we place the word $0F01
Execute the three instructions in sequence
Set PC to 0 and perform 3 fetch-execute cycles Observe result
R16 has the sum of $2C and $0F, $3B
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
17/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
17
A Machine Language Program
0000: $E20C LDI R16, $2C
0001: $E01F LDI R17, $0F
0002: $0F01 ADD R16, R17
R16 = R16 + R17
= $2C + $0F
= $3B
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
18/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
18
AVR Studio
An integrated development environment
Provides a text editor
Supports the AVR assembler
Supports the gnu C compiler
Provides an AVR simulator and debugger
Provides programming support for the AVRprocessors via serial interface
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
19/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
19
AVR Studio: New Project
Start AVRStudio
Click New
Project Select type:
Assembler
Choose aproject name
Select createoptions andpick alocation
Location should be a folder to hold allproject folders
Each project should be in its own folder
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
20/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
20
AVR Studio: New Project
On the next dialog,
select the Debug
platform: AVRSimulator
Pick the device type:
ATMega16
Finish
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
21/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
21
AVR Studio: Interface
Enter the program
in the assembly
source file that isopened for you.
Click the
Assemble button
(F7)
Assemble
Workspace
Output
Editor
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
22/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
22
AVR Studio: Assembler Report
Assembler summary indicates success
6 bytes of code, no data, no errors
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
23/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
23
AVR Studio: Debugger
Start the debugging session
Click Start Debugging
Next instruction is shown with yellow arrow
Choose I/O View
View registers 16-17
Step through program
F10 is Step Over
Start
Debugging
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
24/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming
24
AVR Studio: Debugger
The first 2 instructions are completed
R16 and R17 have the expected values from the
LDI instructions
The sum is placed in R16
$3B is the sum
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
25/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming 25
AVR Studio: Memory
Memory contents may be viewed (and
edited) during debugging
You can view program (flash), data (SRAM),or EEPROM memory
You can also view the general purpose and I/O
registers using this tool The Program
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
26/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming 26
Using Mnemonics
Rather than code themachine languageprogram as a sequence
of numeric wordvalues expressed inhexadecimal,assembly language
programmers usuallyuse instructionmnemonics
This program willassemble and runidentically to the first ldi R16, $2C
ldi R17, $0F
add R16, R17
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
27/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming 27
What's Next?
In our sample
program, we executed
three instructions,what comes next?
Undefined! Depends
on what is in flash
How do we terminatea program?
Use a loop!
0000: $E20C LDI R16, $2C
0001: $E01F LDI R17, $0F
0002: $0F01 ADD R16, R17
0003: $???? ???
If a program is to simply
stop, add an instruction
that jumps to its own
address
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
28/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming 28
Relative Jump
RJMP K 1100 kkkk kkkk kkkk
-2048
8/3/2019 Avr Micro Controller Family Assembly Language Programming 337
29/29
4/30/2012 Dr. Tim Margush - AssemblyLanguage Programming 29
Relative Jump
Here: RJMP Here 1100 kkkk kkkk kkkk
K = -1 ($FFF)
1100 1111 1111 1111 or $CFFF0000: $E20C LDI R16, $2C
0001: $E01F LDI R17, $0F
0002: $0F01 ADD R16, R17
0003: $CFFF RJMP -10004: $???? ???
This distance is -1 word
Remember that the program counter is
incremented before the instruction is executed