#define AD_FUEL_PORT (GPIOA)
#define AD_FUEL_PIN (GPIO_PIN_4)
RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_DMA, ENABLE);
RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC, ENABLE);
RCC_ConfigAdc1mClk(RCC_ADC1MCLK_SRC_HSE, RCC_ADC1MCLK_DIV12);
ADC_ConfigClk(ADC_CTRL3_CKMOD_AHB, RCC_ADCHCLK_DIV16);
MY_GPIO_Init(AD_FUEL_PORT,AD_FUEL_PIN,GPIO_MODE_ANALOG,GPIO_NO_PULL,GPIO_SPEED_LOW,GPIO_DC_LOW,GPIO_NO_AF);
void MYADC_Init(void)
{
ADC_InitType ADC_InitStructure;
DMA_InitType DMA_InitStructure;
/* DMA channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA_CH1);
DMA_StructInit(&DMA_InitStructure);
DMA_InitStructure.PeriphAddr = (uint32_t)&ADC->DAT;
DMA_InitStructure.MemAddr = (uint32_t)&ADCConvertedValue[0];
DMA_InitStructure.Direction = DMA_DIR_PERIPH_SRC;
DMA_InitStructure.BufSize = 2;
DMA_InitStructure.PeriphInc = DMA_PERIPH_INC_DISABLE;
DMA_InitStructure.DMA_MemoryInc = DMA_MEM_INC_ENABLE;
DMA_InitStructure.PeriphDataSize = DMA_PERIPH_DATA_SIZE_HALFWORD;
DMA_InitStructure.MemDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.CircularMode = DMA_MODE_CIRCULAR;
DMA_InitStructure.Priority = DMA_PRIORITY_HIGH;
DMA_InitStructure.Mem2Mem = DMA_M2M_DISABLE;
DMA_Init(DMA_CH1, &DMA_InitStructure);
DMA_RequestRemap(DMA_REMAP_ADC, DMA, DMA_CH1, ENABLE);
/* Enable DMA channel1 */
DMA_EnableChannel(DMA_CH1, ENABLE);
/* ADC configuration ------------------------------------------------------*/
ADC_InitStruct(&ADC_InitStructure);
ADC_InitStructure.MultiChEn = ENABLE;
ADC_InitStructure.ContinueConvEn = DISABLE;
ADC_InitStructure.ExtTrigSelect = ADC_EXT_TRIGCONV_NONE;
ADC_InitStructure.DatAlign = ADC_DAT_ALIGN_R;
ADC_InitStructure.ChsNumber = 2;
ADC_Init(ADC, &ADC_InitStructure);
/* ADC regular channel1 configuration */
ADC_ConfigRegularChannel(ADC, ADC_CH_4_PA4, 1, ADC_SAMP_TIME_56CYCLES5);
ADC_ConfigRegularChannel(ADC, ADC_CH_5_PA5, 2, ADC_SAMP_TIME_56CYCLES5);
/* Enable ADC DMA */
ADC_EnableDMA(ADC, ENABLE);
/* Enable ADC */
ADC_Enable(ADC, ENABLE);
/*wait ADC is ready to use*/
while(!ADC_GetFlagStatusNew(ADC, ADC_FLAG_RDY))
;
/*wait ADC is powered on*/
while(ADC_GetFlagStatusNew(ADC, ADC_FLAG_PD_RDY))
;
/* Start ADC Software Conversion */
ADC_EnableSoftwareStartConv(ADC, ENABLE);
}
void MYADC_Run(void)
{
uint32_t loop = 0;
uint32_t loopb = 0;
uint32_t dat0=0,dat1=0;
ADC_EnableSoftwareStartConv(ADC, ENABLE);
while(!ADC_GetFlagStatus(ADC, ADC_FLAG_ENDC));
ADC_ClearFlag(ADC,ADC_FLAG_ENDC);
dat0=ADCConvertedValue[0];
dat1=ADCConvertedValue[1];
loop = dat0;//油量
if(loop>TABLE_FUEL[0])
loop = TABLE_FUEL[0];
if(loop<TABLE_FUEL[8])
loop = TABLE_FUEL[8];
ucBufFuel[ucLocFuel]=loop;
ucLocFuel++;
ucCntFuel++;
if(ucLocFuel>=5)
ucLocFuel=0;
if(ucCntFuel>=5)
ucCntFuel=5;
loop=0;
for(loopb=0;loopb<ucCntFuel;loopb++)
loop+=ucBufFuel[loopb];
loop/=ucCntFuel; //????
loopb=0;
while(loop<TABLE_FUEL[loopb])
loopb++;
if(0==loopb)
loop = 0;
else
loop = 125*loopb-(loop-TABLE_FUEL[loopb])*125/(TABLE_FUEL[loopb-1]-TABLE_FUEL[loopb]);
loop/=10;
Adc_Fuel=loop;//0~100
loop = dat1;//电压
ucBufBV[ucLocBV] = loop;
ucLocBV++;
ucCntBV++;
if(ucLocBV>=4)
ucLocBV = 0;
if(ucCntBV>=4)
ucCntBV = 4;
loop=0;
for(loopb=0;loopb<ucCntBV;loopb++)
loop += ucBufBV[loopb];
loop /= ucCntBV; //
loop = loop*25/149+12;
Adc_Bv=loop;
}
|