/** * \internal * \brief Setup Function: ADC average mode test. * * This function initializes the ADC in averaging mode. * * \param test Current test case. */ static void setup_adc_average_mode_test(const struct test_case *test) { enum status_code status = STATUS_ERR_IO; /* Skip test if ADC initialization failed */ test_assert_true(test, adc_init_success, "Skipping test due to failed initialization"); /* Disable ADC before initialization */ adc_disable(&adc_inst); struct adc_config config; adc_get_config_defaults(&config); config.positive_input = ADC_POSITIVE_INPUT_PIN2; config.negative_input = ADC_NEGATIVE_INPUT_GND; #if (SAML21) config.reference = ADC_REFERENCE_INTREF; #else config.reference = ADC_REFERENCE_INT1V; #endif config.clock_source = GCLK_GENERATOR_3; #if !(SAML21) config.gain_factor = ADC_GAIN_FACTOR_1X; #endif /* Re-initialize & enable ADC */ status = adc_init(&adc_inst, ADC, &config); test_assert_true(test, status == STATUS_OK, "ADC initialization failed"); status = adc_enable(&adc_inst); test_assert_true(test, status == STATUS_OK, "ADC enabling failed"); }
OSStatus platform_adc_init( const platform_adc_t* adc, uint32_t sample_cycle ) { OSStatus err = kNoErr; struct adc_config adc_cfg; UNUSED_PARAMETER(sample_cycle); platform_mcu_powersave_disable(); require_action_quiet( adc != NULL, exit, err = kParamErr); if( initialized != true ) { adc_enable(); adc_select_clock_source_mck(ADC); adc_get_config_defaults(&adc_cfg); adc_init(ADC, &adc_cfg); adc_set_trigger(ADC, ADC_TRIG_SW); adc_set_resolution(ADC, adc->resolution); initialized = true; } exit: platform_mcu_powersave_enable(); return err; }
//! [setup] void configure_adc(void) { //! [setup_config] struct adc_config config_adc; //! [setup_config] //! [setup_config_defaults] adc_get_config_defaults(&config_adc); //! [setup_config_defaults] //! [setup_modify_conf] config_adc.gain_factor = ADC_GAIN_FACTOR_DIV2; config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV8; config_adc.reference = ADC_REFERENCE_INTVCC1; config_adc.positive_input = ADC_POSITIVE_INPUT_PIN6; config_adc.resolution = ADC_RESOLUTION_12BIT; //! [setup_modify_conf] //! [setup_set_config] adc_init(&adc_instance, ADC, &config_adc); //! [setup_set_config] //! [setup_enable] adc_enable(&adc_instance); //! [setup_enable] }
//! [setup] void configure_adc(void) { //! [setup_config] struct adc_config config_adc; //! [setup_config] //! [setup_config_defaults] adc_get_config_defaults(&config_adc); //! [setup_config_defaults] config_adc.reference = ADC_REFERENCE_INT1V; //ADC_REFERENCE_INTVCC0; //reference voltage on pin 9: PA04 config_adc.resolution = ADC_RESOLUTION_12BIT; //12 bit resolution config_adc.divide_result = ADC_DIVIDE_RESULT_DISABLE; //Don't divide result register after accumulation config_adc.positive_input = ADC_POSITIVE_INPUT_PIN10; //voltage positive input on pin 10: PA05 config_adc.negative_input = ADC_NEGATIVE_INPUT_PIN7; //voltage negative input as internal ground : PA06 //! [setup_set_config] adc_init(&adc_instance, ADC, &config_adc); //! [setup_set_config] //! [setup_enable] adc_enable(&adc_instance); //! [setup_enable] }
/** * \internal * \brief Test for ADC initialization. * * This test initializes the ADC module and checks whether the * initialization is successful or not. * * If this test fails no other tests will run. * * \param test Current test case. */ static void run_adc_init_test(const struct test_case *test) { enum status_code status = STATUS_ERR_IO; /* Structure for ADC configuration */ struct adc_config config; adc_get_config_defaults(&config); config.positive_input = ADC_POSITIVE_INPUT_PIN2; config.negative_input = ADC_NEGATIVE_INPUT_GND; config.reference = ADC_REFERENCE_INT1V; config.clock_source = GCLK_GENERATOR_3; config.gain_factor = ADC_GAIN_FACTOR_1X; /* Initialize the ADC */ status = adc_init(&adc_inst, ADC, &config); /* Check for successful initialization */ test_assert_true(test, status == STATUS_OK, "ADC initialization failed"); /* Enable the ADC */ status = adc_enable(&adc_inst); /* Check for successful enable */ test_assert_true(test, status == STATUS_OK, "ADC enabling failed"); if (status == STATUS_OK) { adc_init_success = true; } }
/** * \internal * \brief Setup Function: ADC window mode test. * * This function initializes the ADC in window mode. * Upper and lower threshold values are provided. * It also registers & enables callback for window detection. * * \param test Current test case. */ static void setup_adc_window_mode_test(const struct test_case *test) { enum status_code status = STATUS_ERR_IO; interrupt_flag = false; /* Set 0.5V DAC output */ dac_chan_write(&dac_inst, DAC_CHANNEL_0, DAC_VAL_HALF_VOLT); delay_ms(1); /* Skip test if ADC initialization failed */ test_assert_true(test, adc_init_success, "Skipping test due to failed initialization"); /* Disable ADC before initialization */ adc_disable(&adc_inst); struct adc_config config; adc_get_config_defaults(&config); config.positive_input = ADC_POSITIVE_INPUT_PIN2; config.negative_input = ADC_NEGATIVE_INPUT_GND; #if (SAML21) config.reference = ADC_REFERENCE_INTREF; config.clock_prescaler = ADC_CLOCK_PRESCALER_DIV16; #else config.reference = ADC_REFERENCE_INT1V; #endif config.clock_source = GCLK_GENERATOR_3; #if !(SAML21) config.gain_factor = ADC_GAIN_FACTOR_1X; #endif config.resolution = ADC_RESOLUTION_12BIT; config.freerunning = true; config.window.window_mode = ADC_WINDOW_MODE_BETWEEN_INVERTED; config.window.window_lower_value = (ADC_VAL_DAC_HALF_OUTPUT - ADC_OFFSET); config.window.window_upper_value = (ADC_VAL_DAC_HALF_OUTPUT + ADC_OFFSET); /* Re-initialize & enable ADC */ status = adc_init(&adc_inst, ADC, &config); test_assert_true(test, status == STATUS_OK, "ADC initialization failed"); status = adc_enable(&adc_inst); test_assert_true(test, status == STATUS_OK, "ADC enabling failed"); /* Register and enable window mode callback */ adc_register_callback(&adc_inst, adc_user_callback, ADC_CALLBACK_WINDOW); adc_enable_callback(&adc_inst, ADC_CALLBACK_WINDOW); /* Start ADC conversion */ adc_start_conversion(&adc_inst); }
/** * \brief Application entry point. * * \return Unused (ANSI-C compatibility). */ int main(void) { int32_t ul_vol; int32_t ul_temp; /* Initialize the SAM system. */ sysclk_init(); board_init(); configure_console(); /* Output example information. */ puts(STRING_HEADER); adc_enable(); struct adc_config adc_cfg; adc_get_config_defaults(&adc_cfg); adc_init(ADC, &adc_cfg); adc_channel_enable(ADC,ADC_TEMPERATURE_SENSOR); adc_set_trigger(ADC, ADC_TRIG_SW); struct adc_temp_sensor_config adc_temp_sensor_cfg; adc_temp_sensor_get_config_defaults(&adc_temp_sensor_cfg); adc_temp_sensor_set_config(ADC, &adc_temp_sensor_cfg); adc_set_callback(ADC, ADC_INTERRUPT_EOC_16, adc_temp_sensor_end_conversion, 1); adc_start_software_conversion(ADC); while (1) { if (is_conversion_done == true) { is_conversion_done = false; ul_vol = g_ul_value * VOLT_REF / MAX_DIGITAL; /* * According to datasheet, The output voltage VT = 1.44V at 27C * and the temperature slope dVT/dT = 4.7 mV/C */ ul_temp = (ul_vol - 1440) * 100 / 470 + 27; printf("-- Temperature is: %4d\r\n", (int)ul_temp); delay_ms(1000); adc_start_software_conversion(ADC); } } }
void adp_example_adc_init(void) { struct adc_config config_adc; adc_get_config_defaults(&config_adc); #if (SAMD21) config_adc.positive_input = ADC_POSITIVE_INPUT_PIN8; #else config_adc.positive_input = ADC_POSITIVE_INPUT_PIN0; #endif config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV64; config_adc.reference = ADC_REFERENCE_INTVCC0; adc_init(&adc_instance, EXT1_ADC_MODULE, &config_adc); adc_enable(&adc_instance); }
void configure_adc(void) { struct adc_config conf_adc; adc_get_config_defaults(&conf_adc); conf_adc.clock_source = GCLK_GENERATOR_1; conf_adc.reference = ADC_REFERENCE_INTVCC1; conf_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV16; conf_adc.positive_input = ADC_POSITIVE_INPUT_PIN0; conf_adc.negative_input = ADC_NEGATIVE_INPUT_GND; adc_init(&adc_instance, ADC, &conf_adc); adc_enable(&adc_instance); }
/** * \brief Configure the ADC for the light sensor. */ static void configure_adc(void) { struct adc_config adc_cfg; /* Configure ADC pin for light sensor. */ gpio_configure_pin(LIGHT_SENSOR_GPIO, LIGHT_SENSOR_FLAGS); /* Enable ADC clock. */ pmc_enable_periph_clk(ID_ADC); /* Configure ADC. */ adc_enable(); adc_get_config_defaults(&adc_cfg); adc_init(ADC, &adc_cfg); adc_channel_enable(ADC, ADC_CHANNEL_0); adc_set_trigger(ADC, ADC_TRIG_SW); }
// Configure the light sensor port as an input void configure_ADC(void) { struct adc_config config_adc; adc_get_config_defaults(&config_adc); config_adc.reference = ADC_REFERENCE_INTVCC0; config_adc.resolution = ADC_RESOLUTION_CUSTOM; config_adc.accumulate_samples = ADC_ACCUMULATE_SAMPLES_1024; config_adc.divide_result = ADC_DIVIDE_RESULT_16; config_adc.differential_mode = DISABLE; config_adc.negative_input = ADC_NEGATIVE_INPUT_GND; config_adc.positive_input = ADC_POSITIVE_INPUT_PIN1; config_adc.freerunning = DISABLE; config_adc.run_in_standby = ENABLE; config_adc.left_adjust = false; adc_init(&adc1, ADC, &config_adc); adc_enable(&adc1); }
//! [setup] void configure_adc(void) { //! [setup_config] struct adc_config config_adc; //! [setup_config] //! [setup_config_defaults] adc_get_config_defaults(&config_adc); //! [setup_config_defaults] //! [setup_set_config] adc_init(&adc_instance, ADC, &config_adc); //! [setup_set_config] //! [setup_enable] adc_enable(&adc_instance); //! [setup_enable] }
void configure_adc_temp(void) { struct adc_config conf_adc; adc_get_config_defaults(&conf_adc); conf_adc.clock_source = GCLK_GENERATOR_1; conf_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV16; conf_adc.reference = ADC_REFERENCE_INT1V; conf_adc.positive_input = ADC_POSITIVE_INPUT_TEMP; conf_adc.negative_input = ADC_NEGATIVE_INPUT_GND; conf_adc.sample_length = ADC_TEMP_SAMPLE_LENGTH; adc_init(&adc_instance, ADC, &conf_adc); ADC->AVGCTRL.reg = ADC_AVGCTRL_ADJRES(2) | ADC_AVGCTRL_SAMPLENUM_4; adc_enable(&adc_instance); }
/** * \brief Application entry point. * * \return Unused (ANSI-C compatibility). */ int main(void) { int32_t ul_vol; /* Initialize the SAM system. */ sysclk_init(); board_init(); configure_console(); /* Output example information. */ puts(STRING_HEADER); adc_enable(); struct adc_config adc_cfg; adc_get_config_defaults(&adc_cfg); adc_init(ADC, &adc_cfg); adc_channel_enable(ADC, ADC_CHANNEL_11); adc_set_trigger(ADC, ADC_TRIG_TIO_CH_0); adc_set_callback(ADC, ADC_INTERRUPT_EOC_11, adc_end_conversion, 1); set_adc_resolution(); adc_start_calibration(ADC); /* Configure TC */ configure_tc_trigger(); while (1) { /* Check if ADC sample is done. */ if (is_conversion_done == true) { ul_vol = g_ul_value * VOLT_REF / g_max_digital; printf("-- Voltage is: %4dmv\r\n", (int)ul_vol); is_conversion_done = false; } } }
/** * \brief Configure and enable ADC */ void configure_adc(void) { /* ADC configuration set up */ struct adc_config config_adc; /* Get default ADC configuration */ adc_get_config_defaults(&config_adc); config_adc.gain_factor = ADC_GAIN_FACTOR_DIV2; config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV64; config_adc.reference = ADC_REFERENCE_INTVCC1; config_adc.positive_input = ADC_POSITIVE_INPUT_PIN0; config_adc.resolution = ADC_RESOLUTION_8BIT; config_adc.event_action = ADC_EVENT_ACTION_START_CONV; /* Initialize ADC */ adc_init(&adc_instance, ADC, &config_adc); /* Enable ADC module */ adc_enable(&adc_instance); }
//! [setup] static void configure_adc(void) { //! [setup_config] struct adc_config config_adc; //! [setup_config] //! [setup_config_defaults] adc_get_config_defaults(&config_adc); //! [setup_config_defaults] //! [setup_config_ch] config_adc.input_channel = CONF_ADC_INPUT_CH; //! [setup_config_ch] //! [setup_set_config] adc_init(&config_adc); //! [setup_set_config] //! [setup_enable] adc_enable(); //! [setup_enable] }