Exemple #1
0
/**
 * \brief Initialize ADC and DAC used to simulate a temperature sensor
 *
 * DACB is used by the simulation plant to output a temperature reading of the
 * oven plate. It is set up to output a voltage on pin B2 which is marked as
 * ADC2 on header J2.
 *
 * ADCA is used in the control step and graphical interface to show the current
 * temperature of the oven plate. It is set up to read a voltage on pin A4 which
 * is marked as ADC4 on header J2.
 *
 * ADC2 and ADC4 should be connected together, so that the ADC samples the DAC
 * directly.
 */
void main_init_adc_dac(void)
{
    struct adc_config adc_conf;
    struct adc_channel_config adcch_conf;
    struct dac_config dac_conf;

    /* Set up the DAC for the simulation to output "real" temperature */
    dac_read_configuration(&DACB, &dac_conf);
    dac_set_conversion_parameters(&dac_conf, DAC_REF_BANDGAP,
                                  DAC_ADJ_RIGHT);
    dac_set_active_channel(&dac_conf, DAC_CH0, 0);
    dac_write_configuration(&DACB, &dac_conf);
    dac_enable(&DACB);

    /* Set up the ADC for the controller to read "real" temperature */
    adc_read_configuration(&ADCA, &adc_conf);
    adcch_read_configuration(&ADCA, ADC_CH0, &adcch_conf);
    adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_12,
                                  ADC_REF_BANDGAP);
    adc_set_clock_rate(&adc_conf, 20000UL);
    adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);
    adc_write_configuration(&ADCA, &adc_conf);
    adcch_set_input(&adcch_conf, ADCCH_POS_PIN4, ADCCH_NEG_NONE, 1);
    adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf);
    adc_enable(&ADCA);
    adc_start_conversion(&ADCA, ADC_CH0);

    /* Enable pull-down, so an open circuit can be detected */
    ioport_set_pin_dir(J2_PIN4, IOPORT_DIR_INPUT);
    ioport_set_pin_mode(J2_PIN4, IOPORT_MODE_PULLDOWN);
}
Exemple #2
0
/**
 * \brief Initialize ADC
 *
 * Here the averaging feature is disabled.
 */
static void main_adc_init(void)
{
	/* ADC module configuration structure */
	struct adc_config adc_conf;
	/* ADC channel configuration structure */
	struct adc_channel_config adcch_conf;

	/* Configure the ADC module:
	 * - unsigned, more than 12-bit results
	 * - VCC /2 voltage reference
	 * - 200 kHz maximum clock rate
	 * - freerun conversion triggering
	 * - enabled internal temperature sensor
	 */
	adc_read_configuration(&ADCA, &adc_conf);
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_OFF, ADC_RES_12,
			ADC_REF_VCCDIV2);
	adc_set_clock_rate(&adc_conf, 200000UL);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_FREERUN, 1, 0);
	adc_enable_internal_input(&adc_conf, ADC_INT_TEMPSENSE);
	adc_write_configuration(&ADCA, &adc_conf);

	/* Configure ADC channel 0:
	 * - single-ended measurement from temperature sensor
	 * - interrupt flag set on completed conversion
	 */
	adcch_read_configuration(&ADCA, ADC_CH0, &adcch_conf);
	adcch_set_input(&adcch_conf, ADCCH_POS_TEMPSENSE, ADCCH_NEG_NONE, 1);
	adcch_set_interrupt_mode(&adcch_conf, ADCCH_MODE_COMPLETE);
	adcch_disable_interrupt(&adcch_conf);
	adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf);

	/* Enable ADC which starts the freerun conversion.*/
	adc_enable(&ADCA);
}
Exemple #3
0
int adc_init(void)
{
	/* Offset Measurement*/
	/*
	struct adc_config adc_conf;
	struct adc_channel_config adcch_conf;
	adc_read_configuration(&MY_ADC, &adc_conf);
	adcch_read_configuration(&MY_ADC, MY_ADC_CH, &adcch_conf);
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_12, ADC_REF_AREFA);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);
	adc_set_clock_rate(&adc_conf, 62500UL);
	adcch_set_input(&adcch_conf, ADCCH_POS_PIN4, ADCCH_NEG_PIN4, 1);
	adc_write_configuration(&MY_ADC, &adc_conf);
	adcch_write_configuration(&MY_ADC, MY_ADC_CH, &adcch_conf);
	adc_enable(&MY_ADC);
	*/
	
	/* Normal Measurement*/
	struct adc_config adc_conf;
	struct adc_channel_config adcch_conf;
	adc_read_configuration(&MY_ADC, &adc_conf);
	adcch_read_configuration(&MY_ADC, MY_ADC_CH, &adcch_conf);
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_OFF, ADC_RES_12, ADC_REF_AREFA);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);
	adc_set_clock_rate(&adc_conf, 62500UL);
	adcch_set_input(&adcch_conf, ADCCH_POS_PIN4, ADCCH_NEG_NONE, 1); //PAD_GND
	adc_write_configuration(&MY_ADC, &adc_conf);
	adcch_write_configuration(&MY_ADC, MY_ADC_CH, &adcch_conf);
	adc_enable(&MY_ADC);
	
	return 0;
}
Exemple #4
0
/**
 * \brief Callback function for ADC interrupts
 *
 * \param adc Pointer to ADC module.
 * \param channel ADC channel number.
 * \param result Conversion result from ADC channel.
 */
static void adc_handler(ADC_t *adc, uint8_t channel, adc_result_t result)
{
	switch (adc_conv[adc_mux_index].in) {
		case EXT_VIN_ADC_INPUT:
			ext_voltage=result;
			adc_mux_index++;

			//start new measurement
			adc_disable(&EXT_VIN_ADC_MODULE);
			adc_set_conversion_parameters(&adc_conf
					,ADC_SIGN_OFF
					,ADC_RES_12
					,adc_conv[adc_mux_index].ref);
			adc_write_configuration(
					&POTENTIOMETER_ADC_MODULE
					, &adc_conf);
			adc_enable(&POTENTIOMETER_ADC_MODULE);
			adcch_set_input(&adcch_conf
					,adc_conv[adc_mux_index].in
					,ADCCH_NEG_NONE,1);
			adcch_write_configuration(
					&POTENTIOMETER_ADC_MODULE
					, ADC_CH0, &adcch_conf);
			adc_start_conversion(&POTENTIOMETER_ADC_MODULE
					, ADC_CH0);
			break;
		case POTENTIOMETER_ADC_INPUT:
			potentiometer=result;
		break;

		default:
		break;
	}
}
Exemple #5
0
/**
 * \brief Configure the ADC for DAC calibration
 */
