/**
******************************************************************************
* 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****/
|