金牌会员
 
- 积分
- 1838
- 金钱
- 1838
- 注册时间
- 2014-6-27
- 在线时间
- 981 小时
|
发表于 2015-10-30 15:35:12
|
显示全部楼层
回复【3楼】szn1423:
---------------------------------
这里有个可以用的,你改改通道数量试试
#include "adc.h"
/*  rivate typedef -----------------------------------------------------------*/
/*  rivate define ------------------------------------------------------------*/
#define ADC1_DR_Address 0x40012440
static uint16_t AdcSingleDataGroup[2];
//==============================================================================================================
void ADC1_DMA_Init(void)
{
ADC_InitTypeDef ADC_InitStruct;
DMA_InitTypeDef DMA_InitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
/* ADC1 DeInit */
ADC_DeInit(ADC1);
/* Enable GPIOA clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* ADC1  eriph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* DMA1 clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
/* Configure  A.01 as analog input */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStruct);
/* DMA1 Channel1 Config */
DMA_DeInit(DMA1_Channel1); //????DMA?¨??1 DMA1_Channel1
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address; //?¨?? DMA?¨?????è?ù???·=ADC1_DR_Address
DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)AdcSingleDataGroup; //??±ê?????????·; //?¨??DMA?¨???????÷???· --??±ê?????????·
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC; //???¨???è???????·
DMA_InitStruct.DMA_BufferSize =2; //?¨??DMA???????ó??1
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //?±?°???è?????÷???·??±?
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable; //?±?°?????÷???·????
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //?¨?????è?????í??16??
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //?¨???????÷?????í??16??
DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; //DMA?¨????×÷???????·??????????
DMA_InitStruct.DMA_Priority = DMA_Priority_High; //DMA?¨??????????
DMA_InitStruct.DMA_M2M = DMA_M2M_Disable; //????DMA?¨???????÷???????÷????
DMA_Init(DMA1_Channel1, &DMA_InitStruct); //??????DMA?¨??1
/* DMA1 Channel1 enable */
DMA_Cmd(DMA1_Channel1, ENABLE);
// /* ADC DMA request in circular mode */
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
/* Enable ADC_DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Initialize ADC structure */
ADC_StructInit(&ADC_InitStruct);
/* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */
ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStruct.ADC_ScanDirection = ADC_ScanDirection_Backward;
ADC_Init(ADC1, &ADC_InitStruct);
/* Convert the ADC1 Vref with 55.5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_0 |ADC_Channel_1 , ADC_SampleTime_55_5Cycles);
// ADC_VrefintCmd(ENABLE);
/* ADC Calibration */
ADC_GetCalibrationFactor(ADC1);
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Wait the ADCEN falg */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1);
while((DMA_GetFlagStatus(DMA1_FLAG_TC1)) == RESET);//Test DMA1 TC flag
DMA_ClearFlag(DMA1_FLAG_TC1);//Clear DMA TC flag
}
///*****************************************************************************************
//???????? uchar test_once_bus(void)
//???? ????????×??????÷
//*****************************************************************************************/
u16 test_once_bus(void)
{
u8 i = 0,j;
u16 value=0;
u16 a[8];
while(i < 8)
{
a[i++] = AdcSingleDataGroup[0];
delay_us(22);
}
for(i=0;i<7;i++)
{
for(j=i+1;j<8;j++)
{
if(a>a[j])
{
value=a;
a=a[j];
a[j]=value;
}
}
}
return ((a[3]+a[4]))>>1;
} |
|