打印
[应用相关]

SC0024—AT32F4xx_定时器同步之并行

[复制链接]
1234|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
ArterySW|  楼主 | 2020-8-29 14:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 ArterySW 于 2020-8-29 14:45 编辑

AT32F4xx定时器同步之并行模式

示例目的
展示3个TMR之间的并行同步模式。

支持型号列表:
AT32F4xx

主要使用外设列表:
TIMER

1 快速使用方法
1.1 硬件资源
AT-START-F403AV1.0 实验板

1.2 软件资源
该Demo以AT32F403A为例,BSP版本AT32F4xx_StdPeriph_Lib_V1.1.7。
TMR2作为主定时器,以更新事件作为TRGO源,用于触发两个从定时器。TMR3和TMR4作为从定时器,开启门控模式用于与主定时器进行同步。并且使能TMR2_CH1,TMR3_CH1,TMR4_CH1比较输出功能,产生PWM。其中TMR2_CH1输出频率750KHz,占空比25%;TMR3_CH1输出频率75KHz,占空比30%;TMR4_CH1输出频率150KHz,占空比60%。

1.3 示例使用
1) 通过示波器连接TMR2_CH1(PA0),TMR3_CH1(PA6),TMR4_CH1(PB6)即可。

SC0024_AT32F4xx_定时器同步之并行.zip (1.12 MB)





使用特权

评论回复
沙发
zeshoufx| | 2020-9-1 09:21 | 只看该作者
谢谢分享【AT32F4xx_定时器同步之并行】

使用特权

评论回复
板凳
自己的灌饼| | 2020-9-3 09:11 | 只看该作者
TMR2作为主定时器,以更新事件作为TRGO源,用于触发两个从定时器。TMR3和TMR4作为从定时器,开启门控模式用于与主定时器进行同步。

使用特权

评论回复
地板
便携手到老| | 2020-9-4 16:12 | 只看该作者
很棒,很赞,我认为这种比较好一些。能够很好的调试。希望国产的单片机越来越强大。

使用特权

评论回复
5
zc53946325| | 2020-10-20 15:34 | 只看该作者
按例子去弄,从定时器波形出不来

使用特权

评论回复
6
zc53946325| | 2020-10-20 15:34 | 只看该作者
/**
  ******************************************************************************
  * File   : TMR/TMR1_Synchro/main.c
  * Version: V1.2.4
  * Date   : 2020-08-26
  * Brief  : Main program body
  ******************************************************************************
  */


#include "at32f4xx.h"

/** @addtogroup AT32F415_StdPeriph_Examples
  * @{
  */

/** @addtogroup TMR_TMR1_Synchro
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TMR_TimerBaseInitType  TMR_TMReBaseStructure;
TMR_OCInitType  TMR_OCInitStructure;
TMR_BRKDTInitType TMR_BDTRInitStructure;

NVIC_InitType   NVIC_InitStructure = {0};

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

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


/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
void RCC_Configuration(void)
{
  /* TMR1, GPIOA and GPIOB clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_TMR1| RCC_APB2PERIPH_AFIO, ENABLE);

  /* TMR3 and TMR4 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_TMR3 | RCC_APB1PERIPH_TMR2 | RCC_APB1PERIPH_TMR4, ENABLE);
}

/**
  * @brief  Configures TMR1, TMR3 and TMR4 Pins.
  * @param  None
  * @retval None
  */
