AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

Similar documents
AVR ISA & AVR Programming (I)

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

By: Dr. Hamed Saghaei

COMP2121: Microprocessors and Interfacing

CN310 Microprocessor Systems Design

COMP2121: Microprocessors and Interfacing

APPENDIX B AVR INSTRUCTIONS EXPLAINED OVERVIEW

COMP3221: Microprocessors and Embedded Systems

8-bit Instruction Set

8-bit Instruction Set

COMP2121: Microprocessors and Interfacing. Instruction Formats and Addressing Modes

AT90S Bit Microcontroller with 1K bytes Downloadable Flash AT90S1200. Features. Description. Pin Configuration

COMP2121: Microprocessors and Interfacing. Instruction Set Architecture (ISA)

ก AVR Microcontrollers

AVR Instruction Set Encoding

AVR MICROCONTROLLER ARCHITECTURTE

Chapter 4: Atmel s AVR 8-bit Microcontroller Part 1 Assembly Programming

Assembly Programming (III) Lecturer: Sri Parameswaran Notes by: Annie Guo Dr. Hui Wu

AVR Control Transfer -AVR Branching

EE 308: Microcontrollers

COMP3221: Microprocessors and Embedded Systems

8-Bit Microcontroller with 1K bytes In-System Programmable Flash AT90S1200. Features. Description. Pin Configuration

Gates and flip-flops: glue logic, simple FSMs, registers Two-level PLDs: FSMs, muxes, decoders. Programmable logic devices (CSE370, CSE467)

Mechatronics and Microcomputers. Stipendium Hungaricum 2018/2019 Autumn Semester Szilárd Aradi, PhD

AVR Microcontrollers Architecture

COMP2121: Microprocessors and Interfacing. AVR Assembly Programming (I) Basic AVR Instructions

Assembly Programming (III)

Logic Instructions and Programs READING

APPENDIX A FOR SKEE3732 LABORATORY 1 SHEET

8-bit Microcontroller with 8K Bytes Programmable Flash AT90C8534. Preliminary

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

ECED3204: Microprocessor Part I--Introduction

Programming. A. Assembly Language Programming. A.1 Machine Code. Machine Code Example: Motorola ADD

ECE 375 Computer Organization and Assembly Language Programming Winter 2018 Solution Set #2

Interrupts (II) Lecturer: Sri Parameswaran Notes by: Annie Guo

8-bit Microcontroller with 2K Bytes of In-System Programmable Flash. ATtiny22 ATtiny22L. Preliminary. Features. Description

8-bit Microcontroller with 2K Bytes of In-System Programmable Flash AT90S2323 AT90LS2323 AT90S2343 AT90S/LS2323. Features.

ATmega128 Assembly Language Programming

8-Bit Microcontroller with 2K Bytes of In-System Programmable Flash. AT90S2323 AT90LS2323 AT90S2343 AT90LS2343 Preliminary AT90S/LS2323.

Building A RISC Microcontroller in an FPGA. Yap Zi He

Lecture 20: AVR Programming, Continued. AVR Program Visible State (ones we care about for now)

EE 308: Microcontrollers

Buses and Parallel Input/Output

AVR Logical and Bitwise Instructions

Microprocessors & Interfacing

Interrupts (I) Lecturer: Sri Notes by Annie Guo. Week8 1

8-Bit Microcontroller with 2K bytes Downloadable Flash. AT90S2313 Preliminary. Features. Description. Pin Configuration

Microprocessors & Interfacing

;Compiler Options.NOLIST.INCLUDE "C:\Program Files (x86)\atmel\avr Tools\AvrAssembler2\Appnotes\m8515def.inc"

Review on Lecture-1. ICT 6641: Advanced Embedded System. Lecture 2 Branch, Call and Delay Loops, AVR I/O port programming

DESIGN NOTE #032. AVR Boot Loader. Introduction. Overview AUTHOR: MARIANO BARRÓN RUIZ KEYWORDS: BOOT LOADER, SPM, SELF-PROGRAMMING

Microcontroller Intel [Instruction Set]

