直接上代码:
include "TIM_syn5.h"
include "led.h"
TIM_HandleTypeDef TIM_HandleStructure1,TIM_HandleStructure2;
TIM_MasterConfigTypeDef TIM_MasterStructure;
TIM_SlaveConfigTypeDef TIM_SlaveStructure;
TIM_OC_InitTypeDef TIM_OC_InitStructure;
TIM_IC_InitTypeDef TIM_IC_InitStructure;
void TIM_syn_Init(void)
{
//定时器TIM1的配置
TIM_HandleStructure1.Instance = MASTER_TIMx;
TIM_HandleStructure1.Init.Prescaler = MASTER_PSC;
TIM_HandleStructure1.Init.Period = MASTER_PERIOD;
TIM_HandleStructure1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
TIM_HandleStructure1.Init.CounterMode = TIM_COUNTERMODE_UP;
HAL_TIM_OC_Init(&TIM_HandleStructure1);
//定时器1主模式配置
TIM_MasterStructure.MasterOutputTrigger = TIM_TRGO_ENABLE;
TIM_MasterStructure.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
HAL_TIMEx_MasterConfigSynchronization(&TIM_HandleStructure1,&TIM_MasterStructure);
//定时器1从模式配置
TIM_SlaveStructure.InputTrigger = TIM_TS_TI1F_ED;
TIM_SlaveStructure.SlaveMode = TIM_SLAVEMODE_TRIGGER;
TIM_SlaveStructure.TriggerFilter = 0;
TIM_SlaveStructure.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;
HAL_TIM_SlaveConfigSynchro(&TIM_HandleStructure1,&TIM_SlaveStructure);
//定时器TIM1比较输出配置
TIM_OC_InitStructure.OCMode = TIM_OCMODE_PWM2;
TIM_OC_InitStructure.Pulse = (MASTER_PERIOD+1)/2;
TIM_OC_InitStructure.OCPolarity = TIM_OCPOLARITY_HIGH;
TIM_OC_InitStructure.OCNPolarity = TIM_OCNPOLARITY_HIGH;
TIM_OC_InitStructure.OCIdleState = TIM_OCIDLESTATE_SET;
TIM_OC_InitStructure.OCNIdleState = TIM_OCNIDLESTATE_RESET;
HAL_TIM_OC_ConfigChannel(&TIM_HandleStructure1,&TIM_OC_InitStructure,TIM_CHANNEL_4);
//定时器TIM1捕获输入通道配置
TIM_IC_InitStructure.ICPrescaler = TIM_ICPSC_DIV1;
TIM_IC_InitStructure.ICFilter = 0;
TIM_IC_InitStructure.ICPolarity = TIM_ICPOLARITY_RISING;
TIM_IC_InitStructure.ICSelection = TIM_ICSELECTION_DIRECTTI;
HAL_TIM_IC_ConfigChannel(&TIM_HandleStructure1,&TIM_IC_InitStructure,TIM_CHANNEL_1);
//定时器TIM2的配置
TIM_HandleStructure2.Instance = SLAVE_TIMx;
TIM_HandleStructure2.Init.Prescaler = SLAVE_PSC;
TIM_HandleStructure2.Init.Period = SLAVE_PERIOD;
TIM_HandleStructure2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
TIM_HandleStructure2.Init.CounterMode = TIM_COUNTERMODE_UP;
HAL_TIM_OC_Init(&TIM_HandleStructure2);
//TIM2从模式配置
TIM_SlaveStructure.InputTrigger = TIM_TS_ITR0;
TIM_SlaveStructure.SlaveMode = TIM_SLAVEMODE_TRIGGER;
TIM_SlaveStructure.TriggerFilter = 0;
TIM_SlaveStructure.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;
HAL_TIM_SlaveConfigSynchro(&TIM_HandleStructure2,&TIM_SlaveStructure);
//定时器TIM2比较输出配置
TIM_OC_InitStructure.OCMode = TIM_OCMODE_PWM2;
TIM_OC_InitStructure.Pulse = (SLAVE_PERIOD+1)/2;
TIM_OC_InitStructure.OCPolarity = TIM_OCPOLARITY_HIGH;
TIM_OC_InitStructure.OCNPolarity = TIM_OCNPOLARITY_HIGH;
TIM_OC_InitStructure.OCIdleState = TIM_OCIDLESTATE_SET;
TIM_OC_InitStructure.OCNIdleState = TIM_OCNIDLESTATE_RESET;
HAL_TIM_OC_ConfigChannel(&TIM_HandleStructure2,&TIM_OC_InitStructure,TIM_CHANNEL_4);
TIM_OC_InitStructure.Pulse = (SLAVE_PERIOD+1)/2;
TIM_OC_InitStructure.OCPolarity = TIM_OCPOLARITY_LOW;
HAL_TIM_OC_ConfigChannel(&TIM_HandleStructure2,&TIM_OC_InitStructure,TIM_CHANNEL_1);
//中断控制器设置
HAL_NVIC_SetPriority(SLAVE_IRQn,1,3);
HAL_NVIC_EnableIRQ(SLAVE_IRQn);
//启动TIM1的OC或OCN通道
HAL_TIM_OC_Start(&TIM_HandleStructure1,TIM_CHANNEL_4);
//开启定时器1捕获通道1
HAL_TIM_IC_Start(&TIM_HandleStructure1,TIM_CHANNEL_1);
//启动TIM2的OC或OCN通道
HAL_TIM_OC_Start(&TIM_HandleStructure2,TIM_CHANNEL_1);
HAL_TIM_OC_Start(&TIM_HandleStructure2,TIM_CHANNEL_4);
//开启带中断功能的定时器TIM2
HAL_TIM_Base_Start_IT(&TIM_HandleStructure2);
}
void HAL_TIM_OC_MspInit(TIM_HandleTypeDef *htim)
{
if(htim->Instance==MASTER_TIMx)
{
//开时钟
HAL_RCC_GPIOA_CLK_ENABLE(); //可以不输出!
HAL_RCC_GPIOB_CLK_ENABLE();
MASTER_TIM_CLK_ENABLE();
SLAVE_TIM_CLK_ENABLE();
//GPIO初始化,如果需要输出的话
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = GPIO_PIN_8;
GPIO_InitStructure.Mode = GPIO_MODE_AF_INPUT;
GPIO_InitStructure.Pull = GPIO_PULLDOWN;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_11; //TIM1_CH4
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA,&GPIO_InitStructure);
// GPIO_InitStructure.Pin = GPIO_PIN_13; //TIM1_CH1N
// HAL_GPIO_Init(GPIOB,&GPIO_InitStructure);
}
if(htim->Instance==SLAVE_TIMx)
{
//开时钟
__HAL_RCC_GPIOA_CLK_ENABLE();
//GPIO初始化,如果需要输出的话
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = GPIO_PIN_0; //TIM2通道1
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_3; //TIM2通道4
HAL_GPIO_Init(GPIOA,&GPIO_InitStructure);
}
}
输出的波形如下图所示,其中图中的channel2实际为定时器1第四通道输出的PWM波形;
图中的channel3和channel4为定时器2第1和第2通道输出的PWM波形。
从图可见:上图中的标注2和3为定时器TIM2两个通道的波形是同步的,标注为1的是定时器TIM1通道4输出的PWM波形,显然1与2和3不同步,咋回事呢?