Академический Документы
Профессиональный Документы
Культура Документы
Overview
ATmega16 Architecture nstructions !im"le #$am"le %rogram ATmega16 &ontrol Registers #$am"le' (igital )O #$am"le' Timer
ATmega16 Architecture
16 M*+ R !& "rocessor registers are , bit wi-e ./ general "ur"ose registers 01%Rs2 ./ )O registers status register 0!R2
ATmega16 Architecture
use- to store return a--ress 0RA2 -uring subroutine calls an- interru"ts stack accessible b5 comman-s push an- pop stack grows towar-s lower memor5 regions 0i6e6 !% is -ecrease- a3ter a push instruction2 se"arate memor5 areas 3or co-e 0.text7 168 3lash memor52 an- -ata 0.eeprom7 91/: ##%ROM 3or constants7 an- .data7 18 !RAM2
*arvar- architecture
nstructions
o3ten use- to alias registers .equ temp_reg,0x02 aliases register / awareness o3 the conte$t in which the alias is useis necessar5 andi r1,temp_reg means r1 '; r1 AN( <$</ and r1,temp_reg means r1 '; r1 AN( r/
.section .text
5
nstructions
.byte an- .word write given b5te)wor016bit2 to current memor5 a--ress .org s"eci3ies a memor5 a--ress
.org 0x0004 sets the current memor5 a--ress to <$<<<= im"licit increment o3 current memor5 a--ress on subse>uent instructions .org 0x0004 .byte 0x01 .byte 0x03 "uts value <$<1 to a--ress <$<<<= an- <$<. to <$<<<9
nstructions
.NOLIST .INCLUDE def.s .LIST inclu-es 3ile -e36s without a--ing the contents to the list 3ile 0use3ul 3or -e3inition 3iles2 label: -e3ines a label at current memor5 location .global e$"orts a label 3or use in other 3iles comment lines start with ?@A
7
nstructions
have the 3orm and r1, r2 i6e6 r1 '; r1 AN( r/ o3ten have a ?imme-iateA variant 3or use with constants
andi r1, 0xF0 masks the = u""er bits o3 r1 the5 -o not work on )O registers see AVR nstruction !et 3or -etails 0O"eran-s2
8
nstructions
adc 0a-- with carr52 uses the carr5 bit in the status wor- that is u"-ate- on ever5 a-instruction some instruction are also available 3or "rocessing o3 wor-s 0/ b5tes2
adiw r4:r3,5 a--s 9 to the wor- that has r= as its high an- r. as its low b5te
nstructions
3or really long 4um" -istances7 jmp woul- have to be use- 0rjmp can a--ress the nearest =<C6 wor-s relative to the rjmp instruction2
rcall perform calls subroutine ?"er3ormA7 i6e6 "ushes the current %& value on the stack 0RA2 an- 4um"s to label ?"er3ormA
ret returns 3rom subroutine7 i6e6 "o"s the RA 3rom the stack into %&
10
nstructions
reti returns 3rom an interru"t service routine 0 !R26 !ame as ret7 a--itionall5 the global interru"t enable 3lag 0 #2 is set 0it is cleareb5 -e3ault on entr5 to an !R2 breq7 brne7 666 4um" i33 certain bits in the !R are set)cleare
breq label 0branch i3 e>ual2 4um"s to label i3 the +ero 3lag in the !R is set use cp r1,r2 0com"are2 to u"-ate the status wor- without changing an5 other registers
11 4um" -istance is at most 6= wor-s in each -irection
nstructions
mov r1, r2
r1 '; r/
nstructions
D7 E an- F are wor-s consisting o3 two 1%Rs each D is r/G'r/67 E is r/C'r/,7 F is r.1'r.< "ostincrement an- "re-ecrement are available
nstructions
shi3t)roll
sec7 clc7 sen7 cln7 sei7 666 set)clear in-ivi-ual bits in the !R 0e6g6 clc clears the carr5 bit2 sbi7 cbi set)clear bit in )O register
sbi PORTA,4 sets bit = in %ORTA secon- o"eran- is a bit mask sbr r3, 0xF0 sets bits G66= in r.
14
nstructions
bst r1,3 stores bit . o3 r1 to the THIlag 0!R2 bld r4,5 loa-s THIlag into bit 9 o3 r= nop no o"eration sleep change to slee" mo-e as s"eci3ie- in the M&U&R register
Misc instructions
15
16
Man5 3eatures o3 the microcontroller are con3igure- through s"ecial control registers
e6g6 to set the timer mo-e 0normal7 %WM7 6662 o3 Timer17 5ou have to set the right bits 0W1M1.'<2 in T&&R1A an- T&&R1: these are )O registers some share a common )O location 0e6g6 U:RR*)U&!R&2
18
#$am"le' (igital )O
"rogram turns on all L#(s i3 !W1 is on an- turns them o33 otherwise
19
#$am"le' (igital )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
#$am"le' (igital )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
#$am"le' Timer
22
#$am"le' 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
#$am"le' Timer
ldi leds, 0x00 out PORTC, leds ; set global interrupt flag sei ; initialize timer ldi temp, 0x00 out TCCR1A, temp ldi temp, 1<<CS12 out TCCR1B, temp in temp, TIMSK sbr temp, 1<<TOIE1 out TIMSK, temp infinite_loop: rjmp infinite_loop
24
#$am"le' Timer
; interrupt service routine overflow: cpi leds, 0x00 breq was_zero was_ff: ldi leds, 0x00 rjmp ovf_end was_zero: ldi leds, 0xFF ovf_end: out PORTC, leds reti
25