adc.inc
author Tero Marttila <terom@fixme.fi>
Sat, 08 May 2010 20:07:18 +0300
changeset 21 95549ce0e3da
parent 20 87cd7b25a94d
child 22 fd72c63b8db8
permissions -rw-r--r--
timer: try and actually sleep
;;
;; Basic ADC operation
;;

.equ ADC_DDR    = DDRC
.equ ADC_PORT   = PORTC
.equ ADC_PIN    = PORTC0
        
;; Initialize the ADC for starting conversions
ADC_Init:
    ; Setup ADMUX
        ; Use AVcc as ref
        ; Left-adjust result (for 8-bit access)
        ; Select ADC0
        ldi         r16, (0b01 << REFS0) | (1 << ADLAR) | (0b000 << MUX0)
        sts         ADMUX, r16
    
    ; Setup ADCSRB
        ; Free-running mode
        ldi         r16, (0b000 << ADTS0)
        sts         ADCSRB, r16

    ; Setup ADCSRA
        ; Enable
        ; Start right away...
        ; Auto-trigger
        ; ???: Enable Interrupt
        ; Sample clock scale 1/128
        ldi         r16, (1 << ADEN) | (1 << ADSC) | (1 << ADATE) | (1 << ADIE) | (0b111 << ADPS0)
        sts         ADCSRA, r16

    ; Disable digital circuit for pin
        ldi         r16, (1 << ADC_PIN)
        sts         DIDR0, r16

    ; Debug LED
        sbi         DDRD, PORTD7
        sbi         PORTD, PORTD7

    ; Done
        ret

ADC_Interrupt:
    ; Off
        cbi         PORTD, PORTD7
    
    ; Done
        reti    

;; Read the current 8-bit ADC sample 
; Returns value in r16
ADC_Read8:
    ; Copy
        lds         r16, ADCH

    ; Done
        ret