ATmega16 Architecture. ○ 16 MHz RISC processor. ○ registers are 8 bit wide.
○ 32 general purpose registers (GPRs). ○ 32 I/O registers. ○ status register ...
AVR Assembler
Microcontroller VL Thomas Nowak TU Wien
1
Overview ●
ATmega16 Architecture
●
Instructions
●
Simple Example Program
●
ATmega16 Control Registers
●
Example: Digital I/O
●
Example: Timer
2
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) 3
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)
4
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 necessary andi r1,temp_reg means r1 := r1 AND 0x02 and r1,temp_reg means r1 := r1 AND r2
.section specifies in which memory the following should be placed ●
.section .text 5
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 0x03 puts value 0x01 to address 0x0004 and 0x03 to 0x0005
6
Instructions ●
Meta instructions 3/3 –
.NOLIST .INCLUDE “def.s” .LIST includes 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 “;”
7
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) 8
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
9
Instructions ●
Jump instructions 1/2 –
rjmp label jumps to label (unconditionally) ●
–
rcall perform calls subroutine “perform”, i.e. pushes the current PC value on the stack (RA) and jumps to label “perform” ●
–
for really long jump distances, jmp would have to be used (rjmp can address the nearest 4096 words relative to the rjmp instruction)
use call for long jump distances
ret returns from subroutine, i.e. pops the RA from the stack into PC
10
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 11 jump distance is at most 64 words in each direction
Instructions ●
Data transfer instructions 1/2 r1 := r2
–
mov 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 12
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
13
Instructions ●
Bit instructions 1/2 shift/roll
–
lsl, lsr, asr, rol, ror
–
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 14
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
15
Simple Example Program ●
●
program simply turns on all LEDs
16
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, 0xFF out DDRC, temp ; activate all LEDs on port C ldi temp, 0xFF out PORTC, temp infinite_loop: rjmp infinite_loop
17
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 18
Example: Digital I/O ●
●
program turns on all LEDs if SW1 is on and turns them off otherwise
19
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, 0xFF out DDRC, temp ldi leds, 0x00 out PORTC, leds 20
Example: Digital I/O ; set port A bit 4 to input and activate pull-up (SW1) cbi DDRA, 4 sbi PORTA, 4 ; main loop. poll PINA bit 4 and activate or deactivate ; the leds accordingly loop_start: in temp, PINA bst temp, 4 brtc switch_on switch_off: ldi leds, 0x00 rjmp loop_end switch_on: ldi leds, 0xFF loop_end: out PORTC, leds rjmp loop_start 21
Example: Timer ●
●
program switches state of LEDs on overflow of Timer1
22
Example: Timer ; timer example program .NOLIST .INCLUDE “Includes/m16def.inc” .LIST ; alias registers .equ temp, 0x10 .equ leds, 0x11 .section .text .org 0x0000 rjmp main ; install timer ISR .org OVF1addr *2 rjmp overflow main: ; set port C to output and initialize with 0 (LEDs) ldi temp, 0xFF out DDRC, temp
23
Example: Timer ldi leds, 0x00 out PORTC, leds ; set global interrupt flag sei ; initialize timer ldi temp, 0x00 out TCCR1A, temp ldi temp, 1