程序如下:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //开启时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //初始化PA7
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //初始化PA1
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //TIM3初始化
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, & TIM_TimeBaseStructure);
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE );
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //TIM3输出PWM
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = arr;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_SetCompare2(TIM3, arr/2);
TIM_TimeBaseStructure.TIM_Period = 10*arr; //TIM2初始化
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //TIM2输出PWM并设置单脉冲模式
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_SetCompare2(TIM2, arr);
TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single);
//设置占空比
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_OC2Ref); //TIM2选择通道2作为输出触发
TIM_SelectInputTrigger(TIM3, TIM_TS_ITR1); //TIM3选择TIM2作为输入触发
TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Gated); //TIM3从模式输入触发高电平有效
TIM_Cmd(TIM2,ENABLE);
TIM_Cmd(TIM3,ENABLE);
原本希望通过控制TIM2CH2的高电平时间来控制TIM3输出9个脉冲,可是实际由于TIM3无法立即响应高电平触发导致输出波形不符,不知哪位大神有办法解决这个问题,指点小弟一二。
波形图如下:
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>