使用PWM1功能结合单脉冲模式输出任意个数脉冲,比如输出3个脉冲,最后电平是高,为什么不是低电平?OCIdleState并不起作用,可以修改配置成低电平吗?下面是代码配置
- TIM_InitStruct.Prescaler = Prescaler;
- TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
- TIM_InitStruct.Autoreload = Period;
- TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
- TIM_InitStruct.RepetitionCounter = 2;
- LL_TIM_Init(TIM1, &TIM_InitStruct);
- LL_TIM_EnableARRPreload(TIM1);
- LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH1);
- TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
- TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
- TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
- TIM_OC_InitStruct.CompareValue = Pulse;
- TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;//LL_TIM_OCPOLARITY_LOW;//
- TIM_OC_InitStruct.OCNPolarity = LL_TIM_OCPOLARITY_HIGH;
- TIM_OC_InitStruct.OCIdleState = LL_TIM_OCIDLESTATE_LOW;
- TIM_OC_InitStruct.OCNIdleState = LL_TIM_OCIDLESTATE_LOW;
- LL_TIM_OC_Init(TIM1, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
- LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH1);
- // TIM_InitStruct.Autoreload = Period;
- LL_TIM_SetOnePulseMode(TIM1, LL_TIM_ONEPULSEMODE_SINGLE);
- LL_TIM_SetTriggerOutput(TIM1, LL_TIM_TRGO_RESET);
- LL_TIM_SetTriggerOutput2(TIM1, LL_TIM_TRGO2_RESET);
- LL_TIM_DisableMasterSlaveMode(TIM1);
- TIM_BDTRInitStruct.OSSRState = LL_TIM_OSSR_DISABLE;
- TIM_BDTRInitStruct.OSSIState = LL_TIM_OSSI_DISABLE;//LL_TIM_OSSI_ENABLE;//
- TIM_BDTRInitStruct.LockLevel = LL_TIM_LOCKLEVEL_OFF;
- TIM_BDTRInitStruct.DeadTime = 0;
- TIM_BDTRInitStruct.BreakState = LL_TIM_BREAK_DISABLE;
- TIM_BDTRInitStruct.BreakPolarity = LL_TIM_BREAK_POLARITY_HIGH;
- TIM_BDTRInitStruct.BreakFilter = LL_TIM_BREAK_FILTER_FDIV1;
- TIM_BDTRInitStruct.BreakAFMode = LL_TIM_BREAK_AFMODE_INPUT;
- TIM_BDTRInitStruct.Break2State = LL_TIM_BREAK2_DISABLE;
- TIM_BDTRInitStruct.Break2Polarity = LL_TIM_BREAK2_POLARITY_HIGH;
- TIM_BDTRInitStruct.Break2Filter = LL_TIM_BREAK2_FILTER_FDIV1;
- TIM_BDTRInitStruct.Break2AFMode = LL_TIM_BREAK_AFMODE_INPUT;
- TIM_BDTRInitStruct.AutomaticOutput = LL_TIM_AUTOMATICOUTPUT_ENABLE;
- LL_TIM_BDTR_Init(TIM1, &TIM_BDTRInitStruct);
复制代码
波形图
|
其实,基于单脉冲模式实现指定个数脉冲输出可以有多种配置。
另外,你提及的OCIdleState并不起作用是正常的,该配置是针对互补输出才有作用。
这里有篇文章 《STM32定时器单脉冲输出模式话题》 非常详细地针对性介绍了STM32TIMER单脉
冲模式的实现原理,并辅以事例讲解,可以阅读下。希望读完之后消除你现在内心的疑惑。