AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 –...

25
1 AVR Assembler Microcontroller VL Thomas Nowak TU Wien

Transcript of AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 –...

Page 1: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

1

AVR Assembler

Microcontroller VLThomas Nowak

TU Wien

Page 2: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

2

Overview

● ATmega16 Architecture● Instructions● Simple Example Program● ATmega16 Control Registers● Example: Digital I/O● Example: Timer

Page 3: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

3

ATmega16 Architecture

● 16 MHz RISC processor● registers are 8 bit wide● 32 general purpose registers (GPRs)● 32 I/O registers● status register (SR)

– contains status of last instruction (for conditional jumps)

● program counter (PC)

Page 4: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

4

ATmega16 Architecture

● stack pointer (SP)– used to store return address (RA) during

subroutine calls and interrupts

– stack accessible by commands push and pop

– stack grows towards lower memory regions (i.e. SP is decreased after a push instruction)

● Harvard architecture– separate memory areas for code (.text, 16K

flash memory) and data (.eeprom, 512B EEPROM for constants, and .data, 1K SRAM)

Page 5: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

5

Instructions

● Meta instructions 1/3– .equ defines an alias

● often used to alias registers● .equ temp_reg,0x02 aliases register 2● awareness of the context in which the alias is used

is necessaryandi r1,temp_reg means r1 := r1 AND 0x02and r1,temp_reg means r1 := r1 AND r2

– .section specifies in which memory the following should be placed

● .section .text

Page 6: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

6

Instructions

● Meta instructions 2/3– .byte and .word write given byte/word

(16bit) to current memory address

– .org specifies a memory address● .org 0x0004 sets the current memory address to

0x0004● implicit increment of current memory address on

subsequent instructions● .org 0x0004.byte 0x01.byte 0x03puts value 0x01 to address 0x0004 and 0x03 to 0x0005

Page 7: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

7

Instructions

● Meta instructions 3/3– .NOLIST.INCLUDE “def.s”.LISTincludes file def.s without adding the contents to the list file (useful for definition files)

– label: defines a label at current memory location

– .global exports a label for use in other files

– comment lines start with “;”

Page 8: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

8

Instructions

● Arithmetic/logical instructions 1/2– have the form and r1, r2

i.e. r1 := r1 AND r2– often have a “immediate” variant for use

with constants● andi r1, 0xF0 masks the 4 upper bits of r1

– do not work on all registers● they do not work on I/O registers● see AVR Instruction Set for details (Operands)

Page 9: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

9

Instructions

● Arithmetic/logical instructions 2/2– adc (add with carry) uses the carry bit in the

status word that is updated on every add instruction

– some instruction are also available for processing of words (2 bytes)

● adiw r4:r3,5 adds 5 to the word that has r4 as its high and r3 as its low byte

Page 10: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

10

Instructions

● Jump instructions 1/2– rjmp label jumps to label (unconditionally)

● for really long jump distances, jmp would have to be used (rjmp can address the nearest 4096 words relative to the rjmp instruction)

– rcall perform calls subroutine “perform”, i.e. pushes the current PC value on the stack (RA) and jumps to label “perform”

● use call for long jump distances

– ret returns from subroutine, i.e. pops the RA from the stack into PC

Page 11: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

11

Instructions

● Jump instructions 2/2– reti returns from an interrupt service routine

(ISR). Same as ret, additionally the global interrupt enable flag (IE) is set (it is cleared by default on entry to an ISR)

– breq, brne, ... jump iff certain bits in the SR are set/cleared

● breq label (branch if equal) jumps to label if the zero flag in the SR is set

● use cp r1,r2 (compare) to update the status word without changing any other registers

● jump distance is at most 64 words in each direction

Page 12: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

12

Instructions

● Data transfer instructions 1/2

– mov r1, r2 r1 := r2

– in r3, PINA copies contents of I/O register PINA to GPR r3

● this is often necessary because most operations only work on GPRs

– out PORTA, r6 copies contents of GPR r6 to I/O register PORTA

– push, pop

Page 13: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

13

Instructions

● Data transfer instructions 2/2– st X,r4 stores content of register r4 to

