ATtiny13 >> Assembly

Compare Match Output


本範例是,當Timer數到跟OCR0A設定的數值一樣時,觸發Compare Match中斷(CTC模式),在中斷裡面Toggle OC0B腳位(Pin1)


main.s

  .equ DDRB,   0x17
  .equ PORTB,  0x18
  .equ OCR0B,  0x29
  .equ TCCR0A, 0x2f
  .equ TCNT0,  0x32
  .equ TCCR0B, 0x33
  .equ OCR0A,  0x36
  .equ TIMSK0, 0x39
   
  .equ BTN,    0
  .equ LED,    1
   
  .org 0x0000
  rjmp main
  reti
  reti
  reti
  reti
  reti
  reti
  rjmp compb_handler

  .org 0x0020
main:
  sbi DDRB, LED
  cbi DDRB, BTN
  sbi PORTB, LED
  sbi PORTB, BTN
  ldi r16, 0x00
  out TCNT0, r16
  ldi r16, 0x08
  out TIMSK0, r16
  ldi r16, 0x12
  out TCCR0A, r16
  ldi r16, 0x05
  out TCCR0B, r16
  ldi r16, 0xf0
  out OCR0A, r16
  ldi r17, 0
  sei
 
loop:
  rjmp loop

compb_handler:
  sbrs r17, 0
  cbi PORTB, LED
  sbrc r17, 0
  sbi PORTB, LED
  ldi r16, 0x01
  eor r17, r16
  reti

編譯和燒錄

$ avr-as -mmcu=attiny13 -o main.o main.s
$ avr-ld -o main.elf main.o
$ avr-objcopy --output-target=ihex main.elf main.ihex
$ sudo avrdude -c usbasp -p t13 -B 1024 -U flash:w:main.ihex:i

完成


返回上一頁