STM32定时器TIM1 TIM2和TIM3无法同步???怎么回事呢?

2019-07-14 23:51发布

三个定时器 tiM1 TIM2和TIM3,  用TIM2做主模式其他两个做从,可以实现。但是以TIM3做主模式,TIM1和TIM2做从时,定时器无法启动。是不是在这种情况下TIM3不能做主模式啊?下面是程序代码,请大家帮帮忙 。
ARE AND/OR THE USE MADE BY CUSTOMERS OF THE


/* Includes ------------------------------------------------------------------*/
#include "STM32f10x.h"

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;

/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);

/* Private functions ---------------------------------------------------------*/

/**
   * @brief  Main program
  * @param  None
   * @retval : None
   */
int main(void)
{
   /* System Clocks Configuration */
   RCC_Configuration();

   /* GPIO Configuration */
  GPIO_Configuration();

  /* Timers synchronisation in parallel mode ----------------------------
     1/TIM3 is configured as Master Timer:
     - PWM Mode is used
     - The TIM2 Update event is used as Trigger Output  
     2/TIM1 and TIM2 are slaves for TIM3,
     - PWM Mode is used
     - The ITR1(TIM3) is used as input trigger for both slaves
     - Gated mode is used, so starts and stops of slaves counters
       are controlled by the Master trigger output signal(update event).

      The TIMxCLK is fixed to 72 MHz, the TIM3 counter clock is 72 MHz.
     The Master Timer TIM2 is running at 281.250 KHz and the duty cycle
     is equal to 25%
     The TIM3 is running:
    - At (TIM2 frequency)/ (TIM3 period + 1) = 28.125 KHz and a duty cycle
     equal to TIM3_CCR1/(TIM3_ARR + 1) = 30%
     The TIM4 is running:
      - At (TIM2 frequency)/ (TIM4 period + 1) = 56.250 KHz and a duty cycle
     equal to TIM4_CCR1/(TIM4_ARR + 1) = 60%
   -------------------------------------------------------------------- */

  /* Time base configuration */
   TIM_TimeBaseStructure.TIM_Period = 255;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

   TIM_TimeBaseStructure.TIM_Period = 9;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  TIM_TimeBaseStructure.TIM_Period = 4;
  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

   /* Master Configuration in PWM1 Mode */
   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 64;
   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

   TIM_OC1Init(TIM3, &TIM_OCInitStructure);

   /* Select the Master Slave Mode */
   TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);

   /* Master Mode selection */
  TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);

   /* Slaves Configuration: PWM1 Mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 3;

  TIM_OC1Init(TIM2, &TIM_OCInitStructure);

   TIM_OC1Init(TIM1, &TIM_OCInitStructure);

   /* Slave Mode selection: TIM3 */
   TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Gated);
   TIM_SelectInputTrigger(TIM2, TIM_TS_ITR1);

   /* Slave Mode selection: TIM4 */
   TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Gated);
   TIM_SelectInputTrigger(TIM1, TIM_TS_ITR1);

   /* TIM enable counter */
   TIM_Cmd(TIM3, ENABLE);
   TIM_Cmd(TIM2, ENABLE);
   TIM_Cmd(TIM1, ENABLE);

  TIM_CtrlPWMOutputs(TIM1, ENABLE);

   while (1)
   {}
}

/**
   * @brief  Configures the different system clocks.
   * @param  None
   * @retval : None
   */
void RCC_Configuration(void)
{
  /* Setup the microcontroller system. Initialize the Embedded Flash Interface,  
     initialize the PLL and update the SystemFrequency variable. */
   SystemInit();

   /* TIM2, TIM3 and TIM4 clock enable */
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 |
                          RCC_APB1Periph_TIM4, ENABLE);

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
   /* GPIOA and GPIOB clock enable */
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
}

/**
  * @brief  Configure the GPIOD Pins.
   * @param  None
   * @retval : None
   */
void GPIO_Configuration(void)
{
   GPIO_InitTypeDef GPIO_InitStructure;

   /* GPIOA Configuration: PA0(TIM2 CH1) and PA6(TIM3 CH1) as alternate function push-pull */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_6 | GPIO_Pin_8;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

   GPIO_Init(GPIOA, &GPIO_InitStructure);

   /* GPIOB Configuration: PB6(TIM4 CH1) as alternate function push-pull */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

   GPIO_Init(GPIOB, &GPIO_InitStructure);
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
   * @param file: pointer to the source file name
  * @param line: assert_param error line source number
   * @retval : None
   */
void assert_failed(uint8_t* file, uint32_t line)
{
   /* User can add his own implementation to report the file name and line number,
      ex: printf("Wrong parameters value: file %s on line %d", file, line) */

  while (1)
   {}
}
#endif

/**
  * @}
  */

/**
   * @}
   */

/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
zhangbao911
1楼-- · 2019-07-15 06:37

TIM2的TRGO事件作为TIM3的预分频器,触发TIM3,然后TIM3的TRGO事件作为TIM4的预分频器触发TIM4计数,
zhangbao911
2楼-- · 2019-07-15 12:32
STM32的每个定时器都可以由另一个定时器触发启动定时器一般是通过软件设置而启动,STM32的每个定时器也可以通过外部信号触发而启动,还可以通过另外一个定时器的某一个条件被触发而启动.这里所谓某一个条件可以是定时到时、定时器超时、比较成功等许多条件.这种通过一个定时器触发另一个定时器的工作方式称为定时器的同步,发出触发信号的定时器工作于主模式,接受触发信号而启动的定时器工作于从模式

一周热门 更多>