University of Florida EEL 4744 Dr. Eric M. Schwartz. Page 1/11 Revision 0 20-Feb-14 Mixed C and Assembly (for Atmel XMEGA)

AVR Subroutine Basics

Microcontrollers. Microcontroller

8-bit Microcontroller with 2K Bytes of In-System Programmable Flash AT90S2313

Input/Output Devices. Lecturer: Sri Parameswaran Notes by: Annie Guo

EE 308: Microcontrollers

Today s Menu. >Use the Internal Register(s) >Use the Program Memory Space >Use the Stack >Use global memory

Introduction to Assembly language

COMP2121: Microprocessors and Interfacing

AVR Assembler Examples

8-bit Microcontroller with 4K Bytes In-System Programmable Flash. ATtiny40. Preliminary

8051 Overview and Instruction Set

VCC PB2 (SCK/ADC1/T0/PCINT2) PB1 (MISO/AIN1/OC0B/INT0/PCINT1) PB0 (MOSI/AIN0/OC0A/PCINT0)

COMP2121: Microprocessors and Interfacing

Addressing Modes Part II AVR Addressing Indirect READING

!" # Today Class administration, overview of course web, and logistics What is an embedded system? What will we be doing in this class?

Assembly AVR Boot loader

Grundlagen Microcontroller Processor Core. Günther Gridling Bettina Weiss

TYPES OF INTERRUPTS: -

Microprocessors 1. The 8051 Instruction Set. Microprocessors 1 1. Msc. Ivan A. Escobar Broitman

8-bit Microcontroller with 1K Byte of In-System Programmable Flash AT90S1200

MICROPROCESSOR BASED SYSTEM DESIGN

FIFTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLOGY-MARCH 2014 EMBEDDED SYSTEMS (Common for CT,CM) [Time: 3 hours] (Maximum marks : 100)

8-bit Microcontroller with 2K Bytes Flash. ATtiny26 ATtiny26L

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1

COMP3221: Microprocessors and. and Embedded Systems. Instruction Set Architecture (ISA) What makes an ISA? #1: Memory Models. What makes an ISA?

8-bit Microcontroller with 2K Bytes of Flash. ATtiny28L ATtiny28V

8-bit Microcontroller with 2K Bytes of In-System Programmable Flash. ATtiny22L. Preliminary

The Assembly Language of the Boz 5

8-bit Microcontroller with 8K Bytes In-System Programmable Flash AT90S8515

AT90S Bit Microcontroller with 8K bytes Downloadable Flash. AT90S8515 Preliminary. Features. Description. Pin Configurations

8-bit Microcontroller with 2/4/8K Bytes In-System Programmable Flash. ATtiny261/V ATtiny461/V ATtiny861/V. Preliminary

Microcontroller VU

SOME ASSEMBLY REQUIRED

Microprocessors & Interfacing

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

Instruction-set Design Issues: what is the ML instruction format(s) ML instruction Opcode Dest. Operand Source Operand 1...

ECED 3204 Microprocessor Midterm Reference Solution

Accumulator and memory instructions 1. Loads, stores, and transfers 2. Arithmetic operations 3. Multiply and divide 4. Logical operations 5. Data test

Processor Status Register(PSR)

Lab Objectives. 2. Preparations. 3. Signing in. 4. Examining the Host Environment. 5. Part A: Introduction to AVR Studio. 5.

8-bit Microcontroller with 4K Bytes of In-System Programmable Flash AT90S4433 AT90LS4433. Features. Not Recommend for New Designs. Use ATmega8.

3.1 DATA MOVEMENT INSTRUCTIONS 45

EE 308: Microcontrollers

Assembly Language Programming of 8085

Lecture 14. Ali Karimpour Associate Professor Ferdowsi University of Mashhad

IAS0430 MICROPROCESSOR SYSTEMS

Digital Blocks Semiconductor IP

Transcription:

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo 1

Lecture Overview AVR ISA AVR Instructions & Programming (I) Basic construct implementation 2

