我尝试修改CH32V003F4U6 的CH32V003EVT\EVT\EXAM\ADC\ADC_DMA示例程序尝试修改成多路数据 直接读取ADC数据
首先测试单路数据A1 A0都是ok的,但是2个ADC同时采集,就会发现A1 通道的电位器会影响A0 ,A0通道读取的数据和A1一样。不知问题所在
#include "debug.h"
void ADC_Function_Init_demo(void)
{
ADC_InitTypeDef ADC_InitStructure = {0};
GPIO_InitTypeDef GPIO_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
// A0 -> PA2 A1 -> PA1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_1; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
*/
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 2;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Calibration_Vol(ADC1, ADC_CALVOL_50PERCENT);
//ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1); //复位校验寄存器
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1)); //等待校验结束
}
u16 Get_ADC_Val_demo(u8 ch)
{
u16 val;
//ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_241Cycles);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
val = ADC_GetConversionValue(ADC1);
return val;
}
/*********************************************************************
* @fn main
*
* @brief Main program.
*
* @return none
*/
int main(void)
{
//u8 i=0;
Delay_Init();
USART_Printf_Init(115200);
printf("SystemClk:%d\r\n",SystemCoreClock);
ADC_Function_Init_demo();
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_241Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_241Cycles);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
Delay_Ms(50);
ADC_SoftwareStartConvCmd(ADC1, DISABLE);
while(1)
{
// printf("A1=%d \r\n", Get_ADC_Val_demo(ADC_Channel_1));
printf("A0=%d A1=%d\r\n", Get_ADC_Val_demo(ADC_Channel_0), Get_ADC_Val_demo(ADC_Channel_1));
// printf("A0=%d A1=%d A2=%d\r\n",Get_ADC_Val_demo(ADC_Channel_0), Get_ADC_Val_demo(ADC_Channel_1), Get_ADC_Val_demo(ADC_Channel_2));
Delay_Ms(100);
}
}