1 | // The board.h header file defines which IO ports pheripherals like
|
2 | // Switches and LEDs are connected to.
|
3 | #include "../board.h"
|
4 |
|
5 | // Setup for different compilers
|
6 | #include "../avr_compiler.h"
|
7 |
|
8 | // ADC Drivers
|
9 | #include "../adc_driver.h"
|
10 |
|
11 | // Defines what you want to connect to the ADC.
|
12 | // Light sensor (XMEGA-A1-Xplained)
|
13 | #define ADC_INPUT ADC_CH_MUXPOS_PIN3_gc
|
14 | //#define ADC_INPUT
|
15 |
|
16 | //NTC
|
17 | //#define ADC_INPUT ADC_CH_MUXPOS_PIN0_gc
|
18 |
|
19 | volatile uint16_t ADC_result;
|
20 |
|
21 | int main( void )
|
22 | {
|
23 | // Variable for use when we read the result from an ADC channel
|
24 | int8_t offset;
|
25 |
|
26 | LEDPORT.DIR = 0xFF; //Set as ouput, the 8 MSB of the result is output here
|
27 | LEDPORT.OUT = 0xFF; //Default off for LED
|
28 |
|
29 | // ntc_enable(); //Pulls a pin low to enable the NTC.
|
30 |
|
31 | /* Move stored calibration values to ADC B */
|
32 | ADC_CalibrationValues_Load(&ADCA);
|
33 |
|
34 |
|
35 |
|
36 | /* Set up ADC B to have signed conversion mode and 12 bit resolution. */
|
37 | ADC_ConvMode_and_Resolution_Config(&ADCA, ADC_ConvMode_Signed, ADC_RESOLUTION_8BIT_gc);
|
38 |
|
39 | // The ADC has different voltage reference options, controlled by the REFSEL bits in the
|
40 | // REFCTRL register. Here the internal reference is selected
|
41 | ADC_Reference_Config(&ADCA,ADC_REFSEL_INT1V_gc);
|
42 |
|
43 | // The clock into the ADC decide the maximum sample rate and the conversion time, and
|
44 | // this is controlled by the PRESCALER bits in the PRESCALER register. Here, the
|
45 | // Peripheral Clock is divided by 8 ( gives 250 KSPS with 2Mhz clock )
|
46 | ADC_Prescaler_Config(&ADCA, ADC_PRESCALER_DIV8_gc);
|
47 |
|
48 | // The used Virtual Channel (CH0) must be set in the correct mode
|
49 | // In this task we will use single ended input, so this mode is selected
|
50 |
|
51 | /* Setup channel 0 to have single ended input. */
|
52 | ADC_Ch_InputMode_and_Gain_Config(&ADCA.CH0,
|
53 | ADC_CH_INPUTMODE_SINGLEENDED_gc,
|
54 | ADC_CH_GAIN_1X_gc);
|
55 |
|
56 | //ADCA.CH0.MUXCTRL = ADC_CH_MUXINT_SCALEDVCC_gc;
|
57 | // Setting up the which pins to convert.
|
58 | // Note that the negative pin is internally connected to ground
|
59 | ADC_Ch_InputMux_Config(&ADCA.CH0, ADC_INPUT, ADC_CH_MUXNEG_PIN0_gc);
|
60 |
|
61 | // Before the ADC can be used it must be enabled
|
62 | ADC_Enable(&ADCA);
|
63 |
|
64 | // Wait until the ADC is ready
|
65 | ADC_Wait_8MHz(&ADCA);
|
66 |
|
67 | // In the while(1) loop, a conversion is started on CH0 and the 8 MSB of the result is
|
68 | // ouput on the LEDPORT when the conversion is done
|
69 |
|
70 | /* Get offset value for ADC B. */
|
71 | offset = ADC_Offset_Get_Signed(&ADCA, &(ADCA.CH0), true);
|
72 |
|
73 | //Read the ADC forever...
|
74 | while (1) {
|
75 | // Start a single conversion
|
76 | ADC_Ch_Conversion_Start(&ADCA.CH0);
|
77 |
|
78 | // Wait for the conversion to complete
|
79 | while(!ADC_Ch_Conversion_Complete(&ADCA.CH0));
|
80 |
|
81 | // Get the result
|
82 | ADC_result = ADC_ResultCh_GetWord_Signed(&ADCA.CH0, offset);
|
83 |
|
84 | // The result is a 12 bit value, 4 rightshifts maps MSB to LED7
|
85 | // invert as the LED is on when pin is low
|
86 | LEDPORT.OUT = ~(ADC_result >> 4);
|
87 | }
|
88 | }
|