Atmel AVR 8-bit RISC architecture Most instructions have 16-bit fixed length Most instructions take 1 clock cycle to execute. Load-store memory access architecture All calculations are on registers Two-stage instruction pipelining Internal program memory and data memory Wide variety of on-chip peripherals (digital I/O, ADC, EEPROM, UART, pulse width modulator (PWM) etc). 3

AVR Registers General purpose registers 32 8-bit registers, R0 ~ R31 or r0 ~ r31 Can be further divided into two groups First half group: R0 ~ R15 and second half group: R16 ~ R31 Some instructions work only on the second half group R16~R31 Due to the limitation of instruction encoding bits Will be covered later E.g. ldi rd, #number ;rd R16~R31 4

AVR Registers (cont.) General purpose registers The following register pairs can work as address indexes X, R26:R27 Y, R28:R29 Z, R31:R31 The following registers can be applied for specific use R1:R0 store the result of multiplication instruction R0 stores the data loaded from the program memory 5

AVR Registers (cont.) I/O registers 64 8-bit registers Their names are defined in the m64def.inc file Used in input/output instructions Mainly storing data/addresses and control signal bits Some instructions work only with I/O registers, others with general purpose registers don t confuse them E.g. in rd, port ; port must be an I/O register Will be covered in detail later Status register (SREG) A special I/O register 6

The Status Register in AVR The Status Register (SREG) contains information about the result of the most recently executed arithmetic instruction. This information can be used for altering program flow in order to perform conditional operations. SREG is updated after any of ALU operations by hardware. SREG is not automatically stored when entering an interrupt routine and restored when returning from an interrupt. This must be handled by software. Using in/out instruction to store/restore SREG 7

The Status Register in AVR (cont.) I T H S V N Z C Bit 7 6 5 4 3 2 1 0 Bit 7 I: Global Interrupt Enable Used to enable and disable interrupts. 1: enabled. 0: disabled. The I-bit is cleared by hardware after an interrupt has occurred, and is set by the RETI instruction to enable subsequent interrupts. 8

The Status Register in AVR (cont.) I T H S V N Z C Bit 7 6 5 4 3 2 1 0 Bit 6 T: Bit Copy Storage The Bit Copy instructions BLD (Bit LoaD) and BST (Bit STore) use the T-bit as source or destination for the operated bit. A bit from a register in the Register File can be copied into T by the BST instruction, and a bit in T can be copied into a bit in a register in the Register File by the BLD instruction. 9

The Status Register in AVR (cont.) I T H S V N Z C Bit 7 6 5 4 3 2 1 0 Bit 5 H: Half Carry Flag The Half Carry Flag H indicates a Half Carry (carry from bit 4) in some arithmetic operations. Half Carry is useful in BCD arithmetic. 10

The Status Register in AVR (cont.) I T H S V N Z C Bit 7 6 5 4 3 2 1 0 Bit 4 S: Sign Bit Exclusive OR between the Negative Flag N and the Two s Complement Overflow Flag V ( S = N V). Bit 3 V: Two s Complement Overflow Flag The Two s Complement Overflow Flag V supports two s complement arithmetic. 11

The Status Register in AVR (cont.) I T H S V N Z C Bit 7 6 5 4 3 2 1 0 Bit 2 N: Negative Flag N is the most significant bit of the result. Bit 1 Z: Zero Flag Z indicates a zero result in an arithmetic or logic operation. 1: zero. 0: Non-zero. 12

The Status Register in AVR (cont.) I T H S V N Z C Bit 7 6 5 4 3 2 1 0 Bit 0 C: Carry Flag Its meaning depends on the operation. For addition X+Y, it is the carry from the most significant bit For subtraction x-y, where x and y are unsigned integers, it indicates if x<y. If x<y, C=1; otherwise, C=0. 13

AVR Address Spaces Three address spaces Data memory Storing data to be processed Program memory Storing program and sometimes constants EEPROM memory Large permanent data storage 14

