我想用TIM2捕获一个外部时钟信号,捕捉之后作为TIM5的计数时钟,并输出PWM,但是现在如下面代码这样配置没有输出,大伙帮我检查下哪里出错了吧. // 所需初始化结构体
GPIO_InitTypeDef gpio;
NVIC_InitTypeDef nvic;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// 打开所需时钟。
::RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
::RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
::RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
::RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC, ENABLE);
// 输入捕获配置 PB11-->TIM2_CH4
// GPIO设置
gpio.GPIO_Mode = GPIO_Mode_AF;
gpio.GPIO_Speed = GPIO_Speed_100MHz;
gpio.GPIO_OType = GPIO_OType_PP;
gpio.GPIO_PuPd = GPIO_PuPd_UP ;
gpio.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOB, &gpio);
// 复用功能重映射
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_TIM2);
TIM_DeInit(TIM2);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
// 上升沿捕获
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
// 输入脉冲与IC4直连
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
// 输入脉冲不分频
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
// 滤波,当输入脉冲持续时间少于两个计数单位时判定为干扰信号
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICStructInit(&TIM_ICInitStructure);
// 将TIM2设置为主模式
TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);
// 设置当发生输入捕获事件时输出TRGO
TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_OC1);
// 输入捕获配置 PA3-->TIM5_CH4(2000HZ),PB10-->TIM2_CH3(250HZ)
// GPIO设置
gpio.GPIO_Mode = GPIO_Mode_AF;
gpio.GPIO_Speed = GPIO_Speed_100MHz;
gpio.GPIO_OType = GPIO_OType_PP;
gpio.GPIO_PuPd = GPIO_PuPd_UP ;
gpio.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOB, &gpio);
gpio.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &gpio);
// 复用功能重映射
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_TIM5);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_TIM2);
// 定时器初始参数设置
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period = 8912;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 4096;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM5, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM5, TIM_OCPreload_Enable);
// 将TIM2过来的信号作为计数时钟模式
TIM_SelectSlaveMode(TIM5,TIM_SlaveMode_External1);
// 选择TIM2过来的信号作为触发源
TIM_SelectInputTrigger(TIM5,TIM_TS_ITR0);
// 定时器使能
TIM_Cmd(TIM5,ENABLE);
TIM_Cmd(TIM2,ENABLE);
|