1、网上很多类似资料,总结就是要么给出不完整的一部分,要么就是没有标注说明之类的,鞋面我给大家贴出实用型的,包括如何配置 DMA 和 ADC,如何采集数据,如何处理数据/系统时钟使用 72MHZunsigned short AD_Value5=0;/定义成半字节数组void RCC_Config(void)ErrorStatus HSEStartUpStatus;/定义枚举型变量RCC_DeInit();RCC_HSEConfig(RCC_HSE_ON);HSEStartUpStatus = RCC_WaitForHSEStartUp();if(HSEStartUpStatus=SUCCESS)R
2、CC_HCLKConfig(RCC_SYSCLK_Div1);RCC_PCLK2Config(RCC_HCLK_Div1);RCC_PCLK1Config(RCC_HCLK_Div2);FLASH_SetLatency(FLASH_Latency_2);FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);RCC_PLLCmd(ENABLE);while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)=RESET);RC
3、C_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);while(RCC_GetSYSCLKSource()!=0x08);/-打开设备时钟 -/RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOF,ENABLE);/RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);/开串口 1 和 ADC1 的时钟void ADC_Start(void)DMA_Cmd(DMA1_Channel1, ENABLE
4、); /启动 DMA 通道ADC_SoftwareStartConvCmd(ADC1, ENABLE);/软件启动转换void DMA_ADC_Config(void)ADC_InitTypeDef ADC_InitStructure;DMA_InitTypeDef DMA_InitStructure;GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);GPIO_InitStructure.GPIO_Mode = GPIO_
5、Mode_AIN; /设置成模拟输入GPIO_Init(GPIOA, GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_0);GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; /设置成模拟输入GPIO_Init(GPIOB, RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);/开 ADC1 的时钟RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE); /开 DMA 时钟RCC_ADCCLKConfig(RCC_PCLK2_Div
6、8);/配置 ADC 时钟,为 PCLK2 的 8 分频,即 9MHzDMA_DeInit(DMA1_Channel1);/ADC 挂接在 DMA 的通道 1 上DMA_InitStructure.DMA_PeripheralBaseAddr = ADC_DR_ADDRESS; /DMA 外设 ADC 基地址DMA_InitStructure.DMA_MemoryBaseAddr = (u32)DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; /内存作为数据传输的目的地DMA_InitStructure.DMA_BufferSize = 5;
7、 /DMA 通道的 DMA 缓存的大小 系统用 5 个通道DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; /外设地址寄存器不变 即地址 ADC1-DR 不变DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; /内存地址寄存器递增 即数组 AD_Value 地址增加DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; /数据宽度为 16 位,这里尤其注意因为选
8、择的是半字节的,所以定义数组的时候也要定义成半字节数组,否则采集数据出乱,很多人是贴出资料来了,但是根本没有把这些注意点写明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_M
9、2M_Disable; /DMA 通道 x 设置为非内存到内存传输DMA_Init(DMA1_Channel1, /DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE);ADC_DeInit(ADC1);ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; /ADC 工作模式:ADC1 和 ADC2工作在独立模式ADC_InitStructure.ADC_ScanConvMode =ENABLE; /模数转换工作在扫描模式ADC_InitStructure.ADC_ContinuousConvMode = EN
10、ABLE;/连续转换/ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; /注意不要使用持续转换模式,否则只要触发一次后续的转换就会永不停歇(除非 CONT 清 0) ,这样第一次以后的 ADC,就不是由 TIM1_CC1 来触发了 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; /ADC 数据右对齐ADC_InitStructure.ADC_Nbr
11、OfChannel = 5; /顺序进行规则转换的 ADC 通道的数目ADC_Init(ADC1, /根据 ADC_InitStruct 中指定的参数初始化外设ADCx 的寄存器/设置指定 ADC 的规则组通道,设置它们的转化顺序和采样时间ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_239Cycles5);ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 2, ADC_SampleTime_239Cycles5);ADC_RegularChannelConfig(ADC
12、1, ADC_Channel_6, 3, ADC_SampleTime_239Cycles5);ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 4, ADC_SampleTime_239Cycles5);ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 5, ADC_SampleTime_239Cycles5);ADC_ExternalTrigInjectedConvCmd(ADC1,DISABLE);ADC_DMACmd(ADC1, ENABLE); / 开启 ADC 的 DMA 支持(要实现 DMA 功能
13、,还需独立配置 DMA 通道等参数)ADC_Cmd(ADC1, ENABLE); /使能指定的 ADC1ADC_ResetCalibration(ADC1);/复位校准寄存器while(ADC_GetResetCalibrationStatus(ADC1); /获取 ADC1 复位校准寄存器的状态,设置状态则等待ADC_StartCalibration(ADC1); /开始指定 ADC1 的校准状态while(ADC_GetCalibrationStatus(ADC1); /获取指定 ADC1 的校准程序,设置状态则等待ADC_Start();/数据采集处理void ADC_DateProce
14、ss(void)unsigned char i,k,h;unsigned int Buf=0;/递推 先进先出原理ADCCollect.PVVoltageBuf9=get_ChannelVale(ADC_Channel_4)ADCCollect.BatteryVoltageBuf9=get_ChannelVale(ADC_Channel_5)ADCCollect.CirTem1Buf9=get_ChannelVale(ADC_Channel_6)ADCCollect.BatteryChargeCurrentBuf9=get_ChannelVale(ADC_Channel_7)ADCCollec
15、t.BatteryDisChargeCurrentBuf9=get_ChannelVale(ADC_Channel_8)for(i=0;i ADCCollect.PVVoltageBufh+1)Buf = ADCCollect.PVVoltageBufh+1;ADCCollect.PVVoltageBufh+1 = ADCCollect.PVVoltageBufh;ADCCollect.PVVoltageBufh = Buf;Buf=0;if(ADCCollect.BatteryVoltageBufh ADCCollect.BatteryVoltageBufh+1)Buf = ADCColle
16、ct.BatteryVoltageBufh+1;ADCCollect.BatteryVoltageBufh+1 = ADCCollect.BatteryVoltageBufh;ADCCollect.BatteryVoltageBufh = Buf;Buf=0;if(ADCCollect.CirTem1Bufh ADCCollect.CirTem1Bufh+1)Buf = ADCCollect.CirTem1Bufh+1;ADCCollect.CirTem1Bufh+1 = ADCCollect.CirTem1Bufh;ADCCollect.CirTem1Bufh = Buf;Buf=0;if(
17、ADCCollect.BatteryChargeCurrentBufh ADCCollect.BatteryChargeCurrentBufh+1)Buf = ADCCollect.BatteryChargeCurrentBufh+1;ADCCollect.BatteryChargeCurrentBufh+1 = ADCCollect.BatteryChargeCurrentBufh;ADCCollect.BatteryChargeCurrentBufh = Buf;ADCCollect.CirTem1=0;ADCCollect.BatteryChargeCurrent=0;ADCCollec
18、t.BatteryDisChargeCurrent=0;ADCCollect.BatteryVoltage=0;ADCCollect.PVVoltage=0;/去掉最大值和最小值 取平均值for(i=3;i7;i+)/ADCCollect.CirTem1+=ADCCollect.CirTem1Bufi;ADCCollect.BatteryChargeCurrent+=ADCCollect.BatteryChargeCurrentBufi;ADCCollect.BatteryDisChargeCurrent+=ADCCollect.BatteryDisChargeCurrentBufi;ADCC
19、ollect.BatteryVoltage+=ADCCollect.BatteryVoltageBufi;ADCCollect.PVVoltage+=ADCCollect.PVVoltageBufi;ADCCollect.CirTem1=ADCCollect.CirTem1/4;ADCCollect.BatteryChargeCurrent=ADCCollect.BatteryChargeCurrent/4;ADCCollect.BatteryDisChargeCurrent=ADCCollect.BatteryDisChargeCurrent/4;ADCCollect.BatteryVolt
20、age=ADCCollect.BatteryVoltage/4;ADCCollect.PVVoltage=ADCCollect.PVVoltage/4;/充电电流ADCCollect.BatteryChargeCurrent=(ADCCollect.BatteryChargeCurrent*33)/4096;ADCCollect.BatteryChargeCurrent=(ADCCollect.BatteryChargeCurrent*30)/31;if(ADCCollect.BatteryChargeCurrent0.2)ADCCollect.BatteryChargeCurrent=0;/
21、电池电压ADCCollect.BatteryVoltage=ADCCollect.BatteryVoltage*33/40960;ADCCollect.BatteryVoltage=ADCCollect.BatteryVoltage*3068/68;/放电电流ADCCollect.BatteryDisChargeCurrent=(ADCCollect.BatteryDisChargeCurrent*33)/4096;ADCCollect.BatteryDisChargeCurrent=(ADCCollect.BatteryDisChargeCurrent*30)/31;if(ADCCollect.BatteryDisChargeCurrent0.2)ADCCollect.BatteryDisChargeCurrent=0;/PV 电压ADCCollect.PVVoltage=ADCCollect.PVVoltage*33/40960;ADCCollect.PVVoltage=ADCCollect.PVVoltage*3068/68;/温度ADCCollect.CirTem1=NTC_Temperature(ADCCollect.CirTem1);ADCCollect.CirTem1=ADCCollect.CirTem1/10;