3404|2

1

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

STM32定时器同步 [复制链接]

三个定时器 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\r\n", file, line) */

  while (1)
   {}
}
#endif

/**
  * @}
  */

/**
   * @}
   */

/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
此帖出自stm32/stm8论坛

最新回复

同意楼上的说法  详情 回复 发表于 2013-7-30 23:04

点评

TIM_SelectInputTrigger(TIM2, TIM_TS_ITR1); 你的TIM_TS_ITR1用的不对,好像应该是ITR2  详情 回复 发表于 2013-7-29 09:32
点赞 关注
 

回复
举报

4008

帖子

0

TA的资源

版主

沙发
 

回复 楼主ngliangzhu 的帖子

TIM_SelectInputTrigger(TIM2, TIM_TS_ITR1);

你的TIM_TS_ITR1用的不对,好像应该是ITR2
此帖出自stm32/stm8论坛

点评

同意楼上的说法  详情 回复 发表于 2013-7-30 23:04
 
 

回复

2606

帖子

0

TA的资源

五彩晶圆(初级)

板凳
 

回复 沙发huo_hu 的帖子

同意楼上的说法
此帖出自stm32/stm8论坛
 
个人签名工程 = 数学+物理+经济
 

回复
您需要登录后才可以回帖 登录 | 注册

查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/8 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表