小学生
最后登录1970-1-1
在线时间 小时
注册时间2020-7-28
|
通用定时器3,占空比可调,相位可调,输出比较模式。可以实现功能,但是第二天下载其他任何正常的程序都不能正常运行,可以下进去,但是就不运行,led灯的都不行。神奇的是下载串口通信后可以运行了,但是一调试这个程序又出现这种问题。我感觉是程序有漏洞,但是自己又看不出来,大神救救我
#include "./generaltim/bsp_generaltim.h"
void Motor1_Init(void)
{
Motor MotorChValue;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/*设置中断*/
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/*时钟*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd( MOTOR1_CLK | RCC_APB2Periph_AFIO, ENABLE);
/*通道引脚*/
GPIO_InitStructure.GPIO_Pin = MOTOR1_A1_PIN | MOTOR1_B1_PIN ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(MOTOR1_A1_PORT, &GPIO_InitStructure);//初始化GPIO
GPIO_InitStructure.GPIO_Pin = MOTOR1_A2_PIN | MOTOR1_B2_PIN;
GPIO_Init(MOTOR1_A2_PORT, &GPIO_InitStructure);
//初始化TIM3
TIM_TimeBaseStructure.TIM_Period = 65535; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =72-1; //72分频,TIM3 = 1Mhz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //1分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
Motor Motor1Cw;
/* 设置第一次触发捕获通道的值,错开指定相位 */
Motor1Cw.Ch1Value = ((PERIOD_COUNT_VALUE*0)/6);
Motor1Cw.Ch2Value = ((PERIOD_COUNT_VALUE*2)/6);
Motor1Cw.Ch3Value = ((PERIOD_COUNT_VALUE*4)/6);
Motor1Cw.Ch4Value = ((PERIOD_COUNT_VALUE*6)/6);
//初始化TIM3 Channel2 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = Motor1Cw.Ch1Value;
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC1
TIM_OCInitStructure.TIM_Pulse = Motor1Cw.Ch2Value;
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC2
TIM_OCInitStructure.TIM_Pulse = Motor1Cw.Ch3Value;
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC3
TIM_OCInitStructure.TIM_Pulse = Motor1Cw.Ch4Value;
TIM_OC4Init(TIM3, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC4
TIM_ITConfig(TIM3, TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4,ENABLE);
TIM_SetCounter(TIM3,0);
TIM_Cmd(TIM3, ENABLE);
// TIM_Cmd(TIM3, DISABLE);
}
void Motor1_Stop(void)
{
TIM_DeInit(TIM3); //关闭电机,电机1定时器4
}
声明
#ifndef BSP_GENERALTIM_H
#define BSP_GENERALTIM_H
#include "stm32f10x.h"
#define PERIOD 100 //频率100hz
#define PERIOD_COUNT_VALUE (1000000/PERIOD) //频率所对应的计数值(period of timer4、timer3 is 1M)
#define ADD1 ((PERIOD_COUNT_VALUE*3)/8) //占空比
#define ADD2 ((PERIOD_COUNT_VALUE*5)/8)
#define MOTOR1_CLK RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB
#define MOTOR1_A1_PORT GPIOA
#define MOTOR1_A1_PIN GPIO_Pin_6
#define MOTOR1_B1_PORT GPIOA
#define MOTOR1_B1_PIN GPIO_Pin_7
#define MOTOR1_A2_PORT GPIOB
#define MOTOR1_A2_PIN GPIO_Pin_0
#define MOTOR1_B2_PORT GPIOB
#define MOTOR1_B2_PIN GPIO_Pin_1
typedef struct
{
u16 Ch1Value;
u16 Ch2Value;
u16 Ch3Value;
u16 Ch4Value;
}Motor;
void Motor1_Init(void);
void Motor1_Stop(void);
#endif /* BSP_GENERALTIM_H */
中断程序
void TIM3_IRQHandler(void) //TIM3中断
{
static u8 Count1=0,Count2=0,Count3=0,Count4=0;
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
if(RESET == Count1)
{
TIM3->CCR1 =(TIM3->CCR1 +ADD1);
}
else
{
TIM3->CCR1 =(TIM3->CCR1 +ADD2);
}
Count1 = !Count1;
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1 ); //清除TIMx的中断待处理位:TIM 中断源
}
else if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
if(RESET == Count2)
{
TIM3->CCR2 =(TIM3->CCR2 +ADD1);
}
else
{
TIM3->CCR2 =(TIM3->CCR2 +ADD2);
}
Count2 = !Count2;
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2 ); //清除TIMx的中断待处理位:TIM 中断源
}
else if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
if(RESET == Count3)
{
TIM3->CCR3 =(TIM3->CCR3 +ADD1);
}
else
{
TIM3->CCR3 =(TIM3->CCR3 +ADD2);
}
Count3 = !Count3;
TIM_ClearITPendingBit(TIM3, TIM_IT_CC3 ); //清除TIMx的中断待处理位:TIM 中断源
}
else if (TIM_GetITStatus(TIM3, TIM_IT_CC4) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
if(RESET == Count4)
{
TIM3->CCR4 =(TIM3->CCR4 +ADD1);
}
else
{
TIM3->CCR4 =(TIM3->CCR4 +ADD2);
}
Count4 = !Count4;
TIM_ClearITPendingBit(TIM3, TIM_IT_CC4 ); //清除TIMx的中断待处理位:TIM 中断源
}
}
|
|