static void configure_adc(void)
{
	struct adc_config adc_conf;
	struct adc_channel_config adcch_conf;

	adc_read_configuration(&CALIBRATION_ADC, &adc_conf);
	adcch_read_configuration(&CALIBRATION_ADC, ADC_CH_TO_DAC_CH0, &adcch_conf);

	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_12, ADC_REF_VCC);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 2, 0);
	adc_set_clock_rate(&adc_conf, 62500UL);

	adcch_set_input(&adcch_conf, ADCCH_POS_PIN1, ADCCH_NEG_PIN1, 1);

	adc_write_configuration(&CALIBRATION_ADC, &adc_conf);
	adcch_write_configuration(&CALIBRATION_ADC, ADC_CH_TO_DAC_CH0, &adcch_conf);

	/* Find the offset of the ADC */
	adc_enable(&CALIBRATION_ADC);
	adc_offset = adc_get_sample_avg(ADC_CH_TO_DAC_CH0, 128);
	adc_disable(&CALIBRATION_ADC);

	/* Switch input to the DAC output pin PB3, i.e. ADC pin 11 */
	adcch_set_input(&adcch_conf, ADCCH_POS_PIN10, ADCCH_NEG_PAD_GND, 1);
	adcch_write_configuration(&CALIBRATION_ADC, ADC_CH_TO_DAC_CH0, &adcch_conf);

	adcch_set_input(&adcch_conf, ADCCH_POS_PIN11, ADCCH_NEG_PAD_GND, 1);
	adcch_write_configuration(&CALIBRATION_ADC, ADC_CH_TO_DAC_CH1, &adcch_conf);

	adc_enable(&CALIBRATION_ADC);

}
Exemple #6
0
void adcInit()
{
	struct adc_config adc_conf;
	struct adc_channel_config adc_ch_conf;
	
	/* Clear the ADC configuration structs */
	adc_read_configuration(&ADCA, &adc_conf);
	adcch_read_configuration(&ADCA, ADC_CH0, &adc_ch_conf);
	
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_12,
	ADC_REF_VCC);
	adc_set_clock_rate(&adc_conf, 100000UL);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 0, 0);
	adc_write_configuration(&ADCA, &adc_conf);
	//adc_set_callback(&ADCA, &adc_handler);
	
	adcch_set_input(&adc_ch_conf, ADCCH_POS_PIN0, ADCCH_NEG_PIN5, 1);
	//adcch_set_interrupt_mode(&adc_ch_conf, ADCCH_MODE_COMPLETE);
	//adcch_enable_interrupt(&adc_ch_conf);
	adcch_write_configuration(&ADCA, ADC_CH1, &adc_ch_conf);
	
	
	adcch_set_input(&adc_ch_conf, ADCCH_POS_PIN0, ADCCH_NEG_PIN6, 1);
	adcch_write_configuration(&ADCA, ADC_CH0, &adc_ch_conf);


	adc_enable(&ADCA);
}
int main(void)
{
	struct adc_config         adc_conf;
	struct adc_channel_config adcch_conf;

	board_init();
	sysclk_init();
	sleepmgr_init();
	irq_initialize_vectors();
	cpu_irq_enable();
	gfx_mono_init();

	// Enable back light of display
	ioport_set_pin_high(LCD_BACKLIGHT_ENABLE_PIN);

	// Initialize configuration structures.
	adc_read_configuration(&ADCA, &adc_conf);
	adcch_read_configuration(&ADCA, ADC_CH0, &adcch_conf);

	/* Configure the ADC module:
	 * - unsigned, 12-bit results
	 * - VCC voltage reference
	 * - 200 kHz maximum clock rate
	 * - manual conversion triggering
	 * - temperature sensor enabled
	 * - callback function
	 */
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_12,
			ADC_REF_VCC);
	adc_set_clock_rate(&adc_conf, 200000UL);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);
	adc_enable_internal_input(&adc_conf, ADC_INT_TEMPSENSE);

	adc_write_configuration(&ADCA, &adc_conf);
	adc_set_callback(&ADCA, &adc_handler);

	/* Configure ADC channel 0:
	 * - single-ended measurement from temperature sensor
	 * - interrupt flag set on completed conversion
	 * - interrupts disabled
	 */
	adcch_set_input(&adcch_conf, ADCCH_POS_PIN1, ADCCH_NEG_NONE,
			1);
	adcch_set_interrupt_mode(&adcch_conf, ADCCH_MODE_COMPLETE);
	adcch_enable_interrupt(&adcch_conf);

	adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf);

	// Enable the ADC and start the first conversion.
	adc_enable(&ADCA);
	adc_start_conversion(&ADCA, ADC_CH0);

	do {
		// Sleep until ADC interrupt triggers.
		sleepmgr_enter_sleep();
	} while (1);
}
int main(void)
{
    struct adc_config         adc_conf;
    struct adc_channel_config adcch_conf;

    board_init();
    sysclk_init();
    sleepmgr_init();
    irq_initialize_vectors();
    cpu_irq_enable();

    // Initialize configuration structures.
    adc_read_configuration(&ADCA, &adc_conf);
    adcch_read_configuration(&ADCA, ADC_CH0, &adcch_conf);

    /* Configure the ADC module:
     * - unsigned, 12-bit results
     * - bandgap (1 V) voltage reference
     * - 200 kHz maximum clock rate
     * - manual conversion triggering
     * - temperature sensor enabled
     * - callback function
     */
    adc_set_conversion_parameters(&adc_conf, ADC_SIGN_OFF, ADC_RES_12,
                                  ADC_REF_BANDGAP);
    adc_set_clock_rate(&adc_conf, 200000UL);
    adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);
    adc_enable_internal_input(&adc_conf, ADC_INT_TEMPSENSE);

    adc_write_configuration(&ADCA, &adc_conf);
    adc_set_callback(&ADCA, &adc_handler);

    /* Configure ADC channel 0:
     * - single-ended measurement from temperature sensor
     * - interrupt flag set on completed conversion
     * - interrupts disabled
     */
    adcch_set_input(&adcch_conf, ADCCH_POS_TEMPSENSE, ADCCH_NEG_NONE,
                    1);
    adcch_set_interrupt_mode(&adcch_conf, ADCCH_MODE_COMPLETE);
    adcch_enable_interrupt(&adcch_conf);

    adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf);

    // Get measurement for 85 degrees C (358 kelvin) from calibration data.
    tempsense = adc_get_calibration_data(ADC_CAL_TEMPSENSE);

    // Enable the ADC and start the first conversion.
    adc_enable(&ADCA);
    adc_start_conversion(&ADCA, ADC_CH0);

    do {
        // Sleep until ADC interrupt triggers.
        sleepmgr_enter_sleep();
    } while (1);
}
/**
 * \internal
 * \brief Test averaging conversion in 12-bit mode using the DAC
 *
 * These values are then measured using the ADC on the pins that are connected
 * to the DAC channel, and the results are compared and checked to see if they
 * are within the acceptable average of values that passes the test.
 *
 * \param test Current test case.
 */
