STM32F407+ADC多通道+DMA配置
STM32F407+ADC多通道+DMAADC配置读取数据单缓冲模式的ADC+DMA操作,代码测试通过ADC配置#define ADC_CHANNEL_SIZE4unsigned short m_ADCValue[ADC_CHANNEL_SIZE];void ADC_Config_Conver(void){DMA_InitTypeDef...
·
单缓冲模式的ADC+DMA操作,代码测试通过
2020.05.07更新,之前的版本有前后不一致的地方已经全部更正
407主频 168MHZ 该实例采集了8路AD数据,每一路获取100组数据,然后进行平均处理,当然也可以加入卡尔曼滤波,采集会更准确,ADC采集的卡尔曼滤波网上有很多。
基础配置
GPIO对应表
PA0 ——> ADC123_IN0
PA1 ——> ADC123_IN1
PB0 ——> ADC12_IN8
PB1 ——> ADC12_IN9
PC1 ——> ADC12_IN11
PC2 ——> ADC123_IN12
PC4 ——> ADC12_IN14
PC5 ——> ADC12_IN15
void RCC_Configuration()
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC
|RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOF
|RCC_AHB1Periph_CRC | RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_DMA1
, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG | RCC_APB1Periph_SPI2 | RCC_APB1Periph_SPI3
|RCC_APB1Periph_USART2 |RCC_APB1Periph_USART3| RCC_APB1Periph_TIM2 ,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_ADC1| RCC_APB2Periph_TIM9
| RCC_APB2Periph_SPI1 | RCC_APB2Periph_SYSCFG| RCC_APB1Periph_USART2 | RCC_APB2Periph_USART6 ,ENABLE);
}
void GPIO_Config(void)
{
//AD Sample-----------------------------------------------
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1| GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
ADC配置
#define ADC_SAMPLE_PNUM 100//AD 采样点数数 100
#define ADC_SAMPLE_CNUM 8//AD 采样通道数
volatile unsigned short m_ADCValue[ADC_SAMPLE_PNUM][ADC_SAMPLE_CNUM] = {0};
void ADC_Config_Conver(void)
{
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
DMA_Cmd(DMA2_Stream0, DISABLE);
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (unsigned int)&(ADC1->DR);
DMA_InitStructure.DMA_Memory0BaseAddr = (unsigned int)&m_ADCValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = ADC_SAMPLE_PNUM*ADC_SAMPLE_CNUM;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
// //双缓冲模式设置
// DMA_DoubleBufferModeConfig(DMA2_Stream0,(uint32_t)(u16 *)(m_ADCValue1),DMA_Memory_0); //DMA_Memory_0首先被传输
// DMA_DoubleBufferModeCmd(DMA2_Stream0,ENABLE);
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div8;//预分频
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;//采样间隔
ADC_CommonInit(&ADC_CommonInitStructure);
/* ADC1 Init ****************************************************************/
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_ExternalTrigConv = 0;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = ADC_SAMPLE_CNUM;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
DMA_Cmd(DMA2_Stream0, ENABLE);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 3, ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 4, ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_15,5,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_14,6, ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_11,7, ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_12,8, ADC_SampleTime_480Cycles);
ADC_SoftwareStartConv(ADC1);
}
读取数据
u16 ADC_Proc_Value(unsigned char nIndex)
{
u32 nValue = 0;
u32 nSum = 0;
u8 i = 0, j = 0;
u16 Temp = 0;
u16 tempArray[ADC_SAMPLE_PNUM] = {0};
for (i=0; i<ADC_SAMPLE_PNUM; i++) //采集10组
{
tempArray[i] = m_ADCValue[i][nIndex];
// printf("\t\t\t******ADCValue:%X\r\n",tempArray[i]);
}
for(i=0; i<ADC_SAMPLE_PNUM-1; i++) //去掉最大值
{
for(j = i+1; j< ADC_SAMPLE_PNUM ;j++){
if(tempArray[j] < tempArray[i])
{
Temp = tempArray[i];
tempArray[i] = tempArray[j];
tempArray[j] = Temp;
}
}
}
for(i=10; i<ADC_SAMPLE_PNUM-10; i++) //取平均值 debug
{
nSum += tempArray[i] ;
}
nValue = nSum / (ADC_SAMPLE_PNUM-20);//debug
return nValue;
}
int main(void)
{
int sum;
u8 i,j;
RCC_Configuration();
GPIO_Config();
float ADC_Value[ADC_SAMPLE_CNUM];//用来保存经过转换得到的电压值
ADC_Config_Conver();
while(1)
{
for(i=0; i<ADC_SAMPLE_CNUM ; i++)
{
ADC_Value[i]=ADC_Proc_Value(i);//求平均值并转换成电压值
ADC_Value[i]=(float)(ADC_Value[i]/4096)*3.3;//求平均值并转换成电压值
printf("ADC_Value[d%]:%.2f\r\n", i, ADC_Value[i]);
}
}
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献1条内容
所有评论(0)