我做了一个动态改变占空比的程序 现在要加上刹车功能 故障时封锁SPWM输出
但是我一使能刹车 SPWM就出不来
高手帮忙看一下 我哪设置不对吗
修给占空比我是在 TIM5的中断中做的
void Timeconfig(void)
{
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1 ;
TIM_TimeBaseStructure.TIM_Period = TIM1_Period;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
//TIM_ARRPreloadConfig(TIM1, ENABLE);
/* Channel 1, Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 680;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
//死区设置
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
TIM_BDTRInitStructure.TIM_DeadTime = 20; //这里调整死区大小0-0xff
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
//启用CCR1寄存器的影子寄存器(直到产生更新事件才更改设置)
//TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Disable);
//TIM1_OC2模块设置(设置2通道占空比)
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 680;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
//启用CCR2寄存器的影子寄存器(直到产生更新事件才更改设置)
//TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_TimeBaseStructure.TIM_Prescaler= 0;
TIM_TimeBaseStructure.TIM_CounterMode= TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period= 4000;
TIM_TimeBaseStructure.TIM_ClockDivision= 0;
TIM_TimeBaseInit( TIM5, &TIM_TimeBaseStructure );
TIM_ITConfig( TIM5, TIM_IT_Update, ENABLE );
TIM_Cmd( TIM5, ENABLE );
TIM_TimeBaseStructure.TIM_Prescaler= 719;
TIM_TimeBaseStructure.TIM_CounterMode= TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period= 20;
TIM_TimeBaseStructure.TIM_ClockDivision= 0;
TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure );
TIM_SelectOutputTrigger( TIM3, TIM_TRGOSource_Update );// ADC Trigger 5KHz
TIM_Cmd( TIM3, ENABLE );
/* TIM1 counter enable */
TIM_Cmd(TIM1, ENABLE);
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
时 出来的SPWM只有180度的有
刹车的时候,就是把电机的2端都接地.可以刹的很死!!!注意别把电源和地短了
如题,程序里有两个功能,一是定时器1输出两路PWM,单独仿真和下载进去没有问题,都有输出;
二是通过定时器2周期性中断改变定时器1的脉宽,实现脉宽调节功能,加上这个功能后,仿真可以实现,下载后没输出
什么原因呢?
void TIM1_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //使能定时器3时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIO
GPIO_ResetBits(GPIOA,GPIO_Pin_8|GPIO_Pin_9);
//初始化TIM1
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned3; //TIM向上计数模式
// TIM_TimeBaseStructure.TIM_RepetitionCounter = 10;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC2
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
// TIM_ClearFlag(TIM1, TIM_FLAG_Update); //清中断,以免一启用中断后立即产生中断
// TIM_CtrlPWMOutputs(TIM1,ENABLE);
// TIM_Cmd(TIM1, ENABLE); //使能TIM3
}
void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 计数到5000为500ms
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 10Khz的计数频率
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_Cmd(TIM3, ENABLE); //使能TIMx外设
}
extern u16 Pulse1,Pulse2;
extern u16 Pulse_Add; // 5%
//定时器3中断服务程序
void TIM3_IRQHandler(void) //TIM3中断
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源
if(Pulse_Add==0)
{ TIM_SetCompare1(TIM1,500);
TIM_SetCompare2(TIM1,500);
}
else
{Pulse_Add=Pulse_Add-25;
Pulse1=500+Pulse_Add;
Pulse2=500-Pulse_Add;
TIM_SetCompare1(TIM1,Pulse1);
TIM_SetCompare2(TIM1,Pulse2);
}
}
// TIM_CtrlPWMOutputs(TIM1,ENABLE);
// TIM_Cmd(TIM1, ENABLE); //使能TIM3
}
u16 Pulse1,Pulse2;
int main(void)
{
// u16 led0pwmval=0;
// u8 dir=1;
delay_init(); //延时函数初始化
NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
// uart_init(9600); //串口初始化为9600
// LED_Init(); //LED端口初始化
// TIM3_PWM_Init(899,0); //不分频。PWM频率=72000/900=8Khz
TIM3_Int_Init(999,7199);
TIM1_PWM_Init(999,0); //预分频9,PWM频率=72000000/1000=72khz 定时器输出频率的计算公式:f(OC1) = 72,000,000 / (ARR - 1)*(PSC - 1);
TIM_SetCompare1(TIM1,500+475);
TIM_SetCompare2(TIM1,500-475);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1, ENABLE);
TIM_Cmd(TIM3, ENABLE);
while(1)
{ }
}