Data Memory Space Covers Register file I.e. registers in the register file also have memory address I/O registers I.e. I/O registers have two versions of addresses I/O addresses Memory addresses SRAM data memory The highest memory location is defined as RAMEND Data Memory 32 General purpose Working Registers 64 Input/Output Registers Internal SRAM (128~4K bytes) External SRAM 8 bits 0x0000 0x1F 0x20 0x5F 0x60 End Address 15

Program Memory Space Covers 16 bit Flash Memory Read only Instructions are retained when power off Can be accessed with special instructions LPM SPM Program Memory Program Flash Memory (1K bytes~128k bytes) 16 Bits 0x0000 End Address 16

EEPROM Memory Space Covers 8-bit EEPROM memory Use to permanently store large set of data Can be accessed using load and store instructions with special control bit settings Not covered in this course Data EEPROM Memory EEPROM Memory (64~4K bytes) 8 bits 0x0000 End address 17

AVR Instruction Format For AVR, almost all instructions are 16 bits long add Rd, Rr sub Rd, Rr mul Rd, Rr brge k Few instructions are 32 bits long lds Rd, k ( 0 k 65535 ) loads 1 byte from the SRAM to a register. 18

Examples (1) -- 16 bits long Clear register instruction Syntax: clr Rd Operand: 0 d 31 Operation: Rd 0 0 0 1 0 0 1 d d d d d d d d d d 15 Instruction format 0 OpCode uses 6 bits (bit 9 to bit 15). The only operand uses the remaining 10 bits (only 5 bits (bit 0 to bit 4) are actually needed). Execution time 1 clock cycle 19

Examples (2) -- 32 bit long Unconditional branch Syntax: jmp k Operand: 0 k < 4M Operation: PC K Instruction format 1 0 0 1 0 1 0 k k k k k 1 1 0 k 15 0 k k k k k k k k k k k k k k k k 31 16 Execution time 3 clock cycles 20

Examples (3) -- with variable cycles Conditional branch Syntax: breq k Operand: -64 k < +63 Operation: If Rd=Rr(Z=1) then PC PC+k+1, else PC PC+1 1 1 1 1 0 0 k k k k k k k 0 0 1 Instruction format Execution time 1 clock cycle if condition is false 2 clock cycles if condition is true 21

AVR Instructions AVR has the following classes of instructions: Arithmetic and Logic Data transfer Program control Bit and Others Bit and Bit test MCU Control An overview of the instructions are given in the next slides. 22

AL Instructions Arithmetic addition E.g. ADD Rd, Rr Subtraction E.g. SUB Rd, Rr Increment/decrement E.g INC Rd Multiplication E.g. MUL Rd, Rr Logic Shift E.g. AND Rd, Rr E.g. LSL Rd 23

Transfer Instructions GP register E.g. MOV Rd, Rr I/O registers Stack E.g. IN Rd, PORTA PUSH Rr POP Rd OUT PORTB, Rr Immediate values E.g. LDI Rd, K8 Memory Data memory E.g. LD Rd, X ST X, Rr Program memory E.g. LPM EEPROM memory Not covered in this course 24

Program Control Instructions Branch Conditional Jump to address BREQ des test ALU flag and jump to specified address if the test was true skips SBIC k test a bit in a register or an IO register and skip the next instruction if the test was true. Unconditional Jump to the specified address RJMP des Call subroutine E.g. RCALL k Return from subroutine E.g. RET 25

Bit & Other Instructions Bit Others Set bit NOP E.g. SBI PORTA, b BREAK Clear bit SLEEP E.g CBI PORTA, b WDR Bit copy E.g. BST Rd, b 26

AVR Instructions (cont.) Not all instructions are implemented in all AVR controllers. Refer to the data sheet of a specific microcontroller Refer to online AVR instruction document for the detail description of each instruction 27

AVR Addressing Modes Immediate Register direct Memory related addressing mode Data memory Direct Indirect Indirect with Displacement Indirect with Pre-decrement Indirect with Post-increment Program memory EPROM memory Not covered in this course 28

Immediate Addressing The operands come from the instructions For example andi r16, $0F Bitwise logic AND operation Clear upper nibble of register r16 29