void GPIO_Configuration(void)
{
        GPIO_InitType GPIO_InitStructure;
       
        RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA | RCC_APB2PERIPH_GPIOB, ENABLE);

        /* GPIOA Configuration: TMR1 Channel1 and TMR3 Channel1 as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pins = GPIO_Pins_1 | GPIO_Pins_8;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        /* GPIOB Configuration: TMR4 Channel1 as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pins = GPIO_Pins_0 | GPIO_Pins_9;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
}


/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main(void)
{
        /*!< At this stage the microcontroller clock setting is already configured,
           this is done through SystemInit() function which is called from startup
           file (startup_at32f415_xx.s) before to branch to application main.
           To reconfigure the default setting of SystemInit() function, refer to
           system_at32f4xx.c file
         */

        /* System Clocks Configuration */
        RCC_Configuration();

        /* GPIO Configuration */
        GPIO_Configuration();

        /* TMR1 and TMRers(TMR3 and TMR4) synchronisation in parallel mode -----------
         1/TMR1 is configured as Master TMRer:
         - PWM Mode is used
         - The TMR1 Update event is used as Trigger Output

         2/TMR2 and TMR3, TMR4 are slaves for TMR1,
         - PWM Mode is used
         - The ITR0(TMR1) 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).
        --------------------------------------------------------------------------- */


        /* TMR2 Peripheral Configuration ----------------------------------------*/
        /* TMR2 Slave Configuration: PWM1 Mode */
        TMR_TMReBaseStructure.TMR_Period        = 287;
        TMR_TMReBaseStructure.TMR_DIV           = 0;
        TMR_TMReBaseStructure.TMR_ClockDivision = 0;
        TMR_TMReBaseStructure.TMR_CounterMode   = TMR_CounterDIR_Up;
        TMR_TimeBaseInit(TMR2, &TMR_TMReBaseStructure);

        TMR_OCInitStructure.TMR_OCMode      = TMR_OCMode_PWM1;
        TMR_OCInitStructure.TMR_OutputState = TMR_OutputState_Enable;
        TMR_OCInitStructure.TMR_Pulse       = 144;
        TMR_OC1Init(TMR2, &TMR_OCInitStructure);

        /* Slave Mode selection: TMR2 */
        TMR_SelectSlaveMode(TMR2, TMR_SlaveMode_Gate);
        TMR_SelectInputTrigger(TMR2, TMR_TRGSEL_ITR0);  //定时器1 的Trigger 0做为触发输入
       
        TMR_OC1PreloadConfig(TMR2, TMR_OCPreload_Enable);  
        TMR_ARPreloadConfig(TMR2, ENABLE);   
       
