#include "adc.h" u16 uAD_Buff_A[ADC_CHANNEL]; //ADC采集缓存A u16 uAD_Buff_B[ADC_CHANNEL]; //ADC采集缓存B void ADC_GPIO_Config(u32 RCC_APBnPeriphn, GPIO_TypeDef *GPIOx,u16 PIN) { RCC_AHB1PeriphClockCmd(RCC_APBnPeriphn, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//模拟输入 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//不带上下拉 GPIO_Init(GPIOx, &GPIO_InitStructure);//初始化 } void ADC_SOFT_Mode_Config(u32 RCC_APBnPeriphn, ADC_TypeDef* ADCn, u8 Channel_num) { RCC_APB2PeriphClockCmd(RCC_APBnPeriphn, ENABLE); //使能ADC3时钟 RCC_APB2PeriphResetCmd(RCC_APBnPeriphn, ENABLE); //ADC3复位 RCC_APB2PeriphResetCmd(RCC_APBnPeriphn, DISABLE); //复位结束 ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//独立模式 ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//两个采样阶段之间的延迟5个时钟 ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //DMA失能 ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//预分频4分频。ADCCLK=PCLK2/4=84/4=21Mhz,ADC时钟最好不要超过36Mhz ADC_CommonInit(&ADC_CommonInitStructure);//初始化 ADC_InitTypeDef ADC_InitStructure; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12位模式 ADC_InitStructure.ADC_ScanConvMode = DISABLE;//扫描模式 ENABLE ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//关闭连续转换 ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//禁止触发检测 //ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; //上升沿触发 //ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv; //事件触发ADC ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐 ADC_InitStructure.ADC_NbrOfConversion = Channel_num;//1个转换在规则序列中 也就是只转换规则序列1 //ADC_RegularChannelConfig(ADCn, ADC_Channel_14, 1, ADC_SampleTime_480Cycles); ADC_Init(ADCn, &ADC_InitStructure);//ADC初始化 ADC_Cmd(ADCn, ENABLE); //开启AD转换器 } u16 Get_SOFT_ADC(ADC_TypeDef* ADCn, u8 ADC_Channe) { ADC_RegularChannelConfig(ADCn, ADC_Channe, 1, ADC_SampleTime_480Cycles); ADC_SoftwareStartConv(ADCn); while(!ADC_GetFlagStatus(ADCn, ADC_FLAG_EOC)); return ADC_GetConversionValue(ADCn); } //u16 Get_SOFT_ADC_xTIME(ADC_TypeDef* ADCn, u8 ADC_Channe, u8 times) //{ // u32 i, temp_value = 0; // // for(i=0; iDR); //外围设备地址,ADC_DR_DATA规则数据寄存器 DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)(u16 *)uAD_Buff_A; //DMA存储器地址,自己设置的缓存地址 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //传输方向,外设->存储器 DMA_InitStructure.DMA_BufferSize = ADC_CHANNEL; //DMA缓存大小,数据传输量32*6 ADC_BUFFSIZE * ADC_CHANNEL 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模式,普通模式、循环模式,还有双缓冲模式,需要特殊设置 DMA_InitStructure.DMA_Priority = DMA_Priority_Low; //DMA优先级,高优先级 DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //不使用FIFO DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; //FIFO阈值 DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; //存储器突发,单次传输 DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; //外设突发,单次传输 DMA_Init(DMA_Stream, &DMA_InitStructure); //初始化DMA2_Stream0,对应为ADC3 //双缓冲模式设置 DMA_DoubleBufferModeConfig(DMA_Stream,(uint32_t)(u16 *)(uAD_Buff_B),DMA_Memory_0); //DMA_Memory_0首先被传输 DMA_DoubleBufferModeCmd(DMA_Stream,ENABLE); //设置DMA中断 DMA_ClearITPendingBit(DMA_Stream, DMA_IT_TC); //清除中断标志 DMA_ITConfig(DMA_Stream, DMA_IT_TC, ENABLE); //传输完成中断 DMA_Cmd(DMA_Stream, ENABLE); //使能DMA } void ADC_TIM_Config(TIM_TypeDef* TIMx,u16 psc, u16 per, void(*ClockCMD)(uint32_t, FunctionalState),u32 RCC_APBnPeriphn, //APB时钟使能设置 void(*TIM_OCInit)(TIM_TypeDef*, TIM_OCInitTypeDef*), //OC通道设置 void(*TIM_OCPreloadConfig)(TIM_TypeDef*, uint16_t)) { (*ClockCMD)(RCC_APBnPeriphn,ENABLE); //时基初始化 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructure.TIM_Prescaler = psc; //定时器分频 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseStructure.TIM_Period = per; //自动重装载值 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIMx,&TIM_TimeBaseStructure);//初始化定时器 //输出比较初始化 TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCInitStructure.TIM_Pulse = 10; TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低 (*TIM_OCInit)(TIMx,&TIM_OCInitStructure); (*TIM_OCPreloadConfig)(TIMx,TIM_OCPreload_Enable); TIM_CtrlPWMOutputs(TIMx, ENABLE); //TIM_ARRPreloadConfig(TIMx, ENABLE);//允许重载 TIM_SelectOutputTrigger(TIMx, TIM_TRGOSource_OC1);//触发源 //TIM_ITConfig(TIMx,TIM_IT_Update,ENABLE); TIM_Cmd(TIMx, ENABLE); //TIM_UpdateDisableConfig(TIMx, DISABLE); } void ADC_NVIC_Config(u8 NVIC_IRQChannel, u8 pre, u8 sub) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = NVIC_IRQChannel; //DMA2_Stream0中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pre; //抢占优先级1 NVIC_InitStructure.NVIC_IRQChannelSubPriority = sub; //子优先级1 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化NVIC寄存器 }