你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

求教,TIM1互补输出模式,互补通道的占空比不能改变

[复制链接]
brucewoo 提问时间:2018-9-19 10:01 /
本帖最后由 woobruce 于 2018-9-19 12:03 编辑

问题已解决,是在其他地方的疏忽以及自己观察不仔细造成的,属于愚蠢的错误,惭愧惭愧。。。。
无论如何,谢谢回复的朋友。
//==============================================================


大家好,最近在看网上的教程自学STM32,今天在试验高级定时器的互补输出时,遇到了一个问题。


具体是,输出通道和互补输出通道各接上一个LED来直观的观察效果。
按照例程配置和初始化定时器和通道后,CH1和CH1N上接的LED会根据初始化时设定的TIM_Pulse值,有不同的明暗度,CH1和CH1N的明暗度相反,这应该符合互补输出的特性。

但是,如果在main函数里修改TIM1->CCR1的值,主通道LED的亮度会变化,但接互补通道的LED亮度没有变化。

请问这是为什么?
希望大家指点一下。

初始化的代码:

  1. RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);

  2. /*--------------------时基结构体初始化-------------------------*/
  3.         TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  4.         TIM_TimeBaseStructure.TIM_Period=(1000-1);               
  5.         TIM_TimeBaseStructure.TIM_Prescaler= (72-1);               
  6.         TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;               
  7.         TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;               
  8.         TIM_TimeBaseStructure.TIM_RepetitionCounter=0;        
  9.         TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  10.         /*--------------------输出比较结构体初始化-------------------*/               
  11.         TIM_OCInitTypeDef  TIM_OCInitStructure;
  12.         TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  13.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  14.         TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;         //反向通道有效
  15.         TIM_OCInitStructure.TIM_Pulse = 4;               
  16.         TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  17.         TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  18.         TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  19.         TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
  20.         TIM_OC1Init(ADVANCE_TIM, &TIM_OCInitStructure);
  21.         TIM_OC1PreloadConfig(ADVANCE_TIM, TIM_OCPreload_Enable);               

  22.         /*-------------------刹车和死区结构体初始化-------------------*/
  23.         TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
  24.   TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
  25.   TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
  26.   TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
  27.   TIM_BDTRInitStructure.TIM_DeadTime = 11;
  28.   TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
  29.   TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
  30.   TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
  31.   TIM_BDTRConfig(ADVANCE_TIM, &TIM_BDTRInitStructure);
  32.          
  33.         // 使能计数器
  34.         TIM_Cmd(ADVANCE_TIM, ENABLE);        
  35.         TIM_CtrlPWMOutputs(ADVANCE_TIM, ENABLE);
复制代码
Main函数中的代码:实验结果是 只有主通道的LED出来呼吸灯效果,互补通道的LED一直不会动


  1. if((Millis()-recMillis)<=(PWMLightCycle/2))
  2.                 {
  3.                         TIM1->CCR1=PWMmax*((Millis()-recMillis)/((float)PWMLightCycle/2));
  4.                 }
  5.                 else if((Millis()-recMillis)<=(PWMLightCycle))
  6.                 {
  7.                         TIM1->CCR1=PWMmax*(1-((Millis()-recMillis-(PWMLightCycle/2))/((float)PWMLightCycle/2)));
  8.                 }
  9.                 else
  10.                 {
  11.                         recMillis=Millis();
  12.                 }
复制代码


收藏 评论3 发布时间:2018-9-19 10:01

举报

3个回答
七哥 回答时间:2018-9-19 10:42:35
首先对应的GPIO要配置为Alternate function push-pull模式,其次你的正向引作用,反向不起作用的话。
请检查输出模式是否正确。

20180919103929.png

评分

参与人数 2ST金币 +1 蝴蝶豆 +3 收起 理由
zero99 + 3
woobruce + 1 很给力!

查看全部评分

