RT
找了好多资料也没配置错,就是出不来
#define ADC1_DR_Address ((u32)0x40012400+0x4c) volatile u16 ADC_ConvertedValue[M]; void ADC1_Init(void) { DMA_InitTypeDef DMA_InitStructure; ADC_InitTypeDef ADC_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /*------------------使能时钟---------------*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE); DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;//ADC地址 DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;//内存地址 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //内存作为数据传输的目的地 DMA_InitStructure.DMA_BufferSize = M; //DMA 通道的DMA 缓存的大小 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址寄存器不变 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址寄存器递增 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //数据宽度为16 位 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //数据宽度为16 位 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //工作在循环缓存模式 DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA 通道 x 拥有高优先级 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA 通道x 没有设置为内存到内存传输 DMA_Init(DMA1_Channel1, &DMA_InitStructure); //根据DMA_InitStruct 中指定的参数初始化DMA 的通道 DMA_Cmd(DMA1_Channel1,ENABLE); //ADC_DeInit(ADC1); //将外设 ADC1 的全部寄存器重设为缺省值 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//独立ADC模式 ADC_InitStructure.ADC_ScanConvMode = ENABLE;//扫描模式 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//连续转换模式 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//不使用外部触发转换 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//采集数据右对齐模式 ADC_InitStructure.ADC_NbrOfChannel = M;//ADC转换通道数目 ADC_Init(ADC1,&ADC_InitStructure); ADC_Cmd(ADC1,ENABLE); RCC_ADCCLKConfig(RCC_PCLK2_Div6);//PCLK2 6分频为ADC时钟,12MHz ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_55Cycles5); ADC_DMACmd(ADC1,ENABLE); ADC_ResetCalibration(ADC1); //复位指定的ADC1 的校准寄存器 while(ADC_GetResetCalibrationStatus(ADC1)); ADC_StartCalibration(ADC1);//ADC校准 while(ADC_GetCalibrationStatus(ADC1)); ADC_SoftwareStartConvCmd(ADC1,ENABLE);//使用软件触发方式触发ADC转换 } /*获取ADC 的值,将二进制换算为十进制*/ u16 MQ4GetValue(void) { float volt,L; u16 value; volt = ADC_ConvertedValue[0] * 3.3 / 4096*1.51; if(volt<1.4) { L=150/1.4; value = (u16)(volt*L); } else if(volt>=1.4 && volt<2.0 ) { L=(380-150)/(2.0-1.4); value = (u16)((volt-1.4)*L); } else if(volt>=2.0 && volt<2.9 ) { L=(1700-380)/(2.9-2.0); value = (u16)((volt-2.0)*L); } else if(volt>=2.9 && volt<3.4 ) { L=(4500-1700)/(3.4-2.9); value = (u16)((volt-2.9)*L); } else if(volt>=3.4 && volt<3.7 ) { L=(9999-4500)/(3.7-3.4); value = (u16)((volt-3.4)*L); } else { value = 9999; } return value; } float BatteryGetVolt(void) { return (float)(ADC_ConvertedValue[1] * 3.3 / 4096)*2; }