初中生
最后登录1970-1-1
在线时间 小时
注册时间2024-4-12
|
在程序中使用了ADC1的10、11、12通道,ADC2的13、14、15通道。
问题1、最终测量会使得同一个ADC的各个通道之间的数值相互影响,修改了采样周期也有这个问题
问题2、ADC2通道转换顺序需要按照以下图片所示顺序设置才能使通道和接口对应
通道转换顺序设置
主函数如下:
int main()
{
uint8_t i;
USART_Config();
printf("双ADC双通道DMA读取\n");
Delay(0XFFFFEE);
ADC_init();
while(1)
{
for(i=0;i<CHANNEL_NUM;i++)
{
adcConValue = (float)((ADCValue&0xffff)*3.3)/4096; //ADC1 低16位
adcConValue[i+CHANNEL_NUM] = (float)((ADCValue>>16)*3.3)/4096; //ADC2 高16位
}
printf("ADC1 PC0通道的电压为:%fV\n",adcConValue[0]);
printf("ADC1 PC1通道的电压为:%fV\n",adcConValue[1]);
printf("ADC1 PC2通道的电压为:%fV\n",adcConValue[2]);
printf("ADC2 PC3通道的电压为:%fV\n",adcConValue[3]);
printf("ADC2 PC4通道的电压为:%fV\n",adcConValue[4]);
printf("ADC2 PC5通道的电压为:%fV\n",adcConValue[5]);
printf("\n");
Delay(0xffffee);
};
}
ADC配置如下:
#include "adc.h"
uint32_t ADCValue[CHANNEL_NUM];
static void ADC_mode_config(void)
{
GPIO_InitTypeDef ADC_GPIO_Initstruct;
ADC_InitTypeDef ADC_Initstruct;
DMA_InitTypeDef DMA_InitStruct;
RCC_APB2PeriphClockCmd(ADC_GPIO_CLK,ENABLE);
RCC_APB2PeriphClockCmd(ADC1_CLK,ENABLE);
RCC_APB2PeriphClockCmd(ADC2_CLK,ENABLE);
RCC_AHBPeriphClockCmd(ADC_DMA_CLK,ENABLE);
ADC_GPIO_Initstruct.GPIO_Mode = GPIO_Mode_AIN;
ADC_GPIO_Initstruct.GPIO_Pin = ADC1_GPIO_PIN0;
ADC_GPIO_Initstruct.GPIO_Pin = ADC1_GPIO_PIN1;
ADC_GPIO_Initstruct.GPIO_Pin = ADC1_GPIO_PIN2;
ADC_GPIO_Initstruct.GPIO_Pin = ADC2_GPIO_PIN1;
ADC_GPIO_Initstruct.GPIO_Pin = ADC2_GPIO_PIN2;
ADC_GPIO_Initstruct.GPIO_Pin = ADC2_GPIO_PIN3;
GPIO_Init(ADC_GPIO_PORT,&ADC_GPIO_Initstruct);
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)(&(ADC1_SOURCE->DR));
DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)ADCValue;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStruct.DMA_BufferSize = CHANNEL_NUM;
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
DMA_InitStruct.DMA_Priority = DMA_Priority_High;
DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
DMA_Init(ADC_DMA_CHANNEL, &DMA_InitStruct);
DMA_ClearFlag(ADC_DMA_FLAG_TC);
DMA_Cmd(ADC_DMA_CHANNEL, ENABLE);
ADC_Initstruct.ADC_Mode = ADC_Mode_RegSimult; //规则同步
ADC_Initstruct.ADC_ScanConvMode = ENABLE; //扫描针对多通道
ADC_Initstruct.ADC_ContinuousConvMode = ENABLE;
ADC_Initstruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //软件触发 不使用外部触发
ADC_Initstruct.ADC_DataAlign = ADC_DataAlign_Right;
ADC_Initstruct.ADC_NbrOfChannel = CHANNEL_NUM; //转换通道数目
ADC_Init(ADC1_SOURCE,&ADC_Initstruct);
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //配置adc时钟 APB2分频
ADC_RegularChannelConfig(ADC1_SOURCE,ADC1_CHANNEL0,1,ADC_SampleTime_239Cycles5); //配置通道 转换顺序、时间
ADC_RegularChannelConfig(ADC1_SOURCE,ADC1_CHANNEL1,2,ADC_SampleTime_239Cycles5); //配置通道 转换顺序、时间
ADC_RegularChannelConfig(ADC1_SOURCE,ADC1_CHANNEL2,3,ADC_SampleTime_239Cycles5); //配置通道 转换顺序、时间
ADC_DMACmd(ADC1_SOURCE,ENABLE);//使能ADC_DMA请求
ADC_Init(ADC2_SOURCE,&ADC_Initstruct);
ADC_RegularChannelConfig(ADC2_SOURCE,ADC2_CHANNEL1,3,ADC_SampleTime_239Cycles5); //配置通道 转换顺序、时间
ADC_RegularChannelConfig(ADC2_SOURCE,ADC2_CHANNEL2,1,ADC_SampleTime_239Cycles5); //配置通道 转换顺序、时间
ADC_RegularChannelConfig(ADC2_SOURCE,ADC2_CHANNEL3,2,ADC_SampleTime_239Cycles5); //配置通道 转换顺序、时间
ADC_ExternalTrigConvCmd(ADC2_SOURCE,ENABLE); //把ADC2设置为外部触发 ADC1是主,由adc1的多路触发转换器来
ADC_Cmd(ADC1_SOURCE,ENABLE); // 初始化ADC 校准寄存器
ADC_ResetCalibration(ADC1_SOURCE); // 等待校准寄存器初始化完成
while(ADC_GetResetCalibrationStatus(ADC1_SOURCE));
ADC_StartCalibration(ADC1_SOURCE);//校准ADC
while(!ADC_GetCalibrationStatus(ADC1_SOURCE));//等待校准完成
ADC_Cmd(ADC2_SOURCE,ENABLE);
ADC_ResetCalibration(ADC2_SOURCE);
while(ADC_GetResetCalibrationStatus(ADC2_SOURCE));
ADC_StartCalibration(ADC2_SOURCE);//校准ADC
while(!ADC_GetCalibrationStatus(ADC2_SOURCE));//等待校准完成
ADC_SoftwareStartConvCmd(ADC1_SOURCE,ENABLE);//开始软件触发转换ADC数据
}
void ADC_init(void)
{
ADC_mode_config();
}
ADC-DMA多通道读取.rar
(4.69 MB, 下载次数: 10)
|
|