Project

General

Profile

Statistics
| Revision:

root / branches / analog / code / projects / template / main.c @ 1454

History | View | Annotate | Download (1.29 KB)

1
#include <dragonfly_lib.h>
2

    
3
int count;
4

    
5
unsigned int testf(int);
6
void setf(int);
7

    
8
int main(void)
9
{
10
        int loops;
11
        unsigned int val;
12
        char i = 0 | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0);
13

    
14
        sei();
15
        
16
        analog_init(ADC_STOP);
17
        usb_init();
18
        range_init();
19
        
20
        usb_puts("STARTING ANALOG CLOCK SCALE TEST\n\r");
21
        usb_puts("ADC10\tACD8\tCount\n\r");
22
        
23
        for(;i >= 0; i--) {
24
                ADCSRA &= ~7;
25
                ADCSRA |= i;
26
                usb_puts("\n\rClock Prescalar: 2^");
27
                usb_puti(i);
28
                usb_puts("\n\r");
29
                
30
                for(loops = 0; loops < 10; loops++) {
31
                        val = testf(WHEEL_PORT);
32
                        usb_puti(val);
33
                        usb_puts("\t");
34
                        usb_puti(val>>2);
35
                        usb_puts("\t");                
36
                        usb_puti(count);
37
                        usb_puts("\n\r");
38
                        delay_ms(500);
39
                }        
40
        }
41
        
42
        usb_puts("done");
43

    
44
        return 0;
45
}
46

    
47
unsigned int testf(int which) {
48
        int adc_h;
49
        int adc_l;
50
        
51
        // Let any previous conversion finish
52
        while (ADCSRA & _BV(ADSC));
53

    
54
        if(which < EXT_MUX) {
55
                ADMUX = ADMUX_OPT + which;
56
        } else {
57
                ADMUX = ADMUX_OPT + EXT_MUX;
58
                setf(which - 8);
59
        }
60
        
61
        // Start the conversion
62
        ADCSRA |= _BV(ADSC);
63
        
64
        count = 0;
65

    
66
        // Wait for the conversion to finish
67
        while (ADCSRA & _BV(ADSC)) {
68
                count++;
69
        }
70
        
71
        adc_l = ADCL;
72
        adc_h = ADCH;
73

    
74
        return ((adc_h << 2) | (adc_l >> 6));
75
}
76

    
77
void setf(int which) {  
78
  // mask so only proper bits are possible.  
79
  PORTG = (PORTG & 0xE3) | ((which & 0x03) << 3) | (which & 0x04);
80
}