static void run_averaging_conversion_test(
		const struct test_case *test)
{
	int16_t volt_output;
	int16_t volt_min, volt_max, volt_input;

	/* ADC module configuration structure */
	struct adc_config adc_conf;
	/* ADC channel configuration structure */
	struct adc_channel_config adcch_conf;

	adc_disable(&ADCA);

	/* Change resolution parameter to accept averaging */
	adc_read_configuration(&ADCA, &adc_conf);
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_MT12,
			ADC_REF_VCC);
	adc_write_configuration(&ADCA, &adc_conf);

	/* Enable averaging */
	adcch_read_configuration(&ADCA, ADC_CH0, &adcch_conf);
	adcch_enable_averaging(&adcch_conf, ADC_SAMPNUM_1024X);
	adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf);

	adc_enable(&ADCA);
	adc_wait_for_interrupt_flag(&ADCA, ADC_CH0);

	/* Check values */
	for (volt_output = -CONV_MAX_VOLTAGE;
			volt_output <= CONV_MAX_VOLTAGE;
			volt_output += CONV_VOLTAGE_STEP) {
		main_dac_output(volt_output);
		/* first capture */
		volt_input = main_adc_input();
		volt_min = volt_max = volt_input;
		/* several capture */
		for (uint8_t i = 0; i < 5; i++) {
			volt_input = main_adc_input();
			if (volt_min > volt_input) {
				volt_min = volt_input;
			}

			if (volt_max < volt_input) {
				volt_max = volt_input;
			}
		}

		test_assert_true(test,
				(volt_max - volt_min) < CONF_TEST_ACCEPT_DELTA_AVERAGING,
				"ADC result is not in acceptable stable range (Min %dmV, Max %dmV)",
				volt_min, volt_max);
	}
}
Exemple #10
0
/**
 * \brief Timer Counter Overflow interrupt callback function
 *
 * This function is called when an overflow interrupt has occurred on
 * TCC0.
 */
static void tcc0_ovf_interrupt_callback(void)
{
	adc_mux_index=0;
	adc_disable(&EXT_VIN_ADC_MODULE);
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_OFF, ADC_RES_12
			,adc_conv[adc_mux_index].ref);
	adc_write_configuration(&EXT_VIN_ADC_MODULE, &adc_conf);
	adc_enable(&EXT_VIN_ADC_MODULE);
	adcch_set_input(&adcch_conf, adc_conv[adc_mux_index].in
			, ADCCH_NEG_NONE,1);
	adcch_write_configuration(&EXT_VIN_ADC_MODULE, ADC_CH0, &adcch_conf);
	adc_start_conversion(&EXT_VIN_ADC_MODULE, ADC_CH0);
}
Exemple #11
0
void ADC_init_funct(void) {
	struct adc_config adc_conf;
	struct adc_channel_config adcch_conf;

	adc_read_configuration(&MY_ADC, &adc_conf);
	adcch_read_configuration(&MY_ADC, CAP_ADC, &adcch_conf);

	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_OFF, ADC_RES_12,ADC_REF_VCC);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);
	adc_set_clock_rate(&adc_conf, 100000);
	adcch_set_input(&adcch_conf, ADCCH_POS_PIN3, ADCCH_NEG_NONE, 1);

	adc_write_configuration(&MY_ADC, &adc_conf);
	adcch_write_configuration(&MY_ADC, CAP_ADC, &adcch_conf);
	adc_enable (&MY_ADC);
}
Exemple #12
0
void owltemp_init() {
	struct adc_config         adc_conf;
	struct adc_channel_config adcch_conf;

	// Clear the configuration structures.
	memset(&adc_conf, 0, sizeof(struct adc_config));
	memset(&adcch_conf, 0, sizeof(struct adc_channel_config));

	/* Configure the ADC module:
	 * - unsigned, 12-bit results
	 * - bandgap (1 V) voltage reference
	 * - 200 kHz maximum clock rate
	 * - manual conversion triggering
	 * - temperature sensor enabled
	 * - callback function
	 */
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_OFF, ADC_RES_12,
			ADC_REF_BANDGAP);
	adc_set_clock_rate(&adc_conf, 200000UL);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 0, 0);
	adc_enable_internal_input(&adc_conf, ADC_INT_TEMPSENSE);

	adc_write_configuration(&ADCA, &adc_conf);
	adc_set_callback(&ADCA, &adc_handler);

	/* Configure ADC channel 0:
	 * - single-ended measurement from temperature sensor
	 * - interrupt flag set on completed conversion
	 * - interrupts disabled
	 */
	adcch_set_input(&adcch_conf, ADCCH_POS_TEMPSENSE, ADCCH_NEG_NONE,
			1);
	adcch_set_interrupt_mode(&adcch_conf, ADCCH_MODE_COMPLETE);
	adcch_enable_interrupt(&adcch_conf);

	adcch_write_configuration(&ADCA, 0, &adcch_conf);

	// Get measurement for 85 degrees C (358 kelvin) from calibration data.
	tempsense = adc_get_calibration_data(ADC_CAL_TEMPSENSE);

	// Enable the ADC and start the first conversion.
	adc_enable(&ADCA);
	adc_start_conversion(&ADCA, ADC_CH0);

}
Exemple #13
0
static void adc_init(void)
{
	struct adc_config adc_conf;
	struct adc_channel_config adcch_conf;

	adc_read_configuration(&MY_ADC, &adc_conf);
	adcch_read_configuration(&MY_ADC, MY_ADC_CH, &adcch_conf);

	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_OFF, ADC_RES_12,
	ADC_REF_AREFA);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);
	adc_set_clock_rate(&adc_conf, 200000UL);

	adcch_set_input(&adcch_conf, ADCCH_POS_PIN1, ADCCH_NEG_NONE, 1);

	adc_write_configuration(&MY_ADC, &adc_conf);
	adcch_write_configuration(&MY_ADC, MY_ADC_CH, &adcch_conf);
	
	adc_enable(&MY_ADC);
	adc_start_conversion(&MY_ADC, MY_ADC_CH);
}
/**
 * \brief Initialize ADC
 */
static void main_adc_init(void)
{
	/* ADC module configuration structure */
	struct adc_config adc_conf;
	/* ADC channel configuration structure */
	struct adc_channel_config adcch_conf;

	/* Configure the ADC module:
	 * - signed, 12-bit results
	 * - voltage reference = VCC / 1.6 = 3.3V / 1.6
	 * - 200 kHz maximum clock rate
	 * - manual conversion triggering
	 */
	adc_read_configuration(&ADCA, &adc_conf); /* Initialize structures. */
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_12,
			ADC_REF_VCC);
	adc_set_clock_rate(&adc_conf, 200000UL);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);
	adc_write_configuration(&ADCA, &adc_conf);

	/* Configure ADC channel:
	 * - differential measurement mode
	 * - Input voltage V+ is ADC2 pin (PA2 pin)
	 * - Input voltage V- is ADC3 pin (PA3 pin)
	 * - 1x gain
	 * - interrupt flag set on completed conversion
	 */
	adcch_read_configuration(&ADCA, ADC_CH0, &adcch_conf);
	adcch_set_input(&adcch_conf, ADCCH_POS_PIN2, ADCCH_NEG_PIN3, 1);
	adcch_set_interrupt_mode(&adcch_conf, ADCCH_MODE_COMPLETE);
	adcch_disable_interrupt(&adcch_conf);
	adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf);

	/* Enable ADC */
	adc_enable(&ADCA);

	/* Do useful conversion */
	adc_start_conversion(&ADCA, ADC_CH0);
	adc_wait_for_interrupt_flag(&ADCA, ADC_CH0);
}
/**
 * \brief Disables over-sampling
 */
