[mw_shl_code=c,true]void Con_Trsf_AD_DMA()
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitStructure.GPIO_Pin = trsf_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(trsf_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = CO2_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(CO2_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = vol_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(vol_GPIO, &GPIO_InitStructure);
/* Enable DMA1 clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* Enable ADC1 and GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 5000;
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_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 2;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel1 configuration */
ADC_RegularChannelConfig(ADC1, CO2_Channelx, 1, ADC_SampleTime_239Cycles5);
//ADC_RegularChannelConfig(ADC1, ADC_Channelx, 2, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, vol_Channel, 2, ADC_SampleTime_239Cycles5);
//ADC_RegularChannelConfig(ADC1, ADC_Channelx, 3, ADC_SampleTime_239Cycles5);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}[/mw_shl_code]
这是配置。
下面是读取函数
[mw_shl_code=c,true]void quick(uint16_t *a,int16_t i,int16_t j) //?ì?????ò
{
int16_t m,n,temp;
uint16_t k;
m=i;
n=j;
k=a[(i+j)/2]; /*??????????*/
do {
while(a[m]<k&&m<j) m++; /* ??×ó??????±?k?ó??????*/
while(a[n]>k&&n>i) n--; /* ??????×ó??±?k????????*/
if(m<=n) { /*?????????ú×????????ò????*/
temp=a[m];
a[m]=a[n];
a[n]=temp;
m++;
n--;
}
}while(m<=n);
if(m<j) quick(a,m,j); /*???????é*/
if(n>i) quick(a,i,n);
}
uint16_t readSF() //?????ò,??????×??ó×?????,×??ó???ù
{
uint8_t ji=0;
ADC_TR_ave=0;
for(ji=0;ji<20;ji++)
{
ADC_Tmp[ji]=ADCConvertedValue[ji*3+1];
}
quick(ADC_Tmp,0,19); //0~35±ê??,??36??
for(ji=2;ji<18;ji++)
{
ADC_TR_ave+=ADC_Tmp[ji];
}
ADC_TR_ave>>=4;
if(ADC_TR_ave>4000)
{
trSF=1002;
}
else
{
trSF=(float)(ADC_TR_ave*(3.3/4096))*100; //AD*0.8/2
}
if(trSF>999)
trSF=999;
return trSF;
}
uint16_t readCO2()
{
uint8_t ji=0;
ADC_CO2_ave=0;
for(ji=0;ji<20;ji++)
{
CO2_Tmp[ji]=ADCConvertedValue[ji*2];
}
quick(CO2_Tmp,0,19); //0~35±ê??,??36??
for(ji=2;ji<18;ji++)
{
ADC_CO2_ave+=CO2_Tmp[ji];
}
ADC_CO2_ave>>=4;
CO2=(float)(ADC_CO2_ave*(3.3/4096))*100; //AD*0.8/2
return CO2;
}
uint16_t readVOL()
{
uint8_t ji=0;
ADC_VOL_ave=0;
for(ji=0;ji<20;ji++)
{
ADC4_Tmp[ji]=ADCConvertedValue[ji*2+1];
}
quick(ADC4_Tmp,0,19); //0~35±ê??,??36??
for(ji=2;ji<18;ji++)
{
ADC_VOL_ave+=ADC4_Tmp[ji];
}
ADC_VOL_ave>>=4;
Vol=(uint16_t)(ADC_VOL_ave*330/4096); //AD*0.8/2
return Vol;
}[/mw_shl_code]
然后再主函数里面分别读取三个ad转换值
用的是10\11\15三个通道,其中其中任意两个组合成双通道都可以正常采集,三个一起就采集数据就异常了
|