从407的手册描述来看,采样率可以通过两种方式来改变,一种是改变那个采样的时钟周期数,一种是改变ADC的时钟预分频系数,但是我在代码里做测试,采样时钟周期数不变的情况下,预分频系数设置为4和8的两种情况下,采集1分钟得到的数据一样多,不知道怎么回事了,请大家帮忙分析一下,下面是我的初始化代码,数据主要是ADC3的DMA数据,ADC1可以先不考虑:
void ADC_Configuration(void)
{
GPIO_InitTypeDef s_GPIO_Init;
ADC_InitTypeDef s_ADC_Init;
ADC_CommonInitTypeDef s_ADC_cInit;
DMA_InitTypeDef s_DMA_Init;
NVIC_InitTypeDef s_NVIC_Init;
//************************ADC功能公共配置*********************************//
//时钟配置
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC |
RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOG |
RCC_AHB1Periph_GPIOH | RCC_AHB1Periph_GPIOI, ENABLE);
//***************配置ADC公共工作时钟和工作模式**********************************//
s_ADC_cInit.ADC_Mode = ADC_Mode_Independent;
s_ADC_cInit.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_8Cycles;
s_ADC_cInit.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
s_ADC_cInit.ADC_Prescaler = ADC_Prescaler_Div4;
ADC_CommonInit(&s_ADC_cInit);
//************************ADC1初始化**************************//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
//设置ADC1通道5(PA.5)的GPIO功能
s_GPIO_Init.GPIO_Pin = GPIO_Pin_5;
s_GPIO_Init.GPIO_Mode = GPIO_Mode_AN;
s_GPIO_Init.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &s_GPIO_Init);
//设置ADC1通道10(PC.0)的GPIO功能
s_GPIO_Init.GPIO_Pin = GPIO_Pin_0;
s_GPIO_Init.GPIO_Mode = GPIO_Mode_AN;
s_GPIO_Init.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &s_GPIO_Init);
//配置ADC1使用参数:8比特1通道单次规则转换
s_ADC_Init.ADC_Resolution = ADC_Resolution_12b;
s_ADC_Init.ADC_ScanConvMode = ENABLE;
s_ADC_Init.ADC_ContinuousConvMode = DISABLE;
s_ADC_Init.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
s_ADC_Init.ADC_DataAlign = ADC_DataAlign_Right;
s_ADC_Init.ADC_NbrOfConversion = 1; //规则通道数为1
ADC_Init(ADC1, &s_ADC_Init);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
//使能温度传感器
ADC_TempSensorVrefintCmd(ENABLE);
//******************ADC3初始化:使用2通道规则采样DMA*******************//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);
//设置ADC3通道12(PC2)的GPIO功能
s_GPIO_Init.GPIO_Pin = GPIO_Pin_2;
s_GPIO_Init.GPIO_Mode = GPIO_Mode_AN;
s_GPIO_Init.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &s_GPIO_Init);
//设置ADC3通道13(PC3)的GPIO功能
s_GPIO_Init.GPIO_Pin = GPIO_Pin_3;
s_GPIO_Init.GPIO_Mode = GPIO_Mode_AN;
s_GPIO_Init.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &s_GPIO_Init);
//配置ADC3使用参数:8比特2通道连续规则转换
s_ADC_Init.ADC_Resolution = ADC_Resolution_8b;
s_ADC_Init.ADC_ScanConvMode = ENABLE;
s_ADC_Init.ADC_ContinuousConvMode = ENABLE;
s_ADC_Init.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
s_ADC_Init.ADC_DataAlign = ADC_DataAlign_Right;
s_ADC_Init.ADC_NbrOfConversion = 2; //规则通道数为2
ADC_Init(ADC3, &s_ADC_Init);
ADC_RegularChannelConfig(ADC3, ADC_Channel_12, 1, ADC_SampleTime_84Cycles); //采样率57k
ADC_RegularChannelConfig(ADC3, ADC_Channel_13, 2, ADC_SampleTime_84Cycles); //采样率57k
//配置ADC3使用的DMA功能
MA-2/stream-0/channel-2
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
s_DMA_Init.DMA_Channel = DMA_Channel_2;
s_DMA_Init.DMA_PeripheralBaseAddr = ADC3_DR_ADDRESS;
s_DMA_Init.DMA_Memory0BaseAddr = (u32)&ADC_Buff[0];
s_DMA_Init.DMA_DIR = DMA_DIR_PeripheralToMemory;
s_DMA_Init.DMA_BufferSize = ABUF_DEEP;
s_DMA_Init.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
s_DMA_Init.DMA_MemoryInc = DMA_MemoryInc_Enable;
s_DMA_Init.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
s_DMA_Init.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
s_DMA_Init.DMA_Mode = DMA_Mode_Circular;
s_DMA_Init.DMA_Priority = DMA_Priority_High;
s_DMA_Init.DMA_FIFOMode = DMA_FIFOMode_Enable;
s_DMA_Init.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
s_DMA_Init.DMA_MemoryBurst = DMA_MemoryBurst_Single;
s_DMA_Init.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_ClearFlag(DMA2_Stream0,DMA_FLAG_HTIF0); //清传输一半标志
DMA_ClearFlag(DMA2_Stream0,DMA_FLAG_TCIF0); //清传输完成标志
DMA_ClearITPendingBit(DMA2_Stream0,DMA_IT_HTIF0);
DMA_ClearITPendingBit(DMA2_Stream0,DMA_IT_TCIF0);
DMA_ITConfig(DMA2_Stream0,DMA_IT_HT,ENABLE); //使能半满/全满传输中断
DMA_ITConfig(DMA2_Stream0,DMA_IT_TC,ENABLE);
DMA_Init(DMA2_Stream0, &s_DMA_Init);
DMA_Cmd(DMA2_Stream0, ENABLE); //使能DAM
/* Enable ADC3 DMA */
ADC_DMACmd(ADC3, ENABLE);
/* Enable ADC3 */
ADC_Cmd(ADC3, ENABLE);
ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE);
//使能ADC3的DMA中断
s_NVIC_Init.NVIC_IRQChannel = DMA2_Stream0_IRQn;
s_NVIC_Init.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级0
s_NVIC_Init.NVIC_IRQChannelSubPriority = 0; //相应优先级0
s_NVIC_Init.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&s_NVIC_Init);
}
一周热门 更多>