功能简介 ADC支持电压监测功能,在需要监测通道电压时,可参考本例进行设计。
本例将固定监控普通通道组的Channel5,监控阈值为0~Vref+/3。
硬件环境: 对应产品型号的AT-START BOARD
PA4——3.3V
PA5——0V
PA6——1.5V左右
配置流程
配置ADC使用的GPIO
配置用于普通通道数据传输的DMA
ADC相关配置及电压监测设定
普通通道软触发
获取转换数据
2) 代码介绍
GPIO配置函数代码
static void gpio_config(void)
{
gpio_init_type gpio_initstructure;
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
gpio_default_para_init(&gpio_initstructure);
/* config adc pin as analog input mode */
gpio_initstructure.gpio_mode = GPIO_MODE_ANALOG;
gpio_initstructure.gpio_pins = GPIO_PINS_4 | GPIO_PINS_5 | GPIO_PINS_6;
gpio_init(GPIOA, &gpio_initstructure);
}
DMA配置函数代码
static void dma_config(void)
{
dma_init_type dma_init_struct;
crm_periph_clock_enable(CRM_DMA1_PERIPH_CLOCK, TRUE);
nvic_irq_enable(DMA1_Channel1_IRQn, 0, 0);
dma_reset(DMA1_CHANNEL1);
dma_default_para_init(&dma_init_struct);
dma_init_struct.buffer_size = 3;
dma_init_struct.direction = DMA_DIR_PERIPHERAL_TO_MEMORY;
dma_init_struct.memory_base_addr = (uint32_t)adc1_ordinary_valuetab;
dma_init_struct.memory_data_width = DMA_MEMORY_DATA_WIDTH_HALFWORD;
dma_init_struct.memory_inc_enable = TRUE;
dma_init_struct.peripheral_base_addr = (uint32_t)&(ADC1->odt);
dma_init_struct.peripheral_data_width = DMA_PERIPHERAL_DATA_WIDTH_HALFWORD;
dma_init_struct.peripheral_inc_enable = FALSE;
dma_init_struct.priority = DMA_PRIORITY_HIGH;
dma_init_struct.loop_mode_enable = TRUE;
dma_init(DMA1_CHANNEL1, &dma_init_struct);
dmamux_enable(DMA1, TRUE);
dmamux_init(DMA1MUX_CHANNEL1, DMAMUX_DMAREQ_ID_ADC1);
/* enable dma transfer complete interrupt */
dma_interrupt_enable(DMA1_CHANNEL1, DMA_FDT_INT, FALSE);
dma_channel_enable(DMA1_CHANNEL1, TRUE);
}
ADC配置函数代码static void adc_config(void)
{
adc_common_config_type adc_common_struct;
adc_base_config_type adc_base_struct;
crm_periph_clock_enable(CRM_ADC1_PERIPH_CLOCK, TRUE);
nvic_irq_enable(ADC1_2_3_IRQn, 0, 0);
adc_common_default_para_init(&adc_common_struct);
/* config combine mode */
adc_common_struct.combine_mode = ADC_INDEPENDENT_MODE;
/* config division,adcclk is division by hclk */
adc_common_struct.div = ADC_HCLK_DIV_4;
/* config common dma mode,it's not useful in independent mode */
adc_common_struct.common_dma_mode = ADC_COMMON_DMAMODE_DISABLE;
/* config common dma request repeat */
adc_common_struct.common_dma_request_repeat_state = FALSE;
/* config adjacent adc sampling interval,it's useful for ordinary shifting mode */
adc_common_struct.sampling_interval = ADC_SAMPLING_INTERVAL_5CYCLES;
/* config inner temperature sensor and vintrv */
adc_common_struct.tempervintrv_state = FALSE;
/* config voltage battery */
adc_common_struct.vbat_state = FALSE;
adc_common_config(&adc_common_struct);
adc_base_default_para_init(&adc_base_struct);
adc_base_struct.sequence_mode = TRUE;
adc_base_struct.repeat_mode = FALSE;
adc_base_struct.data_align = ADC_RIGHT_ALIGNMENT;
adc_base_struct.ordinary_channel_length = 3;
adc_base_config(ADC1, &adc_base_struct);
adc_resolution_set(ADC1, ADC_RESOLUTION_12B);
/* config ordinary channel */
adc_ordinary_channel_set(ADC1, ADC_CHANNEL_4, 1, ADC_SAMPLETIME_47_5);
adc_ordinary_channel_set(ADC1, ADC_CHANNEL_5, 2, ADC_SAMPLETIME_47_5);
adc_ordinary_channel_set(ADC1, ADC_CHANNEL_6, 3, ADC_SAMPLETIME_47_5);
/* config ordinary trigger source and trigger edge */
adc_ordinary_conversion_trigger_set(ADC1, ADC_ORDINARY_TRIG_TMR1CH1,
ADC_ORDINARY_TRIG_EDGE_NONE);
/* config dma mode,it's not useful when common dma mode is use */
adc_dma_mode_enable(ADC1, TRUE);
/* config dma request repeat,it's not useful when common dma mode is use */
adc_dma_request_repeat_enable(ADC1, TRUE);
/* config voltage_monitoring */
adc_voltage_monitor_threshold_value_set(ADC1, 0x100, 0x000);
adc_voltage_monitor_single_channel_select(ADC1, ADC_CHANNEL_5);
adc_voltage_monitor_enable(ADC1, ADC_VMONITOR_SINGLE_ORDINARY);
/* enable adc overflow interrupt */
adc_interrupt_enable(ADC1, ADC_OCCO_INT, TRUE);
/* enable voltage monitoring out of range interrupt */
adc_interrupt_enable(ADC1, ADC_VMOR_INT, TRUE);
/* adc enable */
adc_enable(ADC1, TRUE);
while(adc_flag_get(ADC1, ADC_RDY_FLAG) == RESET);
/* adc calibration */
adc_calibration_init(ADC1);
while(adc_calibration_init_status_get(ADC1));
adc_calibration_start(ADC1);
while(adc_calibration_status_get(ADC1));
}
中断服务函数代码
/* 溢出及电压超出范围监测 */
void ADC1_2_3_IRQHandler(void)
{
if(adc_flag_get(ADC1, ADC_OCCO_FLAG) != RESET)
{
adc_flag_clear(ADC1, ADC_OCCO_FLAG);
adc1_overflow_flag++;
}
if(adc_flag_get(ADC1, ADC_VMOR_FLAG) != RESET)
{
adc_flag_clear(ADC1, ADC_VMOR_FLAG);
vmor_flag_index++;
}
}
main函数代码
int main(void)
{
__IO uint32_t index = 0;
nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
/* config the system clock */
system_clock_config();
/* init at start board */
at32_board_init();
at32_led_off(LED2);
at32_led_off(LED3);
at32_led_off(LED4);
usart1_config(115200);
gpio_config();
dma_config();
adc_config();
printf("voltage_monitoring \r\n");
while(1)
{
at32_led_toggle(LED2);
delay_sec(1);
if((adc1_overflow_flag != 0) || (vmor_flag_index != 0))
{
/* printf flag when error occur */
at32_led_on(LED3);
at32_led_on(LED4);
printf("error occur\r\n");
printf("vmor_flag_index = %d\r\n",vmor_flag_index);
printf("adc1_overflow_flag = %d\r\n",adc1_overflow_flag);
printf("out of range:adc1_channel_5 value is = 0x%x!\r\n", adc1_ordinary_valuetab[1]);
}
adc_ordinary_software_trigger_enable(ADC1, TRUE);
}
}
实验效果
可通过串口打印查看实现效果,测试过程中随机将Channel5外接1.5V,最终串口打印信息如下。
通过打印信息可以看到:有实际监测到Channel5超过监控阈值,且记录到的实际监控值为1.5V。
串口配置
Baud rate:115200
Stop bits:1
Data bits:8
Parity:None
ADC电压监测实验结果
|