2012年2月28日 星期二
Atmega匯編筆記一
; PORTB 接LED 令LED一顆點亮 來回跑
.include "m16def.inc"
.def count =r18
;***** Code
rjmp RESET ;Reset Handle
RESET:
ldi R16,low(RAMEND); Load low byte address of end of RAM into register R16
out SPL,R16 ; Initialize stack pointer to end of internal RAM
ldi R16,high(RAMEND); Load high byte address of end of RAM into register R16
out SPH, R16 ; Initialize high byte of stack pointer to end of internal RAM
;51 的STACK REGISTER 寬度是8BIT
ser R16 ; 也可用 ldi r16,$ff
out DDRB,R16 ; 設PORTB 全為OUTPUT
main_loop:
ldi R16,$00 ;也可用 clr R16 ;clear r16
sec ;設 c flag
ldi count,$08
loop1:
call delay
rol r16 ;左移
out PORTB,R16
dec count ; 相當51的 DJNZ COUNT,LOOP1 AVR裡,沒這方便的指令,但51的DEC並不會影響FLAG喔
brne loop1 ; Z FLAG不為1(就是COUNT不為零) 分歧到LOOP1
ldi R16,$00
sec
ldi count,$08
loop2:
call delay
ror r16 ;右移
out PORTB,R16
dec count
brne loop2
rjmp main_loop ;Loop forever
delay: ;延時
ldi r19,$01
clr r20
clr r21
delay1:
dec r21
brne delay1
dec r20
brne delay1
dec r19
brne delay1
ret
;--------------------------------------------------------------------------------------------
; 匯編查表
.include "m16def.inc"
;***** Code
rjmp RESET ;Reset Handle
RESET:
ldi R16,low(RAMEND); Load low byte address of end of RAM into register R16
out SPL,R16 ; Initialize stack pointer to end of internal RAM
ldi R16,high(RAMEND); Load high byte address of end of RAM into register R16
out SPH, R16 ; Initialize high byte of stack pointer to end of internal RAM
;51 的STACK REGISTER 寬度是8BIT
main_loop:
ldi zl,low(asc_table*2) ;左移一次 空出 z0(bit) ,atmega 1個位址的flash rom 寬度為2bytes=1word
ldi zh,high(asc_table*2);而z0 =0 會指向low byte ,z0=1 會指向high byte
add zl,r16
lpm r16,z ; r16<--(z) , zh:zl= r31:r30
nop
nop
rjmp main_loop ;Loop forever
asc_table:
.db $30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$41,$42,$43,$44,$45,$46
訂閱:
文章 (Atom)