经过查找后,问题的源头在于定时器配置的顺序有问题,换了配置的顺序就好了。之前先配置了定时器模式,再配置中断复位后不输出。之后先配置了中断,再配置定时器模式,工作正常。
/******************************************************************************
* @file static void TIM3_GPIO_Config(void)
* @brief 定时器3 的引脚初始化
* @parameter 无
* @return 无
* @author Love My BaoBaoGao
* @date 2018.12.15
******************************************************************************/
static void TIM1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOA and GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
/* 设置TIM3CLK 为 72MHZ */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* GPIOA and GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/* GPIO复用功能初始化 */
GPIO_InitStructure.GPIO_Pin = Attract_Pushrod_Front_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(Attract_Pushrod_Front_Port, &GPIO_InitStructure);
/* GPIO复用功能初始化 */
GPIO_InitStructure.GPIO_Pin = Attract_Pushrod_Reverse_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(Attract_Pushrod_Reverse_Port, &GPIO_InitStructure);
/* GPIO复用功能初始化 */
GPIO_InitStructure.GPIO_Pin = Brush_Pushrod_Front_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(Brush_Pushrod_Front_Port, &GPIO_InitStructure);
}
/******************************************************************************
* @file static void TIM3_NVIC_Config(void)
* @brief 配置嵌套向量中断控制器NVIC
* @parameter 无
* @return 无
* @author Love My BaoBaoGao
* @date 2018.12.15
******************************************************************************/
static void TIM1_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
/* 配置TIM3_IRQ中断为中断源 */
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/******************************************************************************
* @file void DHT21_Init(void)
* @brief 配置TIM3输出的PWM信号的模式,如周期、极性、占空比
* TIMxCLK/CK_PSC --> TIMxCNT --> TIMx_ARR --> TIMxCNT 重新计数
* TIMx_CCR(电平发生变化)
* 信号周期=(TIMx_ARR +1 ) * 时钟周期
* 占空比=TIMx_CCR/(TIMx_ARR +1)
//定时器的周期为5ms。
* @parameter 无
* @return 无
* @author Love My BaoBaoGao
* @date 2018.12.15
******************************************************************************/
static void TIM1_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 100-1; //当定时器从0计数到999,即为1000次,为一个定时周期
TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock/100000)-1; //设置预分频:不预分频,即为72MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //设置时钟分频系数:不分频(这里用不到)
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* 配置定时器通道 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 500; //设置通道4的电平跳变值,输出另外一个占空比的PWM
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //使能通道4
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE); // 使能TIM1重载寄存器ARR
/* 配置定时器通道 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 500; //设置通道4的电平跳变值,输出另外一个占空比的PWM
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //使能通道4
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE); // 使能TIM1重载寄存器ARR
/* 配置定时器通道 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 500; //设置通道4的电平跳变值,输出另外一个占空比的PWM
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //使能通道4
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE); // 使能TIM1重载寄存器ARR
/* TIM1 enable counter */
TIM_Cmd(TIM1, ENABLE); //使能定时器1
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_ITConfig(TIM1,TIM_IT_Update, ENABLE);
}
工作异常的配置
/******************************************************************************
* @file void TIM3_Init(void)
* @brief TIM3 输出PWM信号初始化,只要调用这个函数
* TIM3的四个通道就会有PWM信号输出
* @parameter 无
* @return 无
* @author Love My BaoBaoGao
* @date 2018.12.15
******************************************************************************/
void TIM1_Init(void)
{
TIM1_GPIO_Config();
TIM1_Mode_Config();
TIM1_NVIC_Config();
}
工作正常的配置
/******************************************************************************
* @file void TIM3_Init(void)
* @brief TIM3 输出PWM信号初始化,只要调用这个函数
* TIM3的四个通道就会有PWM信号输出
* @parameter 无
* @return 无
* @author Love My BaoBaoGao
* @date 2018.12.15
******************************************************************************/
void TIM1_Init(void)
{
TIM1_GPIO_Config();
TIM1_NVIC_Config();
TIM1_Mode_Config();
}