.nolist
.include "m168def.inc" ; Same family as 328P
.list
;; Interrupt Vector
.org 0x00
rjmp init
.org OC1Aaddr
; Timer/Counter1 Overflow
rjmp Timer_OC1A
.org SPIaddr
rjmp SPI_Interrupt
.org ADCCaddr
rjmp ADC_Interrupt
.org 0x40
;; Syntax
.include "macros.inc"
;; SPI
.include "spi.inc"
;; LCD
.include "led7seg.inc"
;; ADC
.include "adc.inc"
;; Timer
.include "timer.inc"
;; Utils
.include "delay.inc"
.include "div.inc"
Sleep_ADC:
; delay
rcall ADC_Read8
; Sleep for 64 * var timer cycles
ldi r17, 64
mul r16, r17
mov XL, r0
mov XH, r1
rjmp Timer_Sleep
;; Show ADC value
Main_ShowADC:
; load value to r16
mov r16, r10
; rcall ADC_Read8
; display from r16
rcall LED7_ShowHex
; *g*
ldi XL, 0
ldi XH, 16
rcall Timer_Sleep
; continue
rjmp Main_ShowADC
;; 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
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:
; DEBUG
sbi DDRD, PORTD7
sbi PORTD, PORTD7
; 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
; Run
; rcall Main_ShowADC
; rcall Main_Spin
rcall Main_Countup
; rcall Main_Countdown
rcall Main_Blink
end:
rjmp end