Register Direct Addressing The operands come from general purpose registers For example and r16, r0 r16 r16 +r0 Clear upper nibble of register r16 30

Register Direct Addressing The operands come from I/O registers For example in r25, PINA -- r25 PIN A 31

Data Memory Addressing 32

Data Direct Addressing The data memory address is given directly from the instruction For example lds r5, $F123 -- r5 Mem($F123), or r5 ($F123) 33

Indirect Addressing The address of memory data is from an address pointer (X, Y, Z) For example ld r11, X -- r11 Mem(X), or r11 (X) 34

Indirect Addressing with displacement The address of memory data is from (Y,Z)+q For example std Y+10, r14 -- (Y+10) r14 35

Indirect Addressing with Predecrement The address of memory data is from an address pointer (X, Y, Z) and the value of the pointer is auto-decreased before each memory access. For example std -Y, r14 -- Y Y-1, (Y) r14 36

Indirect Addressing with Postincrement The address of memory data is from an address pointer (X, Y, Z) and the value of the pointer is autoincreased after each memory access. For example std Y+, r14 -- (Y) r14, Y Y+1 37

Program Memory Addressing 38

Direct Program Addressing The instruction address is from instruction For example jmp k -- (PC) k 39

Relative Program Addressing The instruction address is PC+k+1 For example rjmp k -- (PC) (PC)+k+1 40

Indirect Memory Addressing The instruction address is implicitly stored in Z register icall -- PC(15:0) (Z), PC(21:16) 0 41

Program Memory Constant Addressing The address of the constant is stored in Z register The address is a byte address. For example: lpm -- r0 (Z) 42

Program Memory Addressing with Post-increment For example lpm r16, Z+ -- r16 (Z), Z Z+1 43

AVR Programming Refer to the online AVR Instruction Set documentation for the complete list of AVR instructions http://www.cse.unsw.edu.au/~cs9032/refs/avr- Instruction-Set.pdf The rest of the lecture covers Programming to implement some basic constructs with examples 44

Arithmetic Calculation (1/4) -- example Expressions z = 2x xy 2 x where all data including products from multiplications are 8-bit unsigned numbers; and x, y, z are stored in registers r2, r3, and r4. 45

What instructions do you need? sub mul ldi mov 46

Subtract without Carry Syntax: sub Rd, Rr Operands: Rd, Rr {r0, r1,, r31} Operation: Rd Rd Rr Flags affected: H, S, V, N, Z, C Words: 1 Cycles: 1 47

Multiply Unsigned Syntax: mul Rd, Rr Operands: Rd, Rr {r0, r1,, r31} Operation: r1:r0 Rr*Rd (unsigned unsigned * unsigned ) Flags affected: Z, C C is set if bit 15 of the result is set; cleared otherwise. Words: 1 Cycles: 2 48

Load Immediate Syntax: ldi Rd, k Operands: Rd {r16,, r31}, 0 k 255 Operation: Rd k Flag affected: None Words: 1 Cycles: 1 Encoding: 1110 kkkk dddd kkkk Example: ldi r16, $42 ; Load $42 to r16 49

Copy Register Syntax: mov Rd, Rr Operands: Rd, Rr {r0,r1,,r31} Operation: Rd Rr Flag affected: None Words: 1 Cycles: 1 50

Arithmetic calculation (2/4) AVR code for z = 2x xy where all data including products from multiplications are 8-bit unsigned numbers; and x, y, z are stored in registers r2, r3, and r4. ldi r16, 2 ; r16 2 mul r16, r2 ; r1:r0 2x mov r5, r0 ; r5 2x mul r2, r3 ; r1:r0 xy sub r5, r0 ; r16 < 2x-xy mul r2, r2 ; r1:r0 x 2 sub r5, r0 ; r5 2x-xy- x 2 mov r4, r5 ; r4 z x 2 8 instructions and 11 cycles 51

Arithmetic calculation (3/4) Expressions 2 z = 2x xy x z = x(2 (x + y)) where all data including products from multiplications are 8-bit unsigned numbers; and x, y, z are stored in registers r2, r3, and r4. 52

