#include "ADC.h"
#include "stm32f10x.h"
u16 ADC_ConvertedValue;
#define ADC1_DR_address ((u32)0x40012400+0x4c)
void ADC_init(void)
{
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/**************************DMA_Init***********************/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_address;//´«ËÍÔ´µØÖ·
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;//Ä¿±êµØÖ·
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//´«ËÍ·½Ïò£¬ÍâÉèµ½ÄÚ´æ
DMA_InitStructure.DMA_BufferSize = 1;//Êý¾Ý×Ü´óС£¬µ¥Î»ÎªDMA_PeripheralDataSize
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;//½ûÖ¹µØÖ·×ÔÔö¹¦ÄÜ
DMA_InitStructure.DMA_MemoryInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//Êý¾Ýµ¥Ôª´óС
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//DMAģʽ£¬Ñ»·,Õý³££¨1´Î£©
DMA_InitStructure.DMA_Priority = DMA_Priority_High;//ÓÅÏȼ¶
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//½ûÖ¹´æµ½ÄÚ´æ
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel1, ENABLE);
//ÅäÖÃÏàÓ¦IO¿ÚΪģÄâÊäÈë
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_DeInit(ADC1);//¸´Î»ADC¼Ä´æÆ÷£¬ÉèÖÃΪȱʡֵ
RCC_ADCCLKConfig(RCC_PCLK2_Div6);//ÉèÖÃADCʱÖÓ£¬²»³¬¹ý14Mhz£¬´ËʱΪ72/6
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//ADCģʽΪ¶ÀÁ¢Ä£Ê½
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//Êý¾ÝΪÓÒ¶ÔÆë
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//²»¿ªÆôɨÃèģʽ
ADC_InitStructure.ADC_NbrOfChannel = 0x01;//¹æÔò×éµÄÐòÁг¤Îª1
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//ÉèÖÃÆô¶¯¹æÔò×éת»»ÎªÈí¼þ´¥·¢
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//²»Á¬Ðø²É¼¯£¬£¬ÒòΪ¹æÔò×éÖµÅäÖÃÁËÒ»¸öͨµÀ
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1,ADC_SampleTime_239Cycles5);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);//ʹÄÜADC1
ADC_ResetCalibration(ADC1);//Ö´Ðи´Î»Ð£×¼£¬²¢µÈ´ýУ׼½áÊø
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);//Ö´ÐУÁ£Ä£ÃУ׼£¬²¢µÈ´ýУ׼½áÊø
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//Èí¼þ´¥·¢ADC
}
|