LEDのPWM

とりあえずLEDをPWM(パルス幅変調)できるようになったぞ.1周期が約11 msでそれを20回繰り返し.それが終わるとduty ratioを少し下げる.Duty ratioは10/11から1/11まで段階的に下げていく.1/11まで下げたら次はまた10/11に戻る.だから2 sくらいの周期でLEDが次第に暗くなってゆき,また明るくなるようなループに見える.

.include "tn2313def.inc"

.def    a1      =R16
.def    a2      =R17
.def    cnt1    =R18
.def    cnt2    =R19
.def    cnt3    =R20
.def    a3      =R21

        rjmp    Reset           ; Interrupt vector for reset

Reset:
        ldi     a1,0xFF
        out     DDRA,a1         ; Direction of PortA is set to Output

main:
        ldi     a1,10
loopA1:
        ldi     a3,20
loopA3:
        mov     a2,a1
        sbi     PORTA,0         ; 0th bit of PortA (5th pin) is set to High
loopOn:
        rcall   Time            ; wait for 1 ms
        dec     a2              ; if dec a2 returned non-zero value,
        brne    loopOn          ;     then branch to loopOn

        ldi     a2,11
        sub     a2,a1
        cbi     PORTA,0         ; 0th bit of PortA (5th pin) is set to Low
loopOff:
        rcall   Time
        dec     a2
        brne    loopOff

        dec     a3
        brne    loopA3
        dec     a1
        brne    loopA1
        rjmp    main

;;  Subroutine for waiting 1 ms
Time:
        ldi     cnt1,100        ; 1 us (1 MHz) x 10 cycle x 100 = 1 ms
Loop1:
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        dec     cnt1
        brne    Loop1           ; takes 2 cycles for branch
        ret
;; End of subroutine Time       

やっぱCで書きたいな...(笑