static void main_adc_oversampling_stop(void)
{
    /* ADC module configuration structure */
    struct adc_config adc_conf;
    /* ADC channel configuration structure */
    struct adc_channel_config adcch_conf;

    adc_disable(&ADCA);

    /* Set default resolution parameter */
    adc_read_configuration(&ADCA, &adc_conf);
    adc_set_conversion_parameters(&adc_conf, ADC_SIGN_OFF, ADC_RES_12,
                                  ADC_REF_VCCDIV2);
    adc_write_configuration(&ADCA, &adc_conf);

    /* Disable over-sampling */
    adcch_read_configuration(&ADCA, ADC_CH0, &adcch_conf);
    adcch_disable_oversampling(&adcch_conf);
    adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf);

    adc_enable(&ADCA);
    printf("\n\r* ADC over-sampling disabled\n\r");
}
/**
 * \brief Enables over-sampling
 */
static void main_adc_oversampling_start(void)
{
    /* ADC module configuration structure */
    struct adc_config adc_conf;
    /* ADC channel configuration structure */
    struct adc_channel_config adcch_conf;

    adc_disable(&ADCA);

    /* Change resolution parameter to accept over-sampling */
    adc_read_configuration(&ADCA, &adc_conf);
    adc_set_conversion_parameters(&adc_conf, ADC_SIGN_OFF, ADC_RES_MT12,
                                  ADC_REF_VCCDIV2);
    adc_write_configuration(&ADCA, &adc_conf);

    /* Enable over-sampling */
    adcch_read_configuration(&ADCA, ADC_CH0, &adcch_conf);
    adcch_enable_oversampling(&adcch_conf, ADC_SAMPNUM_1024X, 16);
    adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf);

    adc_enable(&ADCA);
    printf("\n\r* ADC over-sampling enabled\n\r");
}
Exemple #17
0
/**
 * \brief Enables averaging
 */
