led7seg.s
author Tero Marttila <terom@paivola.fi>
Sun, 20 Apr 2014 23:51:57 +0300
changeset 80 5254ba612630
parent 32 7ceb76b5a104
permissions -rw-r--r--
dmx-web: slightly better RGB colorpicker control..
.nolist
.include "m168def.inc"      ; Same family as 328P
.list


;; Interrupt Vector
.org 0x00
        rjmp        init

.org OC1Aaddr
		; Timer/Counter1 Compare Output A
		rjmp		Timer_OC1A

.org OC0Aaddr
        ; Timer/Counter0 Compare Output A
        rjmp        Timer_OC0A

.org SPIaddr
        rjmp        SPI_Interrupt

.org ADCCaddr
        rjmp        ADC_Interrupt

.org 0x40

;; Syntax
.include "macros.inc"

;; SPI
.include "spi.inc"

;; LCD
.include "led7seg.inc"

;; DIPs
.include "dip.inc"

;; ADC

; ADC Interrupt handler
On_ADC:
    ; DEBUG
		sbi			PIND, PORTD7

    ; Check timer, from r16
        rcall       ADC_Read8

        ldi         r17, 64
        mul         r16, r17
        mov         XL, r0
        mov         XH, r1 

        rjmp       Timer_Update

.set ADC_Handler = On_ADC

.include "adc.inc"

;; Timer
.set TIMER0_CB_A = SPI_Update

.include "timer.inc"

;; Utils
.include "delay.inc"
.include "div.inc"

Sleep_ADC:
        ; delay
        rcall       ADC_Read8 
        ; rcall       DIP_Read8
        
        ; Sleep for 64 * var timer cycles
        ldi         r17, 64
        mul         r16, r17
        mov         XL, r0
        mov         XH, r1 

        rjmp       Timer_Sleep

;; Show value
Main_ShowValue:
        ; load value to r16
        ; rcall       DIP_Read8
        rcall       ADC_Read8
        ; rcall       Timer0_Read8

        ; display from r16
        rcall       LED7_ShowHex
        
        ; wait
        rcall       Sleep_ADC

        ; ldi         XL, 0
        ; ldi         XH, 16
        ; rcall       Timer_Sleep
        
        ; rcall       ADC_Read8
        ; mov         r20, r16
        ; rcall       VarDelay

        ; continue
        rjmp        Main_ShowValue

;; Count down from F
; Returns once we've hit zero
Main_Countdown:
        ; init from F
        ldi         r24, LED7_F

_count_loop:
        ; display
        mov         r16, r24
        mov         r16, r24
        rcall       LED7_Show

        ; exit if zero
        tst         r24
        breq        _count_end

        ; count down
        dec         r24

        ; wait...
        rcall       Sleep_ADC

        ; next
        rjmp        _count_loop

_count_end:
        ; done
        ret

;; Count up from 00 -> 255
; Returns once done
Main_CountUp:
        ; init from 0
        ldi         r24, 0

_countup_loop:
        ; display
        mov         r16, r24
        rcall       LED7_ShowDec

        ; wait...
        rcall       Sleep_ADC

        ; exit if zero
        cpi         r24, 255
        breq        _countup_end

        ; count up
        inc         r24

        ; continue
        rjmp        _countup_loop

_countup_end:
        ; done
        ret

;; Blink between dot and empty
Main_Blink:
_blink_loop:
        ; dot
        ldi         r16, LED7_DOT
        ldi         r17, LED7_EMPTY
        rcall       LED7_Show

        ; wait...
        rcall       Sleep_ADC
        
        ; empty
        ldi         r16, LED7_EMPTY
        ldi         r17, LED7_DOT
        rcall       LED7_Show
        
        ; wait... 
        rcall       Sleep_ADC
        
        ; loop
        rjmp        _blink_loop
 
;; Chase segments
Main_Spin:
_spin_init:
        ; init from top
        ldi         r24, 0b00000001 

_spin_next:
        ; display
        mov         r16, r24
        mov         r17, r24
        com         r17
        andi        r17, 0b00111111
        rcall       LED7_ShowRaw

        ; delay
        rcall       Sleep_ADC
        
        ; next segment
        lsl         r24
        
        ; go back to A if we hit G
        sbrc        r24, 6
        rjmp        _spin_init

        rjmp        _spin_next

Main:
init:
    ; Stack
        ldi         r16, high(RAMEND)
        ldi         r17, low(RAMEND)
        out         SPH, r16
        out         SPL, r17

    ; Enable interrupts
        sei

    ; ADC (slowest to start up)
        rcall       ADC_Init
    
    ; Timer
        rcall       Timer_Init

    ; SPI
        rcall       SPI_Init
    
    ; LCD (requires interrupts, blocks)
        rcall       LED7_Init    
    
    ; DEBUG
        sbi         DDRD, PORTD7
        cbi         PORTD, PORTD7

    ; Run
        rcall       Main_Countup
        ; rcall       Main_Countdown
        ; rcall       Main_ShowValue
        ; rcall       Main_Spin
        rcall       Main_Blink

end:
        rjmp        end