新人再次求助:使用定时器主从门控模式精确控制脉冲个数的问题

2019-07-21 04:58发布

程序如下:
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无法立即响应高电平触发导致输出波形不符,不知哪位大神有办法解决这个问题,指点小弟一二。
波形图如下:
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
13条回答
420304805
1楼-- · 2019-07-21 08:41
图中正好是9个脉冲,有什么不对吗?
实际输出的是多少个?
animal
2楼-- · 2019-07-21 08:44
回复【2楼】420304805:
---------------------------------
但是最后一个脉冲只有一半,最后保持在高电平,我希望的是完整的9个脉冲,最后保持在低电平。
420304805
3楼-- · 2019-07-21 12:23
 精彩回答 2  元偷偷看……
xiaoyan
4楼-- · 2019-07-21 14:54
回复【楼主位】animal:
---------------------------------
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);加上这句
zhaolei0329
5楼-- · 2019-07-21 20:32
这个问题你解决了吗?亲,我也遇到了,怎么弄的
liuxiangyong
6楼-- · 2019-07-21 20:45
哥们,请教下,这里为什么是1TIM_SelectInputTrigger(TIM3, TIM_TS_ITR1);        为什么不可以是0,2,3?

一周热门 更多>