/* p7_1.c: A to D conversion of channel 5 * This program converts the analog input from channel 5. * Channel 5 is configured as single-ended input from PA05. * External reference A is used from PA03. Clock prescaler * is left at 0 (divided by 4) and Sampling time is set to * 5 clock cycles (20 us). * Software trigger is used. The bit 9 of the conversion * result is used to turn on/off the LED0. For the full * scale of the reference voltage, the LED0 should be on * for 8 times. * * Tested with Atmel Studio 7 v7.0.1006 and Keil MDK-ARM v5.21a. */ #include "samd21.h" unsigned char* ARRAY_PORT_PINCFG0 = (unsigned char*)®_PORT_PINCFG0; unsigned char* ARRAY_PORT_PMUX0 = (unsigned char*)®_PORT_PMUX0; int main (void) { int result; REG_PM_APBCMASK |= 0x10000; /* enable bus clock for ADC */ REG_GCLK_CLKCTRL = 0x401E; /* GCLK0 to ADC */ REG_ADC_SAMPCTRL = 5; /* sampling time 5 clocks */ ARRAY_PORT_PINCFG0[3] |= 1; /* Use PMUX for PA03 */ ARRAY_PORT_PMUX0[1] = 0x10; /* PA03 = VREFA */ REG_ADC_REFCTRL = 3; /* Use VREFA */ REG_ADC_INPUTCTRL = 0x1805; /* V- = GND; V+ = AIN5 */ ARRAY_PORT_PINCFG0[5] |= 1; /* Use PMUX for PA05 */ ARRAY_PORT_PMUX0[2] = 0x10; /* PA05 = AIN5 */ REG_ADC_CTRLA = 2; /* enable ADC */ REG_PORT_DIRSET1 = 0x40000000; /* PB30 output for LED0 */ while (1) { REG_ADC_SWTRIG = 2; /* start a conversion */ while(!(REG_ADC_INTFLAG & 1)); /* wait for conv complete */ result = REG_ADC_RESULT; /* read conversion result */ if (result & 0x100) REG_PORT_OUTCLR1 = 0x40000000; /* turn on LED0 */ else REG_PORT_OUTSET1 = 0x40000000; /* turn off LED0 */ } }