--- /dev/null
+;
+; thermo - getting temperature using analog comparator
+;
+; author: hackbard@hackdaworld.org
+;
+
+; specify device
+.include "../include/tn2313def.inc"
+
+;
+; interrupts
+;
+
+; RESET
+rjmp INIT
+
+; INT0
+reti
+
+; INT1
+reti
+
+; T1 CAPT1
+rjmp T1_CAPTURE
+
+; T1 COMP A
+reti
+
+; T1 OVF1
+reti
+
+; T0 OVF0
+reti
+
+; UART RX
+reti
+
+; UART UDRE
+reti
+
+; UART TX
+reti
+
+; ANA COMP
+reti
+
+; PCINT
+reti
+
+; T1 COMP B
+reti
+
+; T0 COMP A
+reti
+
+; T0 COMP B
+reti
+
+; USI START
+reti
+
+; USI OVF
+reti
+
+; EE READY
+reti
+
+; WDT OVF
+reti
+
+;
+; init function
+;
+
+INIT:
+
+ ; stackpointer
+ ldi r16,low(RAMEND)
+ out SPL,r16
+
+ ; zero to register 0
+ ldi r17,0
+ mov r0,r17
+
+ ; uart init
+ UART_INIT:
+
+ ldi r16,51 ; 9600 bps @ 8mhz
+ out UBRRL, r16
+ out UBRRH, r0
+
+ ldi r16,(1<<UCSZ0)|(1<<UCSZ1) ; 8 data bits, no parity, 1 stop bit
+ out UCSRC,r16;
+
+ sbi UCSRB,TXEN ; enable tx
+
+ ; timer init
+ TIME_INIT:
+
+ ldi r16,(1<<CS10)|(1<<CS11) ; prescaler 1/64
+ out TCCR1B,r16
+
+ ldi r16,(1<<ICIE1) ; enable input capture interrupt
+ out TIMSK,r16
+
+ ; analog comparator init
+ ANA_COMP_INIT:
+
+ ; interrupt if voltage is higher, enable input capture function ot tc1
+ ldi r16,(1<<ACIS0)|(1<<ACIS0)|(1<<ACIC) ; (no analog comp interrupt!)
+ out ACSR,r16
+
+ ; output for rc element
+ ldi r17,(1<<DDB2)
+ out DDRB,r17
+ out PORTB,r0
+
+ sei ; global interrupt enable
+
+MAIN:
+ rjmp MAIN
+
+;
+; timer 1 input capture interrupt routine
+;
+
+T1_CAPTURE:
+
+ ; disable input capture interrupt
+ in r17,TIMSK
+ cbr r17,ICIE1
+ out TIMSK,r17
+
+ ; pull low the rc element
+ cbi PORTB,PORTB2
+
+ ; read the captured value
+ in r19,ICR1L
+ in r18,ICR1H
+
+ ; uart out
+ LOOP_ONE:
+ sbis UCSRA,UDRE
+ rjmp LOOP_ONE
+ out UDR,r18
+ LOOP_TWO:
+ sbis UCSRA,UDRE
+ rjmp LOOP_TWO
+ out UDR,r19
+
+ ; enable timer 1 overflow interrupt
+ in r17,TIMSK
+ sbr r17,TOIE1
+ out TIMSK,r17
+
+ reti
+
+;
+; timer 1 overflow interrupt function
+;
+
+T1_OVERFLOW:
+
+ ; disable timer 1 overflow interrupt
+ in r17,TOIE1
+ cbr r17,TOIE1
+ out TIMSK,r17
+
+ ; uart space out
+ ldi r17,0x20
+ LOOP_THREE:
+ sbis UCSRA,UDRE
+ rjmp LOOP_TWO
+ out UDR,r17
+
+ ; reset timer counter
+ out TCNT1H,r0
+ out TCNT1L,r0
+
+ ; pull rc element high
+ sbi PORTB,PORTB2
+
+ ; enable input capture interrupt
+ in r17,TIMSK
+ sbr r17,ICIE1
+ out TIMSK,r17
+
+ reti
+