[极海M3内核 APM32E103VET6S MINI开发板]05.ADC高速采样和DAC模拟外设功能
[复制链接]
APM32E103VET6S带有3个采样精度达到12位的ADC单元,每个ADC单元最多有16个外部通道和2个内部通道。其中ADC1和ADC2有16个外部通道、ADC3一般有8个外部通道;每个通道都支持单次、连续、扫描和间断的工作方式,支持左右对齐的数据存储方式。对于ADC来说,最大时钟频率为14MHz,最短的采样周期为14个周期,采样的电压范围由VREF来决定;通过APM32E103VET6S MINI开发板我们来实现一路ADC采样功能,程序配置如下:
/*******************************************************************************
* @brief * @param
* @retval
* @attention *******************************************************************************/
void ADC_MultiTimerCallback(MultiTimer *timer, void *userData)
{
ADC_EnableSoftwareStartConv(ADC1);
while(ADC_ReadSoftwareStartConvStatus(ADC1) != BIT_RESET);
ADC_ClearStatusFlag(ADC1, ADC_FLAG_EOC);
uint32_t Value = ADC_ReadConversionValue(ADC1);
printf("\r\nADC Voltage : %0.1fv", (float)Value / 4096.0 * 3.3);
MultiTimerStart(&ADC_MultiTimer, 1000, ADC_MultiTimerCallback, "ADC");
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void ADC_Init(void)
{
ADC_Config_T ADC_ConfigStruct;
GPIO_Config_T GPIO_ConfigStruct;
RCM_ConfigADCCLK(RCM_PCLK2_DIV_4); /* ADCCLK = PCLK2/4 */
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_ADC1 );
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA);
GPIO_ConfigStructInit(&GPIO_ConfigStruct);
GPIO_ConfigStruct.pin = GPIO_PIN_2;
GPIO_ConfigStruct.mode = GPIO_MODE_ANALOG;
GPIO_Config(GPIOA, &GPIO_ConfigStruct);
ADC_ConfigStructInit(&ADC_ConfigStruct);
ADC_ConfigStruct.mode = ADC_MODE_INDEPENDENT;
ADC_ConfigStruct.scanConvMode = DISABLE;
ADC_ConfigStruct.continuosConvMode = ENABLE;
ADC_ConfigStruct.externalTrigConv = ADC_EXT_TRIG_CONV_None;
ADC_ConfigStruct.dataAlign = ADC_DATA_ALIGN_RIGHT;
ADC_ConfigStruct.nbrOfChannel = 1;
ADC_Config(ADC1, &ADC_ConfigStruct);
ADC_ConfigRegularChannel(ADC1, ADC_CHANNEL_2, 1, ADC_SAMPLETIME_239CYCLES5);
ADC_Enable(ADC1);
ADC_ResetCalibration(ADC1);
while(ADC_ReadResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_ReadCalibrationStartFlag(ADC1));
MultiTimerStart(&ADC_MultiTimer, 1000, ADC_MultiTimerCallback, "ADC");
}
运行结果:
我们使用的PA2这个引脚做ADC采样通道,当PA2悬空时,采样到的电压为1.6V即:(VREF+)+(VREF-))/2,当PA2接GND时,采样电压为0V,当PA2按VDD时,采样电压为3.3V,如下图所示:
APM32E103VET6S带有2路DAC输出引脚,分别为PA4和PA5,这两路通道互不影响;通道有多个触发源可触发转换,可单路触发转换,也可双路触发转换;另外除了输出固定的电平外,这两个通道还都可以生产噪声波形和三角波形。通过APM32E103VET6S MINI开发板我们来实现2路DAC输出固定电平,然后通过ADC来读取DAC输出的电平电压,打印输出,程序配置如下:
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void DAC_Init(void)
{
DAC_Config_T DAC_ConfigStruct;
GPIO_Config_T GPIO_ConfigStruct;
RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_DAC );
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA);
GPIO_ConfigStructInit(&GPIO_ConfigStruct);
GPIO_ConfigStruct.pin = GPIO_PIN_4 | GPIO_PIN_5;
GPIO_ConfigStruct.mode = GPIO_MODE_ANALOG;
GPIO_Config(GPIOA, &GPIO_ConfigStruct);
DAC_ConfigStructInit(&DAC_ConfigStruct);
DAC_ConfigStruct.trigger = DAC_TRIGGER_NONE;
DAC_ConfigStruct.waveGeneration = DAC_WAVE_GENERATION_NONE;
DAC_ConfigStruct.maskAmplitudeSelect = DAC_TRIANGLE_AMPLITUDE_4095;
DAC_ConfigStruct.outputBuffer = DAC_OUTPUT_BUFFER_DISABLE;
DAC_Config((uint32_t)DAC_CHANNEL_1, &DAC_ConfigStruct);
DAC_Config((uint32_t)DAC_CHANNEL_2, &DAC_ConfigStruct);
DAC_Enable(DAC_CHANNEL_1);
DAC_Enable(DAC_CHANNEL_2);
DAC_ConfigDualChannelData(DAC_ALIGN_12BIT_R, (uint16_t)(4096.0*1.0/3.3), (uint16_t)(4096.0*2.2/3.3));
}
运行结果:
将PA2引脚连接PA4或PA5进行测量(PA2在切换/悬空的时候,采样到的电压为1.6V左右),PA4输出电压为1.0V,PA5输出电压为2.2V,如下图所示:
软件工程源代码:
补充/建议:
对于ADC和DAC的例程,在APM32E10x_SDK_V1.1版本的例程示例都比较少,建议是否可以多补充一些常用的示例程序呢?
|