一個avr單片機的匯編程序,注意幾個宏指令
//LED接PB0
//使用定時器1,1024分頻,0.5秒中斷一次,LED閃爍一次
程序如下:
.include "m8def.inc"
.equ led =0
.equ flash =$60
.org$0000//中斷向量表
rest:
rjmp START
INT0addre: ; External Interrupt0 Vector Address
reti
INT1addre: ; External Interrupt1 Vector Address
reti
OC2addre : ; Output Compare2 Interrupt Vector Address
reti
OVF2addre: ; Overflow2 Interrupt Vector Address
reti
ICP1addre: ; Input Capture1 Interrupt Vector Address
reti
OC1Aaddre: ; Output Compare1A Interrupt Vector Address
reti
OC1Baddre: ; Output Compare1B Interrupt Vector Address
reti
OVF1addre: ; Overflow1 Interrupt Vector Address
rjmp Timer1_ovf
OVF0addre: ; Overflow0 Interrupt Vector Address
reti
SPIaddre : ; SPI Interrupt Vector Address
reti
URXCaddre: ; USART Receive Complete Interrupt Vector Address
reti
UDREaddre: ; USART Data Register Empty Interrupt Vector Address
reti
UTXCaddre: ; USART Transmit Complete Interrupt Vector Address
reti
ADCCaddre: ; ADC Interrupt Vector Address
reti
ERDYaddre: ; EEPROM Interrupt Vector Address
reti
ACIaddre : ; Analog Comparator Interrupt Vector Address
reti
TWIaddre : ; Irq.vectoraddress for Two-Wire Interface
reti
SPMaddre : ; SPM complete Interrupt Vector Address
reti
SPMRaddre: ; SPM complete Interrupt Vector Address
reti
.org$20
START:
ldi r16,low(ramend)//設(shè)置堆棧指針
out spl,r16
ldi r16,high(ramend)
out sph,r16
clr r16
sts flash,r16
sbi ddrb,led//PB0接led
rcall Timer1_ovf_init//調(diào)用初始化函數(shù)
MAIN:
lds r16,flash
sbrs r16,0 ;if theflagis set,jump onelow
rjmp main
ldi r16,0x01//LED取反
in r17,pinb
eor r17,r16
out portb,r17
clr r16
sts flash,r16
rjmp main
Timer1_ovf_init:
ldi r16,0xfe
out tcnt1h,r16
ldi r16,0x17
out tcnt1l,r16
in r16,timsk
sbr r16,1<<2//開溢出中斷
out timsk,r16
ldi r16,0x05
out tccr1b,r16//開定時器
sei //開中斷
ret
Timer1_ovf://定時器2定時0.5S
in r16,sreg
push r16//保存SREG入棧
ldi r16,0xfe
out tcnt1h,r16
ldi r16,0x17
out tcnt1l,r16
clr r16
sbrr16,1<<0//設(shè)置標(biāo)志
stsflash,r16
pop r16
out sreg,r16
reti//中斷返回
評論