void ADC1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable ADC1 and GPIOA clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE); //RCC_AHBPeriph_GPIOA
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* Configure PA.3~0 as analog input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure); //
}
/**
* @brief ÅäÖÃADC1µÄ¹¤×÷ģʽΪMDAģʽ
* @param ÎÞ
* @retval ÎÞ
*/
static void ADC1_Mode_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
//NVIC_InitTypeDef NVIC_InitStructure;
/* ADC1 DeInit */
ADC_DeInit(ADC1); //¸´Î»ADC,then release
/* ADC1 Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* DMA1 clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
// DMA1 Channel1 Config
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_ConvertedValue; //ÄÚ´æµØÖ·(Òª´«ÊäµÄ±äÁ¿µÄÖ¸Õë)
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //DMA·½Ïò(´ÓÍâÉèµ½ÄÚ´æ)
DMA_InitStructure.DMA_BufferSize = 4; //3
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //ÍâÉèµØÖ·²»×ÔÔö
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //ÄÚ´æµØÖ·×ÔÔö
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //ÍâÉèÊý¾Ýµ¥Î»16bit
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //ÄÚ´æÊý¾Ýµ¥Î» 16bit
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //DMAģʽ:Ñ»·Ä£Ê½ DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //ÓÅÏȼ¶:¸ß
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //½ûÖ¹ÄÚ´æµ½ÄÚ´æµÄ´«Êä
DMA_Init(DMA1_Channel1, &DMA_InitStructure); //ÅäÖÃDMA1µÄ1ͨµÀ(ADC)
/* DMA1 Channel1 enable */
DMA_Cmd(DMA1_Channel1, ENABLE); //Æô¶¯DMAͨµÀ
/* Enable ADC_DMA */
//ADC_DMACmd(ADC1, ENABLE); //ÒƵ½Ð£×¼ºóÃæÁË
/* ADC DMA request in circular mode */
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
/* Initialize ADC structure */
ADC_StructInit(&ADC_InitStructure);
/* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //ENABLE
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Backward; //Upward
ADC_Init(ADC1, &ADC_InitStructure);
//ADC_SampleTime_55_5Cycles
ADC_ChannelConfig(ADC1, ADC_Channel_3 , ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_2 , ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_1 , ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_0 , ADC_SampleTime_239_5Cycles);
/* ADC Calibration */
ADC_GetCalibrationFactor(ADC1);
//ADC DMA ¿ªÆôÒ»¶¨·ÅÔÚ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);
}
/**
* @brief ADC1³õʼ»¯
* @param ÎÞ
* @retval ÎÞ
*/
void ADC1_Init(void)
{
ADC1_GPIO_Config();
ADC1_Mode_Config();
}
void ADC1_Init(void);
extern __IO uint16_t ADC_ConvertedValue[4];
extern FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint32_t ADC_FLAG);
extern void ADC_StartOfConversion(ADC_TypeDef* ADCx);
int main(void)
{
uint16_t i;
uint32_t lngX;
SystemInit();
USART1_Config();
ADC1_Init();
while (1)
{
if(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN))
ADC_Cmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY));
ADC_StartOfConversion(ADC1);
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADSTART)); //CONT=0,
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOSEQ)); //ADC_ISR_EOSEQ
//通过串口发送4个integer数据出去
for(i=0; i<4; i++)
UART_send_word(ADC_ConvertedValue); //串口程序试了正常
for(lngX=0x4fffff;lngX!=0;lngX--) __nop(); //延时
}
} |