ADCSingleChannelInit(ADCch ADC_Channel_x)
{ ADC_InitTypeDef ADC_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
ADC_StructInit(&ADC_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
//ADC prescale factor
ADC_InitStructure.ADC_PRESCARE = ADC_PCLK2_PRESCARE_6;//12M
//Set ADC mode to continuous conversion mode
ADC_InitStructure.ADC_Mode =ADC_Mode_Scan; //单周期转换
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //右对齐
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRIG;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_x, 0, ADC_CFGR_SAMCTL_7_5 );
ADC_ExternalTrigInjectedConvConfig(ADC1, ADC_ExternalTrigConv_T1_TRIG );
ADC_ExternalTrigConvCmd(ADC1, ENABLE); //ADC外部触发使能
ADC_Cmd(ADC1, ENABLE);
}
void TIM1_PWM_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler = psc;
//Setting Clock Segmentation
TIM_TimeBaseStructure.TIM_ClockDivision = (TIMCKD_TypeDef)0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;//中心对齐;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/** 通道 1 **/
TIM_OCStructInit(&TIM_OCInitStructure);
//Select Timer Mode: TIM Pulse Width Modulation Mode 2
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //OC*输出
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; // 互补输出关闭。关闭OC*N输出
TIM_OCInitStructure.TIM_Pulse = 200; //占空比
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//匹配时输出高电平
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Disable);
TIM_SelectOutputTrigger(TIM1,TIM_TRGOSource_Update ); //TRGO更新触发ADC
TIM_ARRPreloadConfig(TIM1, ENABLE); //重装载ARR
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
为什么采样不是PWM中心点。 |