void adc_Init(void)
{
ADC_Config_T adcConfig;
RCM_ConfigADCCLK(RCM_PCLK2_DIV_4);
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_ADC1);
//ADC_EnableTempSensorVrefint(ADC1);
ADC_Reset(ADC1);
/* ADC1 configuration ------------------------------------------------------*/
ADC_ConfigStructInit(&adcConfig);
adcConfig.mode = ADC_MODE_INDEPENDENT;
adcConfig.scanConvMode = ENABLE;
adcConfig.continuosConvMode = ENABLE;
adcConfig.externalTrigConv = ADC_EXT_TRIG_CONV_None;
adcConfig.dataAlign = ADC_DATA_ALIGN_RIGHT;
adcConfig.nbrOfChannel = 3;
ADC_Config(ADC1, &adcConfig);
/* Enable ADC1 external trigger conversion */
//ADC_EnableExternalTrigConv(ADC1);
// ADC_ExternalTrigConvCmd(ADC1,ENABLE);
/* ADC1 regular channel configuration */
ADC_ConfigRegularChannel(ADC1, ADC_CHANNEL_8, 0, ADC_SAMPLETIME_55CYCLES5);
ADC_ConfigRegularChannel(ADC1, ADC_CHANNEL_9, 1, ADC_SAMPLETIME_55CYCLES5);
ADC_ConfigRegularChannel(ADC1, ADC_CHANNEL_15, 2, ADC_SAMPLETIME_55CYCLES5);
/* Enable ADC1 DMA */
ADC_EnableDMA(ADC1);
/* Enable ADC1 */
ADC_Enable(ADC1);
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_ReadResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_ReadCalibrationStartFlag(ADC1));
/* Start ADC1 Software Conversion */
//ADC_SoftwareStartConvCmd(ADC1, ENABLE); //ADC转换启动最好放在DMA启动之后
}
void dma_Init(void)
{
DMA_Config_T DMA_InitStructure;
RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_DMA1);
/* DMA channel1 configuration ----------------------------------------------*/
DMA_Reset(DMA1_Channel1);
DMA_InitStructure.peripheralBaseAddr = (uint32_t)&ADC1->REGDATA;
DMA_InitStructure.memoryBaseAddr = (uint32_t)ADC_ConvertedValue;
DMA_InitStructure.dir = DMA_DIR_PERIPHERAL_SRC;
DMA_InitStructure.bufferSize = 3;
DMA_InitStructure.peripheralInc = DMA_PERIPHERAL_INC_DISABLE;
DMA_InitStructure.memoryInc = DMA_MEMORY_INC_ENABLE;
DMA_InitStructure.peripheralDataSize = DMA_PERIPHERAL_DATA_SIZE_HALFWORD;
DMA_InitStructure.memoryDataSize = DMA_MEMORY_DATA_SIZE_HALFWORD;
DMA_InitStructure.loopMode = DMA_MODE_CIRCULAR;
DMA_InitStructure.priority = DMA_PRIORITY_HIGH;
DMA_InitStructure.M2M = DMA_M2MEN_DISABLE;
DMA_Config(DMA1_Channel1, &DMA_InitStructure);
DMA_EnableInterrupt(DMA1_Channel1,DMA_INT_TC|DMA_INT_TERR);
/* Enable DMA1 channel1 */
DMA_Enable(DMA1_Channel1);
}
void TMR1_UP_IRQHandler(void)
{
static unsigned DmaTimeOut,DmaFaultCount =0;
if(TMR_ReadIntFlag(TMR1, TMR_FLAG_UPDATE) == SET)
{
TMR_ClearIntFlag(TMR1, TMR_FLAG_UPDATE);
//启动ADC转换,定时器触发或软件触发,当前由TIM2_CC2触发,
ADC_EnableSoftwareStartConv(ADC1);
//等待ADC转换结束以及DMA数据传输完成,由于使用DMA传输,当前判定DMA传输完成
//如果传输出错,选用一个周波前的点填充,连续出错一个周波报错。
while( DMA_ReadStatusFlag(DMA1_FLAG_TC1) !=SET )
{
if(DMA_ReadStatusFlag(DMA1_FLAG_TERR1) ==SET) //传输出错
{
DMA_ClearStatusFlag(DMA1_FLAG_TERR1);
++DmaFaultCount;
}
//超时
++DmaTimeOut;
}
DMA_ClearStatusFlag(DMA1_FLAG_TC1);
adc_ReadSampleData();
}
}
void adc_ReadSampleData(void)
{
int i;
for(i=0; i < 3; i++)
{
SampleData.AIData[i][ADC_SampleIndex] = ADC_ConvertedValue[i];
}
ADC_SampleIndex = ADC_SampleIndex + 1;
if(ADC_SampleIndex >= ADC_SAMPLEBUFF_SIZE)
{
ADC_SampleIndex =0;
}
}
int main(void)
{
SetSysClock();
rcc_init();
gpio_init();
nvic_Init();
dma_Init(); /*配置DMA传输AD转换结果*/
adc_Init();
tim_CreateSampleTimer();
while(1)
{
}
}
|