69 lines
1.9 KiB
C
69 lines
1.9 KiB
C
#include "at32f435_437_board.h"
|
|
|
|
/* App */
|
|
#include "app_adc.h"
|
|
|
|
void app_adc_init(void) {
|
|
crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
|
|
crm_periph_clock_enable(CRM_ADC1_PERIPH_CLOCK, TRUE);
|
|
|
|
gpio_init_type pin_cfg = {
|
|
.gpio_pins = GPIO_PINS_0,
|
|
.gpio_mode = GPIO_MODE_ANALOG,
|
|
.gpio_out_type = GPIO_OUTPUT_PUSH_PULL,
|
|
.gpio_pull = GPIO_PULL_NONE,
|
|
.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER,
|
|
};
|
|
|
|
gpio_init(GPIOB, &pin_cfg);
|
|
|
|
adc_common_config_type adc_cfg = {
|
|
.combine_mode = ADC_INDEPENDENT_MODE,
|
|
.div = ADC_HCLK_DIV_4,
|
|
.common_dma_mode = ADC_COMMON_DMAMODE_DISABLE,
|
|
.common_dma_request_repeat_state = FALSE,
|
|
.sampling_interval = ADC_SAMPLING_INTERVAL_5CYCLES,
|
|
.tempervintrv_state = FALSE,
|
|
.vbat_state = FALSE,
|
|
};
|
|
|
|
adc_common_config(&adc_cfg);
|
|
|
|
adc_base_config_type base_cfg = {
|
|
.sequence_mode = FALSE,
|
|
.repeat_mode = FALSE,
|
|
.data_align = ADC_RIGHT_ALIGNMENT,
|
|
.ordinary_channel_length = 1,
|
|
};
|
|
|
|
adc_base_config(ADC1, &base_cfg);
|
|
|
|
adc_resolution_set(ADC1, ADC_RESOLUTION_12B);
|
|
|
|
adc_ordinary_channel_set(ADC1, ADC_CHANNEL_8, 1, ADC_SAMPLETIME_640_5);
|
|
|
|
adc_enable(ADC1, TRUE);
|
|
|
|
while (adc_flag_get(ADC1, ADC_RDY_FLAG) == RESET) {
|
|
/* -- */
|
|
}
|
|
|
|
adc_calibration_init(ADC1);
|
|
while (adc_calibration_init_status_get(ADC1)) {
|
|
/* -- */
|
|
}
|
|
|
|
adc_calibration_start(ADC1);
|
|
while (adc_calibration_status_get(ADC1)) {
|
|
/* -- */
|
|
}
|
|
}
|
|
|
|
uint16_t app_adc_read(void) {
|
|
adc_ordinary_software_trigger_enable(ADC1, TRUE);
|
|
while (adc_flag_get(ADC1, ADC_OCCE_FLAG) == RESET) {
|
|
/* -- */
|
|
}
|
|
|
|
return adc_ordinary_conversion_data_get(ADC1);
|
|
} |