【兆易GD32H759I-EVAL】ADC测试
[复制链接]
测试开发板的ADC功能,使用板卡上的VR1电位器来测试ADC功能。
一、硬件部分
1.1、ADC电位器电路
1.2、VREF+引脚
通过JP76跳冒连接VREF+到VDDA。
1.3、PC2_C在手册中的定义
PC2_C只能用于模拟引脚功能。
二、程序部分
2.1、adc.c
#include "main.h"
__IO uint32_t adc_value[4];
static float val_value;
void init_adc(void)
{
/* enable GPIO clock */
rcu_periph_clock_enable(RCU_GPIOC);
/* enable ADC clock */
rcu_periph_clock_enable(RCU_ADC2);
/* config the GPIO as analog mode */
gpio_mode_set(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_2);
/* reset ADC */
adc_deinit(ADC2);
/* ADC clock config */
adc_clock_config(ADC2, ADC_CLK_SYNC_HCLK_DIV6);
/* ADC scan mode enable */
adc_special_function_config(ADC2, ADC_SCAN_MODE, ENABLE);
/* ADC data alignment config */
adc_data_alignment_config(ADC2, ADC_DATAALIGN_RIGHT);
/* ADC channel length config */
adc_channel_length_config(ADC2, ADC_REGULAR_CHANNEL, 1);
/* ADC regular channel config */
adc_regular_channel_config(ADC2, 0, ADC_CHANNEL_0, 240);
/* ADC trigger config */
adc_external_trigger_config(ADC2, ADC_REGULAR_CHANNEL, EXTERNAL_TRIGGER_DISABLE);
/* enable ADC interface */
adc_enable(ADC2);
/* wait for ADC stability */
delay_1ms(1);
/* ADC calibration mode config */
adc_calibration_mode_config(ADC2, ADC_CALIBRATION_OFFSET);
/* ADC calibration number config */
adc_calibration_number(ADC2, ADC_CALIBRATION_NUM1);
/* ADC calibration and reset calibration */
adc_calibration_enable(ADC2);
}
uint32_t adc_channel_sample(uint8_t channel)
{
/* ADC regular channel config */
adc_regular_channel_config(ADC2, 0U, channel, 240);
/* ADC software trigger enable */
adc_software_trigger_enable(ADC2, ADC_REGULAR_CHANNEL);
/* wait the end of conversion flag */
while(!adc_flag_get(ADC2, ADC_FLAG_EOC));
/* clear the end of conversion flag */
adc_flag_clear(ADC2, ADC_FLAG_EOC);
/* return regular channel sample value */
return (adc_regular_data_read(ADC2));
}
uint32_t adc_readval(void)
{
uint32_t adv=0;
adv = adc_channel_sample(ADC_CHANNEL_0);
return adv;
}
void adc_test(void)
{
delay_1ms(1000);
adc_value[0] = adc_channel_sample(ADC_CHANNEL_0);
val_value = (adc_value[0]*3.3)/4096;
printf(" the data adc_value[0] is %d , var1 voltage : %4.2fV \r\n", adc_value[0], val_value);
}
2.2、adc.h
#ifndef _ADC_H
#define _ADC_H
void init_adc(void);
void adc_test(void);
uint32_t adc_readval(void);
#endif
2.3、main.c
#include "main.h"
void cache_enable(void);
int main(void)
{
cache_enable();
systick_config();
init_usart(115200);
init_adc();
while(1)
{
adc_test();
}
}
void cache_enable(void)
{
/* enable i-cache */
SCB_EnableICache();
/* enable d-cache */
SCB_EnableDCache();
}
三、运行结果
下载程序,复位开发板,调整电位器VR1,串口输出采集的电压。
四、附件
|