//======================================================

        /* TMR3 Peripheral Configuration ----------------------------------------*/
        /* TMR3 Slave Configuration: PWM1 Mode */
        TMR_TimeBaseStructInit(&TMR_TMReBaseStructure);
        TMR_TMReBaseStructure.TMR_Period        = 287;
        TMR_TMReBaseStructure.TMR_DIV           = 0;
        TMR_TMReBaseStructure.TMR_ClockDivision = 0;
        TMR_TMReBaseStructure.TMR_CounterMode   = TMR_CounterDIR_Up;
        TMR_TimeBaseInit(TMR3, &TMR_TMReBaseStructure);

        TMR_OCStructInit(&TMR_OCInitStructure);
        TMR_OCInitStructure.TMR_OCMode      = TMR_OCMode_PWM1;
        TMR_OCInitStructure.TMR_OutputState = TMR_OutputState_Enable;
        TMR_OCInitStructure.TMR_Pulse       = 144;
        TMR_OC1Init(TMR3, &TMR_OCInitStructure);

        /* Slave Mode selection: TMR3 */
        TMR_SelectSlaveMode(TMR3, TMR_SlaveMode_Gate);
        TMR_SelectInputTrigger(TMR3, TMR_TRGSEL_ITR0);   //定时器1 的Trigger 0做为触发输入
       
        TMR_ARPreloadConfig(TMR3, ENABLE);   
        TMR_OC1PreloadConfig(TMR3, TMR_OCPreload_Enable);  

        /* TMR4 Peripheral Configuration ----------------------------------------*/
        /* TMR4 Slave Configuration: PWM1 Mode */
        TMR_TimeBaseStructInit(&TMR_TMReBaseStructure);
        TMR_TMReBaseStructure.TMR_Period        = 287;
        TMR_TMReBaseStructure.TMR_DIV           = 0;
        TMR_TMReBaseStructure.TMR_ClockDivision = 0;
        TMR_TMReBaseStructure.TMR_CounterMode   = TMR_CounterDIR_Up;
        TMR_TimeBaseInit(TMR4, &TMR_TMReBaseStructure);

        TMR_OCStructInit(&TMR_OCInitStructure);
        TMR_OCInitStructure.TMR_OCMode      = TMR_OCMode_PWM1;
        TMR_OCInitStructure.TMR_OutputState = TMR_OutputState_Enable;
        TMR_OCInitStructure.TMR_Pulse       = 144;
        TMR_OC1Init(TMR4, &TMR_OCInitStructure);

        /* Slave Mode selection: TMR4 */
        TMR_SelectSlaveMode(TMR4, TMR_SlaveMode_Gate);
        TMR_SelectInputTrigger(TMR4, TMR_TRGSEL_ITR0);  //定时器1 的Trigger 0做为触发输入
       
        TMR_ARPreloadConfig(TMR4, ENABLE);   
        TMR_OC1PreloadConfig(TMR4, TMR_OCPreload_Enable);  

        /* TMR1 Peripheral Configuration ----------------------------------------*/
        /* TMRe Base configuration */
        TMR_TMReBaseStructure.TMR_DIV               = 0;
        TMR_TMReBaseStructure.TMR_CounterMode       = TMR_CounterDIR_Up;
        TMR_TMReBaseStructure.TMR_Period            = 287;  //500Khz, 144000/288=500
        TMR_TMReBaseStructure.TMR_ClockDivision     = 0;
        TMR_TMReBaseStructure.TMR_RepetitionCounter =  0;  //4;
        TMR_TimeBaseInit(TMR1, &TMR_TMReBaseStructure);

        /* Channel 1 Configuration in PWM mode */
        TMR_OCInitStructure.TMR_OCMode       = TMR_OCMode_PWM1;
        TMR_OCInitStructure.TMR_OutputState  = TMR_OutputState_Enable;
        TMR_OCInitStructure.TMR_OutputNState = TMR_OutputNState_Enable;
        TMR_OCInitStructure.TMR_Pulse        = 144;  //127;
        TMR_OCInitStructure.TMR_OCPolarity   = TMR_OCPolarity_Low;
        TMR_OCInitStructure.TMR_OCNPolarity  = TMR_OCNPolarity_Low;
        TMR_OCInitStructure.TMR_OCIdleState  = TMR_OCIdleState_Set;
        TMR_OCInitStructure.TMR_OCNIdleState = TMR_OCIdleState_Reset;

        TMR_OC1Init(TMR1, &TMR_OCInitStructure);

        /* Master Mode selection */
        TMR_SelectOutputTrigger(TMR1, TMR_TRGOSource_Update);

        /* Select the Master Slave Mode */
        TMR_SelectMasterSlaveMode(TMR1, TMR_MasterSlaveMode_Enable);  //触发
        TMR_OC1PreloadConfig(TMR1, TMR_OCPreload_Enable);  
        TMR_ARPreloadConfig(TMR1, ENABLE);      

        /*************************************************************************/

        /* TMR enable counter */
        TMR_Cmd(TMR2, ENABLE);
        TMR_Cmd(TMR3, ENABLE);
        TMR_Cmd(TMR4, ENABLE);

        /* TMR1 counter enable */
        TMR_Cmd(TMR1, ENABLE);

        /* Main Output Enable */
        TMR_CtrlPWMOutputs(TMR1, ENABLE);


        TMR_SetCompare1(TMR1, 144);


       
        while (1)
        {
               
        }
}


#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 2018 ArteryTek *****END OF FILE****/

使用特权

评论回复
7
zc53946325| | 2020-10-20 15:35 | 只看该作者
只有TMR1有波形出来

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

178

主题

266

帖子

8

粉丝