OCInitType TIM_OCInitStructure;
DMA_InitType DMA_InitStructure;
ADC_InitType ADC_InitStructure;
NVIC_InitType NVIC_InitStructure;
TIM_TimeBaseInitType TIM_TimeBaseStructure;
drv_gpio_mode_set(GPIOE,(GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 |
GPIO_PIN_11 | GPIO_PIN_12),GPIO_Mode_AIN,GPIO_INPUT);
RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_DMA2, ENABLE);
RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC3,ENABLE);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM8,ENABLE);
DMA_DeInit(DMA2_CH5); //DMA初始化
DMA_InitStructure.PeriphAddr = (uint32_t)&ADC3->DAT;
DMA_InitStructure.MemAddr = (uint32_t)adc3_result_buff;
DMA_InitStructure.Direction = DMA_DIR_PERIPH_SRC;
DMA_InitStructure.BufSize = ADC3_DMA_BUFF_LEN;
DMA_InitStructure.PeriphInc = DMA_PERIPH_INC_DISABLE;
DMA_InitStructure.DMA_MemoryInc = DMA_MEM_INC_ENABLE;
DMA_InitStructure.PeriphDataSize = DMA_PERIPH_DATA_SIZE_HALFWORD;
DMA_InitStructure.MemDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.CircularMode = DMA_MODE_CIRCULAR;
DMA_InitStructure.Priority = DMA_PRIORITY_LOW;
DMA_InitStructure.Mem2Mem = DMA_M2M_DISABLE;
DMA_Init(DMA2_CH5, &DMA_InitStructure);
DMA_ConfigInt(DMA2_CH5, DMA_INT_TXC, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Channel5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_EnableChannel(DMA2_CH5, ENABLE); //先使能DMA中断,才能使能DMA,否则使能中断无效
ADC_InitStructure.WorkMode = ADC_WORKMODE_INDEPENDENT;
ADC_InitStructure.MultiChEn = ENABLE; //扫描模式
ADC_InitStructure.ContinueConvEn = DISABLE; //单次模式,TIM触发一次转换一次
ADC_InitStructure.ExtTrigSelect = ADC_EXT_TRIGCONV_T8_CC1;
ADC_InitStructure.DatAlign = ADC_DAT_ALIGN_R;
ADC_InitStructure.ChsNumber = 5;
ADC_Init(ADC3, &ADC_InitStructure);
ADC_ConfigRegularChannel(ADC3, ADC3_Channel_06_PE8, 1, ADC_SAMP_TIME_55CYCLES5);
ADC_ConfigRegularChannel(ADC3, ADC3_Channel_02_PE9, 2, ADC_SAMP_TIME_55CYCLES5);
ADC_ConfigRegularChannel(ADC3, ADC3_Channel_14_PE10, 3, ADC_SAMP_TIME_55CYCLES5);
ADC_ConfigRegularChannel(ADC3, ADC3_Channel_15_PE11, 4, ADC_SAMP_TIME_55CYCLES5);
ADC_ConfigRegularChannel(ADC3, ADC3_Channel_04_PE12, 5, ADC_SAMP_TIME_55CYCLES5);
ADC_EnableExternalTrigConv(ADC3, ENABLE);
ADC_EnableDMA(ADC3, ENABLE);
TIM_InitTimBaseStruct(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.Period = 2000;
TIM_TimeBaseStructure.Prescaler = 72-1;
TIM_TimeBaseStructure.ClkDiv = TIM_CLK_DIV1; //这个配置是PWM死区发生器频率和TIM频率得之间的分频系数,这里可以忽略
TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
TIM_InitTimeBase(TIM8, &TIM_TimeBaseStructure);
TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1;
TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
TIM_OCInitStructure.Pulse = 10; //0-1000
TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_HIGH; //无影响
TIM_InitOc1(TIM8, &TIM_OCInitStructure);
ADC_Enable(ADC3, ENABLE);
while(ADC_GetFlagStatusNew(ADC3,ADC_FLAG_RDY) == RESET);
ADC_StartCalibration(ADC3);
while (ADC_GetCalibrationStatus(ADC3));
|