代码:
#define ADC0_DR_Address ((u32)0x4001244C)
__IO u16 ADC_ConvertedValue;
/*????2é?ùí¨μà???ú ê1?üGPIOê±?ó éè??ADC2é?ùPA0???úD?o?*/
void ADC0_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //GPIOéè???a?£?aê?è?
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*????ADC0μ?1¤×÷?£ê??aMDA?£ê? */
void ADC0_Mode_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //ê1?üMDA1ê±?ó
/* DMA channel1 configuration */
DMA_DeInit(DMA1_Channel1); //???¨DMAí¨μà
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC0_DR_Address;//éè??DMAíaéèμ??·
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; //éè??DMA?ú′?μ??·£?ADC×a???á1??±?ó·?è???μ??·
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //íaéè?aéè???aêy?Y′?ê?μ?à′?′
DMA_InitStructure.DMA_BufferSize = 1; //DMA?o3???éè???a1£?
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
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 DMA channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE); //ê1?üDMAí¨μà
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //ê1?üADC1ê±?ó
/* ADC1 configuration */
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ê1ó??àá¢?£ê?£?é¨?è?£ê?
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //?TDèía?ó′¥·¢?÷
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //ê1ó?êy?Yóò????
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1; // ??óD1??×a??í¨μà
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel11 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5); //í¨μà12é?ù?ü?ú55.5??ê±?ó?ü?ú
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE); //ê1?üADCμ?DMA
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE); //ê1?üADC1
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //?aê?×a??
}
/*3?ê??ˉADC1 */
void ADC0_Init(void)
{
ADC0_GPIO_Config();
ADC0_Mode_Config();
}
AD_value = 3300000/4096*ADC_ConvertedValue/1000;
目前我用了3V LDO给STM32F103供电,如果外部接入3.3V电压给单片机,AD采集可以正常执行,采集的结果也正确,但用3V供电AD也工作,但采集的数据不对了。 |