File: adc.c

package info (click to toggle)
simulavr 1.0.0%2Bgit20160221.e53413b-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 5,748 kB
  • sloc: cpp: 35,491; python: 6,991; ansic: 3,567; makefile: 1,072; sh: 653; asm: 414; tcl: 320
file content (43 lines) | stat: -rw-r--r-- 1,269 bytes parent folder | download | duplicates (2)
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
#include <avr/io.h>
#include <avr/interrupt.h>

volatile uint32_t dummy = 0;
volatile uint32_t conversions = 0;

volatile uint16_t adc_value = 5555;
ISR(ADC_vect)
{
    if((ADCSRA & (1 << ADSC)) == 0)
    {
        ++conversions;
        adc_value = ADC;
        ADCSRA |= /* ADC Control and Status Register A */
            (1 << ADSC); /* ADC Start Conversion */
    }
}

int main(void)
{
    /* I'm not sure if this is necessary. I think, program startup is
     * considered to be handling of the reset interrupt.
     * So Interrupts will be disabled at program startup.
     * just to be on the safe side: */
    cli();

    ADCSRA = /* ADC Control and Status Register A */
          (1 << ADEN) /* ADC Enable */
        | (1 << ADSC) /* ADC Start Conversion */
        | (1 << ADIE) /* ADC Interrupt Enable */
        | (1<< ADPS2) /* ADPS2...ADPS0 ADC Prescaler Select Bits = 1 0 0, on 4MHz,
                         factor 16 = 4µs adc clock, conversion time x13 = 52µs normally */
        ;
    ADMUX = /* ADC Multiplexer Select Register */
        /* REFS1...REFS0 (ReferenceSelection Bits) = 0 --> Externes AREF */        
        (0 << MUX2) | (0 << MUX1) | (0 << MUX0); /* MUX4...MUX0 = channel*/

    sei();
    while(1)
    {
        ++dummy;
    }
}