summaryrefslogtreecommitdiff
path: root/templed.asm
blob: dfe4131f380b29bcca9254f2866736984487694b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
.include "tn85def.inc"
.def tmp0 = r16
.def tmp1 = r17
.def tmp2 = r18
.def tmp3 = r19

; Interrupt vectors
.org 0x0000
interrupts:
	rjmp init
	.org ADCCaddr
	rjmp adc_isr

.org INT_VECTORS_SIZE
init:
	; Initialize stack pointer
	ldi tmp0, HIGH(RAMEND)
	out SPH, tmp0
	ldi tmp0, LOW(RAMEND)
	out SPL, tmp0
	; Set pin directions
	ldi tmp0, 0b00010010
	out DDRB, tmp0
	; Set default outputs
	;ldi tmp0, 0b00000010
	;out PORTB, tmp0
	; Initialize timer 0B
	ldi tmp0, 0b00100011
	out TCCR0A, tmp0
	ldi tmp0, 0b00000001
	out TCCR0B, tmp0
	; Initialize timer 1B
	ldi tmp0, 0b00000001
	out TCCR1, tmp0
	ldi tmp0, 0b01100000
	out GTCCR, tmp0
	; Enable interrupts
	ldi tmp0, 0b10000000
	out SREG, tmp0
	; Initialize ADC
	ldi tmp0, 0b01000001
	out ADMUX, tmp0
	ldi tmp0, 0b10101000
	out ADCSRA, tmp0
	; Start ADC conversion
	sbi ADCSRA, ADSC

main:
	rjmp main

adc_isr:
	push tmp0
	push tmp1
	; Read out ADC
	in tmp0, ADCL
	in tmp1, ADCH
	; Substract offset
	subi tmp0, LOW(638)
	sbci tmp1, HIGH(638)
	; Normalize to 8 bit for timer
	lsr tmp1
	ror tmp0
	lsr tmp1
	ror tmp0
	; Set as timer compare
	out OCR0B, tmp0
	com tmp0
	out OCR1B, tmp0
	pop tmp1
	pop tmp0
	reti