static void adc_setup(void) { rcc_periph_clock_enable(RCC_ADC); rcc_periph_clock_enable(RCC_GPIOA); gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO0); gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO1); adc_power_off(ADC1); adc_set_clk_source(ADC1, ADC_CLKSOURCE_ADC); adc_calibrate_start(ADC1); adc_calibrate_wait_finish(ADC1); adc_set_operation_mode(ADC1, ADC_MODE_SCAN); adc_disable_external_trigger_regular(ADC1); adc_set_right_aligned(ADC1); adc_enable_temperature_sensor(); adc_set_sample_time_on_all_channels(ADC1, ADC_SMPTIME_071DOT5); adc_set_regular_sequence(ADC1, 1, channel_array); adc_set_resolution(ADC1, ADC_RESOLUTION_12BIT); adc_disable_analog_watchdog(ADC1); adc_power_on(ADC1); /* Wait for ADC starting up. */ int i; for (i = 0; i < 800000; i++) { /* Wait a bit. */ __asm__("nop"); } }
static void adc_setup(void) { int i; rcc_periph_clock_enable(RCC_ADC1); /* Make sure the ADC doesn't run during config. */ adc_power_off(ADC1); /* We configure everything for one single timer triggered injected conversion. */ adc_disable_scan_mode(ADC1); adc_set_single_conversion_mode(ADC1); /* We can only use discontinuous mode on either the regular OR injected channels, not both */ adc_disable_discontinuous_mode_regular(ADC1); adc_enable_discontinuous_mode_injected(ADC1); /* We want to start the injected conversion with the TIM2 TRGO */ adc_enable_external_trigger_injected(ADC1,ADC_CR2_JEXTSEL_TIM2_TRGO); adc_set_right_aligned(ADC1); /* We want to read the temperature sensor, so we have to enable it. */ adc_enable_temperature_sensor(ADC1); adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); adc_power_on(ADC1); /* Wait for ADC starting up. */ for (i = 0; i < 800000; i++) /* Wait a bit. */ __asm__("nop"); adc_reset_calibration(ADC1); while ((ADC_CR2(ADC1) & ADC_CR2_RSTCAL) != 0); adc_calibration(ADC1); while ((ADC_CR2(ADC1) & ADC_CR2_CAL) != 0); }
static void adc_setup(void) { int i; rcc_periph_clock_enable(RCC_ADC1); /* Make sure the ADC doesn't run during config. */ adc_power_off(ADC1); /* We configure everything for one single conversion. */ adc_disable_scan_mode(ADC1); adc_set_single_conversion_mode(ADC1); adc_disable_external_trigger_regular(ADC1); adc_set_right_aligned(ADC1); /* We want to read the temperature sensor, so we have to enable it. */ adc_enable_temperature_sensor(ADC1); adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); adc_power_on(ADC1); /* Wait for ADC starting up. */ for (i = 0; i < 800000; i++) /* Wait a bit. */ __asm__("nop"); adc_reset_calibration(ADC1); adc_calibration(ADC1); }
static void adc_setup(void) { //ADC rcc_periph_clock_enable(RCC_ADC12); rcc_periph_clock_enable(RCC_GPIOA); //ADC gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO0); gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO1); adc_power_off(ADC1); adc_set_clk_prescale(ADC1, ADC_CCR_CKMODE_DIV2); adc_set_single_conversion_mode(ADC1); adc_disable_external_trigger_regular(ADC1); adc_set_right_aligned(ADC1); /* We want to read the temperature sensor, so we have to enable it. */ adc_enable_temperature_sensor(); adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_61DOT5CYC); uint8_t channel_array[16]; channel_array[0]=16; // Vts (Internal temperature sensor channel_array[0]=1; //ADC1_IN1 (PA0) adc_set_regular_sequence(ADC1, 1, channel_array); adc_set_resolution(ADC1, ADC_CFGR1_RES_12_BIT); adc_power_on(ADC1); /* Wait for ADC starting up. */ int i; for (i = 0; i < 800000; i++) __asm__("nop"); }
static void adc_setup(void) { gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO0); gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO1); /* Make sure the ADC doesn't run during config. */ adc_power_off(ADC1); /* We configure everything for one single conversion. */ adc_disable_scan_mode(ADC1); adc_set_single_conversion_mode(ADC1); adc_disable_external_trigger_regular(ADC1); adc_set_right_aligned(ADC1); adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); adc_power_on(ADC1); /* Wait for ADC starting up. */ int i; for (i = 0; i < 800000; i++) /* Wait a bit. */ __asm__("nop"); adc_reset_calibration(ADC1); adc_calibrate(ADC1); }
static void adc_setup(void) { gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO0); gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO1); adc_power_off(ADC1); adc_disable_scan_mode(ADC1); adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_3CYC); adc_power_on(ADC1); }
/** **************************************************************************************** * @brief ADC sample complete handler **************************************************************************************** */ void app_event_adc_sample_cmp_handler(void) { int bas_average_adc_value = 0; int bas_voltage; uint8_t bas_percentage; int i; ke_evt_clear(1UL << EVENT_ADC_SAMPLE_CMP_ID); // Close ADC and battery monitor battery_monitor_enable(MASK_DISABLE); adc_clock_off(); adc_power_off(); // Calculate average value for(i = 0; i < BASS_SAMPLE_NUMBER; ++i) bas_average_adc_value += usr_env.bas_reg_buf[i]; bas_average_adc_value /= BASS_SAMPLE_NUMBER; // When enable ADC decimation, the adc value should div 4 bas_average_adc_value /= 4; // bas voltage is 4 times bas_voltage = 4 * ADC_RESULT_mV(bas_average_adc_value); // Calculate the percentage of remaining battery if(bas_voltage <= BASS_FULLY_DISCHARGED_VOLTAGE) { bas_percentage = 0; } else if(bas_voltage >= BASS_FULLY_CHARGED_VOLTAGE) { bas_percentage = 100; } else { bas_percentage = (uint8_t)((bas_voltage - BASS_FULLY_DISCHARGED_VOLTAGE) * 100 / (BASS_FULLY_CHARGED_VOLTAGE - BASS_FULLY_DISCHARGED_VOLTAGE)); } QPRINTF("battery:%dmv(%d%%) adc:%d\r\n", bas_voltage, bas_percentage, bas_average_adc_value); // Update the percentage of remaining battery via bluetooth app_bass_batt_level_upd_req(app_bass_env->conhdl, 0, bas_percentage); }
static void adc_setup(void) { int i; rcc_periph_clock_enable(RCC_ADC1); /* Make sure the ADC doesn't run during config. */ adc_power_off(ADC1); /* We configure everything for one single timer triggered injected conversion with interrupt generation. */ /* While not needed for a single channel, try out scan mode which does all channels in one sweep and * generates the interrupt/EOC/JEOC flags set at the end of all channels, not each one. */ adc_enable_scan_mode(ADC1); adc_set_single_conversion_mode(ADC1); /* We want to start the injected conversion with the TIM2 TRGO */ adc_enable_external_trigger_injected(ADC1,ADC_CR2_JEXTSEL_TIM2_TRGO); /* Generate the ADC1_2_IRQ */ adc_enable_eoc_interrupt_injected(ADC1); adc_set_right_aligned(ADC1); /* We want to read the temperature sensor, so we have to enable it. */ adc_enable_temperature_sensor(ADC1); adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); /* Select the channels we want to convert. * 16=temperature_sensor, 17=Vrefint, 13=ADC1, 10=ADC2 */ channel_array[0] = 16; channel_array[1] = 17; channel_array[2] = 13; channel_array[3] = 10; adc_set_injected_sequence(ADC1, 4, channel_array); adc_power_on(ADC1); /* Wait for ADC starting up. */ for (i = 0; i < 800000; i++) /* Wait a bit. */ __asm__("nop"); adc_reset_calibration(ADC1); while ((ADC_CR2(ADC1) & ADC_CR2_RSTCAL) != 0); //added this check adc_calibration(ADC1); while ((ADC_CR2(ADC1) & ADC_CR2_CAL) != 0); //added this check }
/** **************************************************************************************** * @brief ADC sample complete handler **************************************************************************************** */ void app_event_adc_sample_cmp_handler(void) { ke_evt_clear(1UL << EVENT_ADC_SAMPLE_CMP_ID); // CLose ADC and temp sensor temp_sensor_enable(MASK_DISABLE); adc_clock_off(); adc_power_off(); if(usr_env.is_temp_meas_config && usr_env.is_should_indicate) { usr_env.is_should_indicate = false; app_temp_meas_indicate(); } if (usr_env.is_temp_imeas_config) { app_interm_temp_notify(); } }