static void main_adc_averaging(void)
{
	/* ADC module configuration structure */
	struct adc_config adc_conf;
	/* ADC channel configuration structure */
	struct adc_channel_config adcch_conf;

	adc_disable(&ADCA);

	/* Change resolution parameter to accept averaging */
	adc_read_configuration(&ADCA, &adc_conf);
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_MT12,
			ADC_REF_VCC);
	adc_write_configuration(&ADCA, &adc_conf);

	/* Enable averaging */
	adcch_read_configuration(&ADCA, ADC_CH0, &adcch_conf);
	adcch_enable_averaging(&adcch_conf, ADC_SAMPNUM_1024X);
	adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf);

	adc_enable(&ADCA);
	adc_wait_for_interrupt_flag(&ADCA, ADC_CH0);
}
Exemple #18
0
int main(void)
{
	struct adc_config         adc_conf;
	struct adc_channel_config adcch_conf;

	board_init();
	sysclk_init();
	sleepmgr_init();
	irq_initialize_vectors();
	cpu_irq_enable();

	// Initialize configuration structures.
	adc_read_configuration(&ADCA, &adc_conf);
	adcch_read_configuration(&ADCA, ADC_CH0, &adcch_conf);

	/* Configure the ADC module:
	 * - unsigned, 12-bit results
	 * - bandgap (1 V) voltage reference
	 * - 200 kHz maximum clock rate
	 * - manual conversion triggering
	 */
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_OFF, ADC_RES_12,
			ADC_REF_BANDGAP);
	adc_set_clock_rate(&adc_conf, 200000UL);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);

	adc_write_configuration(&ADCA, &adc_conf);

	/* Configure ADC channel 0:
	 * - single-ended measurement from configured input pin
	 * - interrupt flag set on completed conversion
	 */
	adcch_set_input(&adcch_conf, INPUT_PIN, ADCCH_NEG_NONE,
			1);
	adcch_set_interrupt_mode(&adcch_conf, ADCCH_MODE_COMPLETE);
	adcch_disable_interrupt(&adcch_conf);

	adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf);

	// Enable the ADC and do one dummy conversion.
	adc_enable(&ADCA);
	adc_start_conversion(&ADCA, ADC_CH0);
	adc_wait_for_interrupt_flag(&ADCA, ADC_CH0);

	// Light up LED 1, wait for button press.
	ioport_set_pin_low(LED1_PIN);
	wait_for_button();

	// Perform oversampling of offset.
	cal_data.offset = get_mean_sample_value();

	// Light up LED 2, wait for button press.
	ioport_set_pin_low(LED2_PIN);
	wait_for_button();

	// Perform oversampling of 0.9 V for gain calibration.
	cal_data.gain = get_mean_sample_value() - cal_data.offset;

	// Turn off LEDs.
	ioport_set_pin_high(LED1_PIN);
	ioport_set_pin_high(LED2_PIN);

	// Enable interrupts on ADC channel, then trigger first conversion.
	adcch_enable_interrupt(&adcch_conf);
	adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf);
	adc_start_conversion(&ADCA, ADC_CH0);

	do {
		// Sleep until ADC interrupt triggers.
		sleepmgr_enter_sleep();
	} while (1);
}
void alphasense_adc_getValue( void )
{
	//ALPHASENSE_STATS * const ps = &g_internal;
	struct adc_config adc_conf;
	struct adc_channel_config adcch_conf;
	uint8_t inputgain = 1;//, elements = 20;
	//char szBUF[64];
	
	// DISABLE jtag - it locks the upper 4 pins of PORT B
	CCP       = CCP_IOREG_gc;    // Secret handshake
	MCU.MCUCR = 0b00000001;
	
	PORTB.PIN0CTRL = PORT_OPC_TOTEM_gc; // Auxiliary Electrode
	PORTB.PIN2CTRL = PORT_OPC_TOTEM_gc; // Working Electrode
	PORTB.PIN6CTRL = PORT_OPC_TOTEM_gc; // GND x offset
	
	adc_read_configuration(&ALPHASENSE_ADC, &adc_conf);
	adcch_read_configuration(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH, &adcch_conf);
	
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_OFF, ADC_RES_12, ADC_REF_VCC);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);
	adc_set_clock_rate(&adc_conf, 200000UL);
	adc_write_configuration(&ALPHASENSE_ADC, &adc_conf);
	
	
	adcch_set_input(&adcch_conf, ADCCH_POS_PIN6, ADCCH_NEG_NONE, inputgain);
	adcch_write_configuration(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH, &adcch_conf);
	adc_enable(&ALPHASENSE_ADC);
	adc_start_conversion(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
	adc_wait_for_interrupt_flag(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
	adc_start_conversion(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
	adc_wait_for_interrupt_flag(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
	const int16_t off = adc_get_result(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
	adc_disable(&ALPHASENSE_ADC);
	//sprintf_P(szBUF,PSTR("Offset: %u\t"),off);
	//debug_string(NORMAL,szBUF,false);
	
	
	//adcch_set_input(&adcch_conf, ADCCH_POS_BANDGAP, ADCCH_NEG_NONE, inputgain);
	//adcch_write_configuration(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH, &adcch_conf);
	//adc_enable(&ALPHASENSE_ADC);
	//adc_start_conversion(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
	//adc_wait_for_interrupt_flag(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
	//const uint16_t gain = adc_get_result(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
	//adc_disable(&ALPHASENSE_ADC);
	////sprintf_P(szBUF,PSTR("gain: %u\t"),gain);
	////debug_string(NORMAL,szBUF,false);
	
	
	adcch_set_input(&adcch_conf, ADCCH_POS_PIN0, ADCCH_NEG_NONE, inputgain);
	adcch_write_configuration(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH, &adcch_conf);
	adc_enable(&ALPHASENSE_ADC);
	adc_start_conversion(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
	adc_wait_for_interrupt_flag(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
	adc_start_conversion(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
	adc_wait_for_interrupt_flag(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
	const int16_t adc_w = adc_get_result(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH)-off;
	adc_disable(&ALPHASENSE_ADC);
	//sprintf_P(szBUF,PSTR("ADC_WORK: %u\t"),adc_w);
	//debug_string(NORMAL,szBUF,false);
	
	adcch_set_input(&adcch_conf, ADCCH_POS_PIN2, ADCCH_NEG_NONE, inputgain);
	adcch_write_configuration(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH, &adcch_conf);
	adc_enable(&ALPHASENSE_ADC);
	adc_start_conversion(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
	adc_wait_for_interrupt_flag(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
	adc_start_conversion(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
	adc_wait_for_interrupt_flag(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
	const int16_t adc_a = adc_get_result(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH)-off;
	adc_disable(&ALPHASENSE_ADC);
	//sprintf_P(szBUF,PSTR("ADC_AUX: %u\r\n"),adc_a);
	//debug_string(NORMAL,szBUF,false);	
	
	
	
	//adc_enable(&ALPHASENSE_ADC);
	//
	//for (int i=0;i<elements;i++)
	//{
		//adc_start_conversion(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
		//adc_wait_for_interrupt_flag(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
		//adc_start_conversion(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
		//adc_wait_for_interrupt_flag(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
		//result[i]=adc_get_result(&ALPHASENSE_ADC, ALPHASENSE_ADC_CH);
//
	//}
	
	//adc_disable(&ALPHASENSE_ADC);
	
	PORTB.PIN0CTRL = PORT_OPC_PULLUP_gc;
	PORTB.PIN2CTRL = PORT_OPC_PULLUP_gc;
	PORTB.PIN6CTRL = PORT_OPC_PULLUP_gc;
	
	g_recordingData_Alphasense = true;
	g_partialSumWork+=(int32_t)adc_w;
	g_partialSumAux+=(int32_t)adc_a;
	g_partialCount_Alphasense++;
	g_recordingData_Alphasense = false;
	
	//sprintf_P(szBUF,PSTR("Sample: %u\tPartialSumWork: %lu\tPartialSumAux: %lu\r\n"),g_partialCount, g_partialSumWork,g_partialSumAux);
	//debug_string(NORMAL,szBUF,false);
	
	//ps->working = adc_w;
	//ps->aux = adc_a;
}
Exemple #20
0
void app_sampling_init(void)
{
	/* QDec configuration */
	qdec_get_config_defaults(&qdec_config);
	qdec_config_phase_pins(&qdec_config, &PORTA, 6, false, 500);
	qdec_config_enable_rotary(&qdec_config);
	qdec_config_tc(&qdec_config, &TCC5);
	qdec_config_revolution(&qdec_config, 40);
	qdec_enabled(&qdec_config);

	/* ! ADC module configuration */
	struct adc_config adc_conf;

	/* Configure the ADC module:
	 * - signed, 12-bit results
	 * - VCC reference
	 * - 200 kHz maximum clock rate
	 * - manual conversion triggering
	 * - callback function
	 */
	adc_read_configuration(&LIGHT_SENSOR_ADC_MODULE, &adc_conf);
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_12,
			ADC_REF_VCC);
	adc_set_clock_rate(&adc_conf, 200000);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);
	adc_write_configuration(&LIGHT_SENSOR_ADC_MODULE, &adc_conf);
	adc_set_callback(&LIGHT_SENSOR_ADC_MODULE, &app_sampling_handler);
	adc_enable(&LIGHT_SENSOR_ADC_MODULE);

	/* Configure ADC A channel 0 for light and NTC sensors.
	 * - differantial measurement (V- linked on internal GND)
	 * - gain x0.5
	 * - interrupt flag set on completed conversion
	 * - interrupts enabled
	 */
	adcch_read_configuration(&LIGHT_SENSOR_ADC_MODULE, ADC_CH0,
			&adcch_conf);
	adcch_set_input(&adcch_conf, LIGHT_SENSOR_ADC_INPUT,
			ADCCH_NEG_INTERNAL_GND, 0);
	adcch_set_interrupt_mode(&adcch_conf, ADCCH_MODE_COMPLETE);
	adcch_enable_interrupt(&adcch_conf);
	adcch_write_configuration(&LIGHT_SENSOR_ADC_MODULE, ADC_CH0,
			&adcch_conf);

	fifo_init(&app_sampling_fifo_desc, app_sampling_fifo_buffer,
			APP_SAMPLING_FIFO_SIZE);
	rtc_set_callback(&app_sampling_start);

	/* Display background */
	gfx_mono_draw_line(DISPLAY_SAMPLING_TEXT_POS_X - 3,
			0,
			DISPLAY_SAMPLING_TEXT_POS_X - 3,
			32,
			GFX_PIXEL_SET);
	app_sampling_display_rate();
	gfx_mono_draw_string(DISPLAY_LIGHT_TEXT,
			DISPLAY_LIGHT_TEXT_POS_X,
			DISPLAY_LIGHT_TEXT_POS_Y,
			&sysfont);
	gfx_mono_draw_filled_rect(
			DISPLAY_LIGHT_PROBAR_START_POS_X,
			DISPLAY_LIGHT_PROBAR_START_POS_Y,
			DISPLAY_LIGHT_PROBAR_START_SIZE_X,
			DISPLAY_LIGHT_PROBAR_START_SIZE_Y,
			GFX_PIXEL_SET);
	gfx_mono_draw_filled_rect(
			DISPLAY_LIGHT_PROBAR_STOP_POS_X,
			DISPLAY_LIGHT_PROBAR_STOP_POS_Y,
			DISPLAY_LIGHT_PROBAR_STOP_SIZE_X,
			DISPLAY_LIGHT_PROBAR_STOP_SIZE_Y,
			GFX_PIXEL_SET);

	/* Start a RTC alarm immediatly */
	rtc_set_alarm_relative(0);
}
Exemple #21
0
/**
 * \internal
 * \brief Test differential conversion with gain in 12-bit mode using the DAC
 *
 * This test outputs a gain compensated level on DAC output that should result
 * in a value of half maximum positive value on the ADC.
 *
 * These values are then measured using the ADC on the pins that are connected
 * to the DAC channel, and the results are compared and checked to see if they
 * are within the acceptable range of values that passes the test.
 *
 * \param test Current test case.
 */
static void run_differential_12bit_with_gain_conversion_test(
		const struct test_case *test)
{
	// Number of MUX inputs that are to be read
	const uint8_t num_inputs = 2;

	/* Connection between DAC outputs and ADC MUX inputs.
	 * Only the high nibble on PORTA is possible for gain.
	 * input 4, 6 is connected to DACA0
	 * input 5, 7 is connected to DACA1.
	 */
	const uint8_t channel_pos[] = {4, 6};
	const uint8_t channel_neg[] = {5, 7};

	/*
	 * Go through gain level up to 8, since any higher gives too much
	 * propagated error for sensible unit test limits.
	 */
	const uint8_t gain[] = {1, 2, 4, 8};
	uint8_t gain_index;

	uint8_t adc_channel;
	uint8_t mux_index;
	int16_t results[2];
	struct dac_config dac_conf;
	struct adc_config adc_conf;

	// Configure ADC
	adc_read_configuration(&ADCA, &adc_conf);
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_12,
			ADC_REF_BANDGAP);
	adc_set_clock_rate(&adc_conf, 2000UL);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);
	adc_write_configuration(&ADCA, &adc_conf);

	// Configure DAC
	dac_read_configuration(&DACA, &dac_conf);
	dac_set_conversion_parameters(&dac_conf, DAC_REF_BANDGAP,
			DAC_ADJ_RIGHT);
	dac_set_active_channel(&dac_conf, DAC_CH0 | DAC_CH1, 0);
	dac_set_conversion_trigger(&dac_conf, 0, 0);
	dac_set_conversion_interval(&dac_conf, 10);
	dac_set_refresh_interval(&dac_conf, 20);
	dac_write_configuration(&DACA, &dac_conf);
	dac_enable(&DACA);


	// Set negative output to zero
	dac_wait_for_channel_ready(&DACA, DAC_CH1);
	dac_set_channel_value(&DACA, DAC_CH1, DAC_MIN);

	for (gain_index = 0; gain_index < sizeof(gain); gain_index++) {
		// Set positive output to half positive range adjusted to gain
		dac_wait_for_channel_ready(&DACA, DAC_CH0);
		dac_set_channel_value(&DACA, DAC_CH0, DAC_MAX /
				(gain[gain_index] * 2));

		/* Read all ADC pins connected to active DAC output.
		 * All channels are converted NUM_SAMPLES times and the
		 * final value used for the assert is an average.
		 */
		for (adc_channel = 0; adc_channel < NUM_CHANNELS;
				adc_channel++) {
			differential_signed_average(&ADCA, 1 << adc_channel,
					channel_pos, channel_neg, num_inputs,
					results, gain[gain_index]);
			for (mux_index = 0; mux_index < num_inputs;
					mux_index++) {
				verify_signed_result(test,
						ADC_SIGNED_12BIT_MAX / 2,
						results[mux_index],
						adc_channel,
						channel_pos[mux_index],
						channel_neg[mux_index],
						ADC_SIGNED_12BIT_MIN,
						ADC_SIGNED_12BIT_MAX,
						gain[gain_index], true);
			}
		}
	}
}
Exemple #22
0
/**
 * \internal
 * \brief Test differential conversion in 12-bit mode using the DAC
 *
 * This tests output three different values on the two DAC channels:
 * - 1/2 * \ref DAC_MAX on both outputs to get a differential zero
 * - \ref DAC_MAX on positive and \ref DAC_MIN on negative to get max positive
 * result
 * - \ref DAC_MIN on positive and \ref DAC_MAX on negative to get max negative
 * result
 *
 * These values are then measured using the ADC on the pins that are connected
 * to the DAC channel, and the results are compared and checked to see if they
 * are within the acceptable range of values that passes the test.
 *
 * \param test Current test case.
 */
static void run_differential_12bit_conversion_test(
		const struct test_case *test)
{
	// Number of MUX inputs that are to be read
	const uint8_t num_inputs = 4;

	/* Connection between DAC outputs and ADC MUX inputs
	 * input 0, 2, 4, 6 is connected to DACA0
	 * input 1, 3, 5, 7 is connected to DACA1.
	 */
	const uint8_t channel_pos[] = {0, 2, 4, 6};
	const uint8_t channel_neg[] = {1, 3, 5, 7};

	uint8_t adc_channel;
	uint8_t mux_index;
	int16_t results[4];
	struct dac_config dac_conf;
	struct adc_config adc_conf;

	// Configure ADC
	adc_read_configuration(&ADCA, &adc_conf);
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_12,
			ADC_REF_BANDGAP);
	adc_set_clock_rate(&adc_conf, 2000UL);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);
	adc_write_configuration(&ADCA, &adc_conf);

	// Configure DAC
	dac_read_configuration(&DACA, &dac_conf);
	dac_set_conversion_parameters(&dac_conf, DAC_REF_BANDGAP,
			DAC_ADJ_RIGHT);
	dac_set_active_channel(&dac_conf, DAC_CH0 | DAC_CH1, 0);
	dac_set_conversion_trigger(&dac_conf, 0, 0);
	dac_set_conversion_interval(&dac_conf, 10);
	dac_set_refresh_interval(&dac_conf, 20);
	dac_write_configuration(&DACA, &dac_conf);
	dac_enable(&DACA);


	// Set outputs to same (1/2 * MAX_VALUE) to get zero
	dac_wait_for_channel_ready(&DACA, DAC_CH0 | DAC_CH1);
	dac_set_channel_value(&DACA, DAC_CH0, DAC_MAX / 2);
	dac_set_channel_value(&DACA, DAC_CH1, DAC_MAX / 2);

	/* Read all ADC pins connected to active DAC output.
	 * All channels are converted NUM_SAMPLES times and the
	 * final value used for the assert is an average.
	 */
	for (adc_channel = 0; adc_channel < NUM_CHANNELS; adc_channel++) {
		differential_signed_average(&ADCA, 1 << adc_channel,
				channel_pos, channel_neg, num_inputs, results,
				1);
		for (mux_index = 0; mux_index < num_inputs; mux_index++) {
			verify_signed_result(test, ADC_ZERO,
					results[mux_index], adc_channel,
					channel_pos[mux_index],
					channel_neg[mux_index],
					ADC_SIGNED_12BIT_MIN,
					ADC_SIGNED_12BIT_MAX, 1, true);
		}
	}

	// Set output to max positive range for positive result
	dac_wait_for_channel_ready(&DACA, DAC_CH0 | DAC_CH1);
	dac_set_channel_value(&DACA, DAC_CH0, DAC_MAX);
	dac_set_channel_value(&DACA, DAC_CH1, DAC_MIN);

	/* Read all ADC pins connected to active DAC output.
	 * All channels are converted NUM_SAMPLES times and the
	 * final value used for the assert is an average.
	 */
	for (adc_channel = 0; adc_channel < NUM_CHANNELS; adc_channel++) {
		differential_signed_average(&ADCA, 1 << adc_channel,
				channel_pos, channel_neg, num_inputs, results,
				1);
		for (mux_index = 0; mux_index < num_inputs; mux_index++) {
			verify_signed_result(test, ADC_SIGNED_12BIT_MAX,
					results[mux_index], adc_channel,
					channel_pos[mux_index],
					channel_neg[mux_index],
					ADC_SIGNED_12BIT_MIN,
					ADC_SIGNED_12BIT_MAX, 1, true);
		}
	}

	// Set output to max negative range for negative result
	dac_wait_for_channel_ready(&DACA, DAC_CH0 | DAC_CH1);
	dac_set_channel_value(&DACA, DAC_CH0, DAC_MIN);
	dac_set_channel_value(&DACA, DAC_CH1, DAC_MAX);

	/* Read all ADC pins connected to active DAC output.
	 * All channels are converted NUM_SAMPLES times and the
	 * final value used for the assert is an average.
	 */
	for (adc_channel = 0; adc_channel < NUM_CHANNELS; adc_channel++) {
		differential_signed_average(&ADCA, 1 << adc_channel,
				channel_pos, channel_neg, num_inputs, results,
				1);
		for (mux_index = 0; mux_index < num_inputs; mux_index++) {
			verify_signed_result(test, ADC_SIGNED_12BIT_MIN,
					results[mux_index], adc_channel,
					channel_pos[mux_index],
					channel_neg[mux_index],
					ADC_SIGNED_12BIT_MIN,
					ADC_SIGNED_12BIT_MAX, 1, true);
		}
	}
}
Exemple #23
0
/**
 * \internal
 * \brief Test single ended conversion in 8-bit mode using the DAC
 *
 * This tests output three different values on the two DAC channels:
 * - 0 (output analog value is greater than 0, as the DAC cannot go that low)
 * - 1/2 * \ref DAC_MAX Half of the maximum value of the DAC
 * - \ref DAC_MAX The maximum value (VREF) of the DAC.
 *
 * These values are then measured using the ADC on the pins that are connected
 * to the DAC channel, using all available ADC channels and the results are
 * compared and checked to see if they are within the acceptable range of
 * values that passes the test.
 *
 * \param test Current test case.
 */
static void run_single_ended_8bit_conversion_test(
		const struct test_case *test)
{
	// Number of MUX inputs that are to be read
	const uint8_t num_inputs = 4;

	/* Connection between DAC outputs and ADC MUX inputs
	 * input 0, 2, 4, 6 is connected to DACA0
	 * input 1, 3, 5, 7 is connected to DACA1.
	 */
	const uint8_t channelgroup[2][4] = {{0, 2, 4, 6}, {1, 3, 5, 7}};

	uint8_t dac_channel;
	uint8_t adc_channel;
	uint8_t mux_index;
	uint16_t results[4];
	struct dac_config dac_conf;
	struct adc_config adc_conf;

	// Configure ADC
	adc_read_configuration(&ADCA, &adc_conf);
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_OFF, ADC_RES_8,
			ADC_REF_BANDGAP);
	adc_set_clock_rate(&adc_conf, 2000UL);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);
	adc_write_configuration(&ADCA, &adc_conf);

	// Configure DAC
	dac_read_configuration(&DACA, &dac_conf);
	dac_set_conversion_parameters(&dac_conf, DAC_REF_BANDGAP, DAC_ADJ_RIGHT);
	dac_set_active_channel(&dac_conf, DAC_CH0 | DAC_CH1, 0);
	dac_set_conversion_trigger(&dac_conf, 0, 0);
	dac_set_conversion_interval(&dac_conf, 10);
	dac_set_refresh_interval(&dac_conf, 20);

	dac_write_configuration(&DACA, &dac_conf);
	dac_enable(&DACA);

	// Set outputs as zero
	dac_wait_for_channel_ready(&DACA, DAC_CH0 | DAC_CH1);
	dac_set_channel_value(&DACA, DAC_CH0, DAC_MIN);
	dac_set_channel_value(&DACA, DAC_CH1, DAC_MIN);

	for(dac_channel = 0; dac_channel < 2; dac_channel++) {
		/* Read all ADC pins connected to active DAC output.
		 * All channels are converted NUM_SAMPLES times and the
		 * final value used for the assert is an average.
		 */
		for (adc_channel = 0; adc_channel < NUM_CHANNELS;
				adc_channel++) {
			single_ended_unsigned_average(&ADCA, 1 << adc_channel,
					(uint8_t *)&channelgroup[dac_channel],
					num_inputs, results);
			for (mux_index = 0; mux_index < num_inputs;
					mux_index++) {
				verify_unsigned_result(test,
						ADC_ZERO,
						results[mux_index],
						dac_channel,
						adc_channel,
						channelgroup[dac_channel]
						[mux_index],
						ADC_UNSIGNED_8BIT_MAX, false);
			}
		}
	}

	// Set outputs as 1/2 * MAX_VALUE
	dac_wait_for_channel_ready(&DACA, DAC_CH0 | DAC_CH1);
	dac_set_channel_value(&DACA, DAC_CH0, DAC_MAX / 2);
	dac_set_channel_value(&DACA, DAC_CH1, DAC_MAX / 2);

	for(dac_channel = 0; dac_channel < 2; dac_channel++) {
		/* Read all ADC pins connected to active DAC output.
		 * All channels are converted NUM_SAMPLES times and the
		 * final value used for the assert is an average.
		 */
		for (adc_channel = 0; adc_channel < NUM_CHANNELS;
				adc_channel++) {
			single_ended_unsigned_average(&ADCA, 1 << adc_channel,
					(uint8_t *)&channelgroup[dac_channel],
					num_inputs, results);
			for (mux_index = 0; mux_index < num_inputs;
					mux_index++) {
				verify_unsigned_result(test,
						ADC_UNSIGNED_8BIT_MAX / 2,
						results[mux_index],
						dac_channel,
						adc_channel,
						channelgroup[dac_channel]
						[mux_index],
						ADC_UNSIGNED_8BIT_MAX, false);
			}
		}
	}

	// Set outputs as MAX_VALUE
	dac_wait_for_channel_ready(&DACA, DAC_CH0 | DAC_CH1);
	dac_set_channel_value(&DACA, DAC_CH0, DAC_MAX);
	dac_set_channel_value(&DACA, DAC_CH1, DAC_MAX);

	for(dac_channel = 0; dac_channel < 2; dac_channel++) {
		/* Read all ADC pins connected to active DAC output.
		 * All channels are converted NUM_SAMPLES times and the
		 * final value used for the assert is an average.
		 */
		for (adc_channel = 0; adc_channel < NUM_CHANNELS;
				adc_channel++) {
			single_ended_unsigned_average(&ADCA, 1 << adc_channel,
					(uint8_t *)&channelgroup[dac_channel],
					num_inputs, results);
			for (mux_index = 0; mux_index < num_inputs;
					mux_index++) {
				verify_unsigned_result(test,
						ADC_UNSIGNED_8BIT_MAX,
						results[mux_index],
						dac_channel,
						adc_channel,
						channelgroup[dac_channel]
						[mux_index],
						ADC_UNSIGNED_8BIT_MAX, false);
			}
		}
	}
}
Exemple #24
0
/**
 * \brief This function initialize the ADCB,gets ADCB-CH0 offset and configure
 *        ADCB-CH0 for oversampling
 *  - ADCB-CH0 is configured in 12bit, signed differential mode without gain
 *  - To read ADC offset, ADCB-Pin3(PB3) used as both +ve and -ve input
 *  - After reading ADC offset,to start oversampling,ADCB +ve and -ve input
 *    are configured
 */
void init_adc(void)
{
	/* Initialize configuration structures */
	adc_read_configuration(&ADCB, &adc_conf);
	adcch_read_configuration(&ADCB, ADC_CH0, &adc_ch_conf);

	/* Configure the ADCB module:
	 * - Signed, 12-bit resolution
	 * - External reference on AREFB pin.
	 * - 250 KSPS ADC clock rate
	 * - Manual conversion triggering
	 * - Callback function
	 */
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_12,
			ADC_REF_AREFB);
	adc_set_clock_rate(&adc_conf, 250000UL);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);
	adc_write_configuration(&ADCB, &adc_conf);
	adc_set_callback(&ADCB, &adc_handler);

	/* Configure ADC B channel 0 for offset calculation
	 * - Differential mode without gain
	 * - Selected Pin3 (PB3) as +ve and -ve input for offset calculation
	 */
	adcch_set_input(&adc_ch_conf, ADCCH_POS_PIN3, ADCCH_NEG_PIN3, 1);
	adcch_write_configuration(&ADCB, ADC_CH0, &adc_ch_conf);

	/* Enable ADCB */
	adc_enable(&ADCB);

	/* Get ADC offset in to ADC_Offset variable and disable ADC */
	adc_offset_one_sample = adc_offset_get_signed();

	/* Find ADC_Offset for for total number of samples */
	adc_offset = adc_offset_one_sample * ADC_OVER_SAMPLED_NUMBER;

	/* Disable ADC to configure for oversampling */
	adc_disable(&ADCB);

	/* Configure the ADCB module for oversampling:
	 * - Signed, 12-bit resolution
	 * - External reference on AREFB pin.
	 * - 250 KSPS ADC clock rate
	 * - Free running mode on Channel0 ( First Channel)
	 */

	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_FREERUN_SWEEP, 1, 0);
	adc_write_configuration(&ADCB, &adc_conf);

	/* Configure ADC B channel 0 for oversampling input
	 * - Differential mode without gain
	 * - Selected Pin1 (PB1) as +ve and Pin2 (PB2) as-ve input
	 * - Conversion complete interrupt
	 */
	adcch_set_input(&adc_ch_conf, ADC_OVER_SAMP_POSTIVE_PIN,
			ADC_OVER_SAMP_NEGATIVE_PIN, 1);
	adcch_set_interrupt_mode(&adc_ch_conf, ADCCH_MODE_COMPLETE);
	adcch_enable_interrupt(&adc_ch_conf);
	adcch_write_configuration(&ADCB, ADC_CH0, &adc_ch_conf);

	/* Enable ADCB */
	adc_enable(&ADCB);
}
void ADCA_init(void)
{
    struct adc_config adca_conf;
    struct adc_channel_config adca_ch_conf;
    //
    //// Initialize configuration structures.
    //adc_read_configuration(&ADCB, &adcb_conf);
    //
    ///* Configure the ADC module:
    //* - unsigned, 12-bit results
    //* - AREFA voltage reference
    //* - 8000 kHz clock rate
    //* - FreeRun Mode
    //*/
    adc_get_calibration_data(ADC_CAL_ADCA);
    adc_set_conversion_parameters(&adca_conf,ADC_SIGN_OFF,ADC_RES_12,ADC_REF_AREFA);
    adc_set_clock_rate(&adca_conf,125000UL);
    adc_set_conversion_trigger(&adca_conf,ADC_TRIG_FREERUN_SWEEP,1,0);
   // adc_set_config_compare_value(adcb_conf,KCK_MAX_CHARGE_AMP);
    adc_write_configuration(&ADCA,&adca_conf);
    //
    ///* Configure ADC channel 0:
    //* - Input: ADCB4
    //* - interrupts disable
    //*/
    adcch_read_configuration(&ADCA,1, &adca_ch_conf);
    adcch_set_input(&adca_ch_conf,ADCCH_POS_PIN3,ADCCH_NEG_NONE,ADC_CH_GAIN_1X_gc);
    adcch_write_configuration(&ADCA,1,&adca_ch_conf);
    
    ///* Configure ADC channel 1: darim az channel 0 estefade mikonim ehtemalan!
    //* - Input: ADCB5
    //* - Set Interrupt Mode: Below the threshold
    //* - interrupts disable
    ////*/
    //adcch_read_configuration(&ADCA,1, &adca_ch_conf);
    //adcch_set_input(&adcb_ch_conf,ADCCH_POS_PIN5,ADCCH_NEG_NONE,ADC_CH_GAIN_1X_gc);
	////adcch_set_interrupt_mode(&adcb_ch_conf,ADCCH_MODE_ABOVE);
	////adcch_enable_interrupt(&adcb_ch_conf);
    //adcch_write_configuration(&ADCA,1,&adca_ch_conf);
  
	//
    ///* Configure ADC channel 2:
    //* - Input: ADCB6
    //* - interrupts disable
    //*/
    //adcch_read_configuration(&ADCB,2, &adcb_ch_conf);
    //adcch_set_input(&adcb_ch_conf,ADCCH_POS_PIN6,ADCCH_NEG_NONE,ADC_CH_GAIN_1X_gc);
    ////adcch_disable_interrupt(&adcb_ch_conf);
    //adcch_write_configuration(&ADCB,2,&adcb_ch_conf);
    ////
    ///* Configure ADC channel 3:
    //* - Input: ADCB7
    //* - interrupts disable
    //*/
    //adcch_read_configuration(&ADCB,3, &adcb_ch_conf);
    //adcch_set_input(&adcb_ch_conf,ADCCH_POS_PIN7,ADCCH_NEG_NONE,ADC_CH_GAIN_1X_gc);
    //adcch_set_interrupt_mode(&adcb_ch_conf,ADCCH_MODE_ABOVE);
    //adcch_enable_interrupt(&adcb_ch_conf);
    //adcch_write_configuration(&ADCB,3,&adcb_ch_conf);
    //
    adc_enable(&ADCA);
    adc_start_conversion(&ADCA,ADC_CH0);
    //adc_start_conversion(&ADCB,ADC_CH1);
    //adc_start_conversion(&ADCB,ADC_CH2);
    ////adc_start_conversion(&ADCB,ADC_CH3);
}