Revision 1454 branches/analog/code/projects/template/main.c

View differences:

main.c
1 1
#include <dragonfly_lib.h>
2 2

  
3
int count;
4

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

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

  
6
	/* initialize components, set wireless channel */
7
	dragonfly_init(ALL_ON);
14
	sei();
8 15
	
9
	int val;
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");
10 43

  
11
	while (1) {
44
	return 0;
45
}
12 46

  
13
		usb_puts("Val: ");
14
		bom_refresh(BOM_ALL);
15
		usb_puti(bom_get_max());
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));
16 53

  
17
		usb_putc('\n');
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;
18 65

  
19
		delay_ms(200);
20

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

  
23
	return 0;
74
	return ((adc_h << 2) | (adc_l >> 6));
24 75
}
25 76

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

Also available in: Unified diff