七哥 回答时间:2018-9-19 11:11:41
请参考标准函数库例程“STM32F10x_StdPeriph_Examples\TIM\7PWM_Output”

  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm32f10x.h"

  3. /** @addtogroup STM32F10x_StdPeriph_Examples
  4.   * @{
  5.   */

  6. /** @addtogroup TIM_7PWM_Output
  7.   * @{
  8.   */

  9. /* Private typedef -----------------------------------------------------------*/
  10. /* Private define ------------------------------------------------------------*/
  11. /* Private macro -------------------------------------------------------------*/
  12. /* Private variables ---------------------------------------------------------*/
  13. TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  14. TIM_OCInitTypeDef  TIM_OCInitStructure;
  15. uint16_t TimerPeriod = 0;
  16. uint16_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 0;

  17. /* Private function prototypes -----------------------------------------------*/
  18. void RCC_Configuration(void);
  19. void GPIO_Configuration(void);

  20. /* Private functions ---------------------------------------------------------*/

  21. /**
  22.   * @brief   Main program
  23.   * @param  None
  24.   * @retval None
  25.   */
  26. int main(void)
  27. {
  28.   /*!< At this stage the microcontroller clock setting is already configured,
  29.        this is done through SystemInit() function which is called from startup
  30.        file (startup_stm32f10x_xx.s) before to branch to application main.
  31.        To reconfigure the default setting of SystemInit() function, refer to
  32.        system_stm32f10x.c file
  33.      */     
  34.       
  35.   /* System Clocks Configuration */
  36.   RCC_Configuration();

  37.   /* GPIO Configuration */
  38.   GPIO_Configuration();

  39.   /* TIM1 Configuration ---------------------------------------------------
  40.    Generate 7 PWM signals with 4 different duty cycles:
  41.    TIM1CLK = SystemCoreClock, Prescaler = 0, TIM1 counter clock = SystemCoreClock
  42.    SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
  43.    and Connectivity line devices and to 24 MHz for Low-Density Value line and
  44.    Medium-Density Value line devices
  45.    
  46.    The objective is to generate 7 PWM signal at 17.57 KHz:
  47.      - TIM1_Period = (SystemCoreClock / 17570) - 1
  48.    The channel 1 and channel 1N duty cycle is set to 50%
  49.    The channel 2 and channel 2N duty cycle is set to 37.5%
  50.    The channel 3 and channel 3N duty cycle is set to 25%
  51.    The channel 4 duty cycle is set to 12.5%
  52.    The Timer pulse is calculated as follows:
  53.      - ChannelxPulse = DutyCycle * (TIM1_Period - 1) / 100
  54.   ----------------------------------------------------------------------- */
  55.   /* Compute the value to be set in ARR regiter to generate signal frequency at 17.57 Khz */
  56.   TimerPeriod = (SystemCoreClock / 17570 ) - 1;
  57.   /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 and 1N */
  58.   Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
  59.   /* Compute CCR2 value to generate a duty cycle at 37.5%  for channel 2 and 2N */
  60.   Channel2Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000);
  61.   /* Compute CCR3 value to generate a duty cycle at 25%  for channel 3 and 3N */
  62.   Channel3Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100);
  63.   /* Compute CCR4 value to generate a duty cycle at 12.5%  for channel 4 */
  64.   Channel4Pulse = (uint16_t) (((uint32_t) 125 * (TimerPeriod- 1)) / 1000);

  65.   /* Time Base configuration */
  66.   TIM_TimeBaseStructure.TIM_Prescaler = 0;
  67.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  68.   TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
  69.   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  70.   TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

  71.   TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  72.   /* Channel 1, 2,3 and 4 Configuration in PWM mode */
  73.   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  74.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  75.   TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  76.   TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;
  77.   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  78.   TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  79.   TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  80.   TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

  81.   TIM_OC1Init(TIM1, &TIM_OCInitStructure);

  82.   TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
  83.   TIM_OC2Init(TIM1, &TIM_OCInitStructure);

  84.   TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;
  85.   TIM_OC3Init(TIM1, &TIM_OCInitStructure);

  86.   TIM_OCInitStructure.TIM_Pulse = Channel4Pulse;
  87.   TIM_OC4Init(TIM1, &TIM_OCInitStructure);

  88.   /* TIM1 counter enable */
  89.   TIM_Cmd(TIM1, ENABLE);

  90.   /* TIM1 Main Output Enable */
  91.   TIM_CtrlPWMOutputs(TIM1, ENABLE);

  92.   while (1)
  93.   {}
  94. }

  95. /**
  96.   * @brief  Configures the different system clocks.
  97.   * @param  None
  98.   * @retval None
  99.   */
  100. void RCC_Configuration(void)
  101. {
  102.   /* TIM1, GPIOA, GPIOB, GPIOE and AFIO clocks enable */
  103.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOE|
  104.                          RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO, ENABLE);
  105. }

  106. /**
  107.   * @brief  Configure the TIM1 Pins.
  108.   * @param  None
  109.   * @retval None
  110.   */
  111. void GPIO_Configuration(void)
  112. {
  113.   GPIO_InitTypeDef GPIO_InitStructure;

  114. #ifdef STM32F10X_CL
  115.   /* GPIOE Configuration: Channel 1/1N, 2/2N, 3/3N and 4 as alternate function push-pull */
  116.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_11|GPIO_Pin_13|GPIO_Pin_14|
  117.                                 GPIO_Pin_8|GPIO_Pin_10|GPIO_Pin_12;
  118.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  119.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  120.   GPIO_Init(GPIOE, &GPIO_InitStructure);

  121.   /* TIM1 Full remapping pins */
  122.   GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);

  123. #else
  124.   /* GPIOA Configuration: Channel 1, 2 and 3 as alternate function push-pull */
  125.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
  126.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  127.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  128.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  129.   /* GPIOB Configuration: Channel 1N, 2N and 3N as alternate function push-pull */
  130.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  131.   GPIO_Init(GPIOB, &GPIO_InitStructure);

  132. #endif
  133. }
复制代码

复制代码

评分

参与人数 1ST金币 +1 收起 理由
woobruce + 1 赞一个!

查看全部评分

STM1024 回答时间:2018-9-19 14:07:03
修改配置前:
TIM_Cmd(TIM1, DIABLE);
然后再修改配置,改完以后:
TIM_Cmd(TIM1, ENABLE);

评分

参与人数 1蝴蝶豆 +2 收起 理由
zero99 + 2

查看全部评分

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32Cube扩展软件包
意法半导体边缘AI套件
ST - 理想汽车豪华SUV案例
ST意法半导体智能家居案例
STM32 ARM Cortex 32位微控制器
关注我们
st-img 微信公众号
st-img 手机版