这是代码:
[mw_shl_code=c,true]#define ADC1_DR_Address ((uint32_t)0x4001244C)
vu32 ADCConvertedValue;
void ADC_DMA_Init(void)
{
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCm(RCC_APB2Periph_ADC1|RCC_APB2Periph_ADC2 | RCC_APB2Periph_GPIOC, ENABLE );
//P2,PC3 作为模拟通道输入引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入引脚
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* DMA channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADCConvertedValue; //DMA内存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
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);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //单次转换[/mw_shl_code]
[mw_shl_code=c,true] ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channels configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 1, ADC_SampleTime_239Cycles5);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* ADC2 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC2, &ADC_InitStructure); [/mw_shl_code]
[mw_shl_code=c,true] /* ADC2 regular channels configuration */
ADC_RegularChannelConfig(ADC2, ADC_Channel_13, 1, ADC_SampleTime_239Cycles5);
/* Enable ADC2 external trigger conversion */
ADC_ExternalTrigConvCmd(ADC2, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable Vrefint channel17 */
ADC_TempSensorVrefintCmd(ENABLE);
/* 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));
/* Enable ADC2 */
ADC_Cmd(ADC2, ENABLE);
/* Enable ADC2 reset calibaration register */
ADC_ResetCalibration(ADC2);
/* Check the end of ADC2 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC2));
/* Start ADC2 calibaration */
ADC_StartCalibration(ADC2);
/* Check the end of ADC2 calibration */
while(ADC_GetCalibrationStatus(ADC2));
/* Start ADC1 Software Conversion */
// ADC_SoftwareStartConvCmd(ADC1, ENABLE);
/* Test on Channel 1 DMA_FLAG_TC flag */
// while(!DMA_GetFlagStatus(DMA_FLAG_TC1));
/* Clear Channel 1 DMA_FLAG_TC flag */
// DMA_ClearFlag(DMA_FLAG_TC1);
}
int main(void)[/mw_shl_code]
[mw_shl_code=c,true]{[/mw_shl_code]
[mw_shl_code=c,true] delay_init();[/mw_shl_code]
[mw_shl_code=c,true]NVIC_Configuration();[/mw_shl_code]
[mw_shl_code=c,true]uart_init(9600);[/mw_shl_code]
[mw_shl_code=c,true] LED_Init();[/mw_shl_code]
[mw_shl_code=c,true] ADC_DMA_Init();[/mw_shl_code]
[mw_shl_code=c,true]ADC_SoftwareStartConvCmd(ADC1, ENABLE);[/mw_shl_code]
[mw_shl_code=c,true] printf("%d\r\n",ADCConvertedValue); //打印接收到的数据[/mw_shl_code]
[mw_shl_code=c,true] [/mw_shl_code]
[mw_shl_code=c,true] [/mw_shl_code]
[mw_shl_code=c,true] [/mw_shl_code]
[mw_shl_code=c,true]}
[/mw_shl_code]
在战舰开板上我用杜邦线连接PC2,PC3,用板上的3.3v和GND进行测试,当都连接3.3v时输出0147853311268374015或者0145756159268374015,都连接GND时输出014784921665536,01478492160,0147849217131072这些数字。不知道是配置有问题还是ADC有问题?
|