;Breathing LED 07 Delta-Sigma Segmented Exp
;and Sound Input

    LIST	P=PIC12CE518
    LIST	R=DECIMAL
    include	"p12ce518.inc"

    __config _MCLRE_OFF&_CP_OFF&_WDT_OFF&_ExtRC_OSC

k_FF    equ 0x10
pwm_saw equ 0x11
lfo_inc equ 0x12
lfo_saw equ 0x13
lfo_tri equ 0x14
lfo_cnt equ 0x15
lfo_ofs equ 0x16
sigma   equ 0x17
mod_out equ 0x18
tmp     equ 0x19
acc_flg equ 0x1A

    org 0
start
    nop
    movlw   0xFF
    movwf   k_FF
    movwf   GPIO
    movlw   0xFE     ; GP0=out
    tris    6

    movlw   0x00
    movwf   pwm_saw
    movwf   lfo_saw
    movwf   lfo_cnt
    movwf   acc_flg
    movlw   0xC0
    movwf   lfo_tri
    movwf   sigma
    movwf   mod_out
    movlw   0x0F
    movwf   lfo_inc
    movlw   0x0A
    movwf   lfo_ofs
loop
delta_sigma
    movf    mod_out,w
    subwf   lfo_tri,w
    addwf   sigma,f
    movf    sigma,w
    andlw   0x80
     bnz    led_off
led_on
    bcf     GPIO,0
    movlw   0x40
    goto    led_end
led_off
    bsf     GPIO,0
    movlw   0xC0
led_end
    movwf   mod_out

    movlw   8
    addwf   pwm_saw,w
    andlw   0x7F
    movwf   pwm_saw
     bnz    loop

    btfss   GPIO,2
     bsf    acc_flg,0

    movlw   4
    addwf   lfo_cnt,f
     bnz    loop

lfo
    clrc
    rrf     lfo_inc,w
    movwf   tmp
    clrc
    rrf     tmp,w
    addwf   lfo_ofs,w
    addwf   lfo_saw,f
     bnc    tri
rate
    btfss   acc_flg,0
     goto   decel
accel
    bcf     acc_flg,0
    movwf   lfo_inc
    subwf   k_FF,w
    movwf   tmp
    clrc
    rrf     tmp,f
    clrc
    rrf     tmp,w
    addwf   lfo_inc,f
    goto    tri
decel
    clrc
    rrf     lfo_inc,w
    movwf   tmp
    clrc
    rrf     tmp,f
    clrc
    rrf     tmp,f
    clrc
    rrf     tmp,w
    subwf   lfo_inc,f

tri
    movf    lfo_saw,w
    movwf   lfo_tri
    andlw   0x80
    skpz
    comf    lfo_tri,f

;segmented exp
    movlw   0x40
    subwf   lfo_tri,w
     bc     segexp_40_7F
segexp_00_3F
    movlw   0x20
    subwf   lfo_tri,w
     bc     segexp_20_3F
segexp_00_1F
    clrc
    rrf     lfo_tri,f
    clrc
    rrf     lfo_tri,f
    movlw   0x08
    addwf   lfo_tri,f
    goto    segexp_end
segexp_20_3F
    clrc
    rrf     lfo_tri,f
    goto    segexp_end
segexp_40_7F
    movlw   0x60
    subwf   lfo_tri,w
     bc     segexp_60_7F
segexp_40_5F
    movlw   0x20
    subwf   lfo_tri,f
    goto    segexp_end
segexp_60_7F
    movlw   0x60
    subwf   lfo_tri,f
    rlf     lfo_tri,f
    movlw   0x40
    addwf   lfo_tri,f
segexp_end
    movlw   0x47
    subwf   lfo_tri,f
    goto    loop

    END
