#申请原创# #申请开发板#
单片机的ADC(模数转换器)在嵌入式系统中起着至关重要的作用。ADC将模拟信号转换为数字信号,为后续处理提供基础数据。在单片机系统中,ADC常常用于采集外部环境的变化,如温度、湿度、光照等。同时,ADC还可用于数据采集、控制系统的操作等方面。因此,ADC的准确性和稳定性对单片机系统的性能至关重要.
总之,ADC在单片机系统中扮演着重要的角色,其准确性和稳定性对于整个系统的性能至关重要。因此,在选择和使用ADC时,需要考虑到其精度、稳定性、功耗等因素,并结合具体应用场景进行选择和优化。
本次要做的也就是ADC采样并通过串口上送数据,查看起精度。代码如下:
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_IRQn, 0, 0);
crm_adc_clock_select(CRM_ADC_CLOCK_SOURCE_HCLK);
adc_common_default_para_init(&adc_common_struct);
/* config division,adcclk is division by hclk */
adc_common_struct.div = ADC_HCLK_DIV_4;
/* config inner temperature sensor and vintrv */
adc_common_struct.tempervintrv_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_640_5);
adc_ordinary_channel_set(ADC1, ADC_CHANNEL_5, 2, ADC_SAMPLETIME_640_5);
adc_ordinary_channel_set(ADC1, ADC_CHANNEL_6, 3, ADC_SAMPLETIME_640_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, FALSE);
/* config dma request repeat,it's not useful when common dma mode is use */
adc_dma_request_repeat_enable(ADC1, FALSE);
/* each ordinary channel conversion set occe flag */
adc_occe_each_conversion_enable(ADC1, TRUE);
/* enable adc overflow interrupt */
adc_interrupt_enable(ADC1, ADC_OCCO_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 uart_print_init(uint32_t baudrate)
{ gpio_init_type gpio_init_struct;#if defined (__GNUC__) && !defined (__clang__) setvbuf(stdout, NULL, _IONBF, 0);#endif /* enable the uart and gpio clock */ crm_periph_clock_enable(PRINT_UART_CRM_CLK, TRUE); crm_periph_clock_enable(PRINT_UART_TX_GPIO_CRM_CLK, TRUE); gpio_default_para_init(&gpio_init_struct); /* configure the uart tx pin */ gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER; gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; gpio_init_struct.gpio_mode = GPIO_MODE_MUX; gpio_init_struct.gpio_pins = PRINT_UART_TX_PIN; gpio_init_struct.gpio_pull = GPIO_PULL_NONE; gpio_init(PRINT_UART_TX_GPIO, &gpio_init_struct); gpio_pin_mux_config(PRINT_UART_TX_GPIO, PRINT_UART_TX_PIN_SOURCE, PRINT_UART_TX_PIN_MUX_NUM); /* configure uart param */ usart_init(PRINT_UART, baudrate, USART_DATA_8BITS, USART_STOP_1_BIT); usart_transmitter_enable(PRINT_UART, TRUE); usart_enable(PRINT_UART, TRUE);}
在main函数打印输出:
printf("AT32F423VCT7的adc测试:\r\n"); printf("通道1 = %6.2fV\r\n", (float)adc1_ordinary_valuetab[0]*3.3f/4096.0f); printf("通道2 = %6.2fV\r\n", (float)adc1_ordinary_valuetab[1]*3.3f/4096.0f); printf("通道3 = %6.2fV\r\n", (float)adc1_ordinary_valuetab[2]*3.3f/4096.0f); printf("打印结束\r\n"); delay_sec(1); at32_led_toggle(LED3);
编译仿真打印:如图:是施加的电压
板子:
最后是采样的数据:
上述可以看出采样还是蛮精确的。
|
|