高级会员
- 积分
- 729
- 金钱
- 729
- 注册时间
- 2012-6-21
- 在线时间
- 59 小时
|
楼主 |
发表于 2017-1-11 15:39:07
|
显示全部楼层
static void ADC1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable DMA clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_5|GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
static void ADC1_Mode_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
DMA_InitTypeDef DMA_InitStructure;
//GPIO_InitTypeDef GPIO_InitStructure;
/* ¨º1?¨¹¨ªa¨¦¨¨¨º¡À?¨® */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* DMA2 Stream0 channel0 ????-------------------------------------------------- */
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)&AD_Value[0][0];// (uint32_t)&uhADCConvertedValue;;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = M*N;
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_Disable;
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);
/* DMA2_Stream0 enable */
DMA_Cmd(DMA2_Stream0, ENABLE);
/****************************************************************************
PCLK2 = HCLK / 2
????????¦Ì?¨º?2¡¤??¦Ì
ADCCLK = PCLK2 /8 = HCLK / 8 = 168 / 8 = 21M
ADC2¨¦?¨´?¦Ì?¨º¡êo Sampling Time + Conversion Time = 480 + 12 cycles = 492cyc
Conversion Time = 21MHz / 492cyc = 42.6ksps.
*****************************************************************************/
/* ADC Common ???? ----------------------------------------------------------*/
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
/* ADC1 regular channel 12 configuration ************************************/
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_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = M;
ADC_Init(ADC1, &ADC_InitStructure);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* ADC1 regular channel18 (VBAT) configuration ******************************/
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 2, ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 3, ADC_SampleTime_480Cycles);
/* Enable VBAT channel */
ADC_VBATCmd(ENABLE);
ADC_TempSensorVrefintCmd(ENABLE);
/* Enable DMA request after last transfer (Single-ADC mode) */
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
/* Enable ADC1 **************************************************************/
ADC_Cmd(ADC1, ENABLE);
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConv(ADC1);
}
void ADC1_Init(void)
{
ADC1_GPIO_Config();
ADC1_Mode_Config();
}
u16 MultiDMAGetAdcTest(u8 ch)
{
u8 i;
u16 AdcMax,AdcMin,AdcSum,AdcResult,Adc_temp;
volatile u16 ADC_Data;
ADC_Data=0;
AdcMax=0;
AdcMin=~0;
AdcSum = 0;
Adc_temp = 0;
//taskDISABLE_INTERRUPTS(); /* ¿ªÆôµ÷¶ÈËø */
// taskENTER_CRITICAL();
for(i=0;i<34;i++)
{
Adc_temp =AD_Value[i][ch]&0XFFF;
if(Adc_temp>AdcMax)
{
AdcMax=Adc_temp;
}
if(Adc_temp<AdcMin)
{
AdcMin=Adc_temp;
}
AdcSum+=Adc_temp;
}
AdcResult=(AdcSum-AdcMax-AdcMin)>>5;
After_filter[ch]=(u16)AdcResult;
//taskENABLE_INTERRUPTS();
// taskEXIT_CRITICAL(); /* Í˳öÁÙ½çÇø */
return(After_filter[ch]);
}
|
|