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****/
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。