memory location contained in word X (XH:XL)● X, Y and Z are words consisting of two GPRs each● X is r27:r26, Y is r29:r28, Z is r31:r30● postincrement and predecrement are available

– ld r5,X loads contents of memory location contained in word X into register r5

● postincrement and predecrement are available

– direct memory addressing through the commands lds and sts

Page 14: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

14

Instructions

● Bit instructions 1/2– lsl, lsr, asr, rol, ror shift/roll

– sec, clc, sen, cln, sei, ... set/clear individual bits in the SR (e.g. clc clears the carry bit)

– sbi, cbi set/clear bit in I/O register● sbi PORTA,4 sets bit 4 in PORTA

– sbr, cbr set/clear bits in GPR● second operand is a bit mask● sbr r3, 0xF0 sets bits 7..4 in r3

Page 15: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

15

Instructions

● Bit instructions 2/2– bst r1,3 stores bit 3 of r1 to the T-Flag (SR)

– bld r4,5 loads T-Flag into bit 5 of r4

● Misc instructions– nop no operation

– sleep change to sleep mode as specified in the MCUCR register

Page 16: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

16

Simple Example Program

● program simply turns on all LEDs

Page 17: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

17

Simple Example Program; simple example program.NOLIST.INCLUDE “Includes/m16def.inc”.LIST; alias registers.equ temp, 0x10

.section .text

.org 0x0000

; set port C to output (LEDs)ldi temp, 0xFFout DDRC, temp

; activate all LEDs on port Cldi temp, 0xFFout PORTC, temp

infinite_loop:rjmp infinite_loop

Page 18: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

18

ATmega16 Control Registers

● Many features of the microcontroller are configured through special control registers– e.g. to set the timer mode (normal, PWM, ...)

of Timer1, you have to set the right bits (WGM13:0) in TCCR1A and TCCR1B

– these are I/O registers– some share a common I/O location (e.g.

UBRRH/UCSRC)

● Check the manual for details

Page 19: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

19

Example: Digital I/O

● program turns on all LEDs if SW1 is on and turns them off otherwise

Page 20: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

20

Example: Digital I/O

; digital i/o example program.NOLIST.INCLUDE “Includes/m16def.inc”.LIST; alias registers.equ temp, 0x10.equ leds, 0x11

.section .text

.org 0x0000

; set port C to output and initialize with 0 (LEDs)ldi temp, 0xFFout DDRC, temp

ldi leds, 0x00out PORTC, leds

Page 21: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

21

Example: Digital I/O; set port A bit 4 to input and activate pull-up (SW1)cbi DDRA, 4sbi PORTA, 4

; main loop. poll PINA bit 4 and activate or deactivate; the leds accordinglyloop_start:

in temp, PINAbst temp, 4brtc switch_on

switch_off:ldi leds, 0x00rjmp loop_end

switch_on:ldi leds, 0xFF

loop_end:out PORTC, ledsrjmp loop_start

Page 22: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

22

Example: Timer

● program switches state of LEDs on overflow of Timer1

Page 23: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

23

Example: Timer; timer example program.NOLIST.INCLUDE “Includes/m16def.inc”.LIST; alias registers.equ temp, 0x10.equ leds, 0x11

.section .text

.org 0x0000rjmp main

; install timer ISR.org OVF1addr *2rjmp overflow

main:; set port C to output and initialize with 0 (LEDs)ldi temp, 0xFFout DDRC, temp

Page 24: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

24

Example: Timerldi leds, 0x00out PORTC, leds

; set global interrupt flagsei

; initialize timerldi temp, 0x00out TCCR1A, templdi temp, 1<<CS12out TCCR1B, temp

in temp, TIMSKsbr temp, 1<<TOIE1out TIMSK, temp

infinite_loop:rjmp infinite_loop

Page 25: AVR Assembler - Institute of Computer Engineering (E191) · Arithmetic/logical instructions 1/2 – have the form and r1, r2 i.e. r1 := r1 AND r2 – often have a “immediate”

25

Example: Timer

; interrupt service routineoverflow:

cpi leds, 0x00breq was_zero

was_ff:ldi leds, 0x00rjmp ovf_end

was_zero:ldi leds, 0xFF

ovf_end:out PORTC, ledsreti