What instructions do you need? sub mul ldi mov add 53

Add without Carry Syntax: add Rd, Rr Operands: Rd, Rr {r0, r1,, r31} Operation: Rd Rd + Rr Flags affected: H, S, V, N, Z, C Words: 1 Cycles: 1 54

Arithmetic calculation (4/4) AVR code for 2 z = 2x xy x z = x(2 (x + y)) where all data including products from multiplications are 8-bit unsigned numbers; and x, y, z are stored in registers r2, r3, and r4. mov r5, r2 ; r5 x add r5, r3 ; r5 x+y ldi r16, 2 ; r16 2 sub r16, r5 ; r16 < 2-(x+y) mul r2, r16 ; r1:r0 x(2-(x+y)) mov r4, r0 ; r4 z 6 instructions and 7 cycles 55

Control Structure (1/2) -- example IF-THEN-ELSE control structure if(x<0) else z=1; z=-1; Numbers x, z are 8-bit signed integers and stored in registers. You need to decide which registers to use. Instructions interested Compare Conditional branch Unconditional jump 56

Compare Syntax: cp Rd, Rr Operands: Rd {r0, r1,, r31} Operation: Rd Rr (Rd is not changed) Flags affected: H, S, V, N, Z, C Words: 1 Cycles: 1 Example: cp r4, r5 brne noteq... noteq: nop ; Compare r4 with r5 ; Branch if r4 r5 ; Branch destination (do nothing) 57

Compare with Immediate Syntax: cpi Rd, k Operands: Rd {r16, r17,, r31} and 0 k 255 Operation: Rd k (Rd is not changed) Flags affected: H, S, V, N, Z, C Words: 1 Cycles: 1 58

Conditional Branch Syntax: brge k Operands: -64 k < 64 Operation: If Rd Rr (N V=0) then PC PC+k+1, Flag affected: None Words: 1 else PC PC+1 if condition is false Cycles: 1 if condition is false; 2 if condition is true 59

Relative Jump Syntax: rjmp k Operands: -2K k < 2K Operation: PC PC+k+1 Flag affected: None Words: 1 Cycles: 2 60

Control (2/2) IF-THEN-ELSE control structure if(a<0) else b=1; b=-1; Numbers x, z are 8-bit signed integers and stored in registers. You need to decide which registers to use..def.def a=r16 b=r17 cpi a, 0 ;a-0 brge ELSE ;if a 0 ldi b, 1 ;b=1 rjmp END ;end of IF statement ELSE: ldi b, -1 ;b=-1 END: 61

Loop (1/2) WHILE loop i=1; while (i<=n){ sum += i*i; i++; } Numbers i, sum are 8-bit unsigned integers and stored in registers. You need to decide which registers to use. 62

Loop (2/2) WHILE loop.def i = r16.def n = r17.def sum = r18 loop: end: ldi i, 1 clr sum cp n, i brlo end mul i, i add sum, r0 inc i rjmp loop rjmp end ;initialize 63

Homework 1. Refer to the AVR Instruction Set documentation (available at http://www.cse.unsw.edu.au/~comp9032/re fs/avr-instruction-set.pdf). Study the following instructions: Arithmetic and logic instructions add, adc, adiw, sub, subi, sbc, sbci, subiw, mul, muls, mulsu and, andi, or, ori, eor com, neg 64

Homework 1. Study the following instructions (cont.) Branch instructions cp, cpc, cpi rjmp breq, brne brge, brlt brsh, brlo Data transfer instructions mov ldi, ld, st 65

Homework 2. Implement the following functions with AVR assembly language 1) 2-byte addition 2) 2-byte multiplication 3. Inverse a string of ten characters that is stored in the registers r0~r9; and store the inversed string in registers r10~r19 66

Homework 4. Translate the following if-then-else statement, where x is an 8-bit unsigned integer. if(x<0) else z=1; z=255; 67

Reading Material AVR Instruction Set online documentation Instruction Set Nomenclature I/O Registers The Program and Data Addressing Arithmetic instructions, program control instructions 68