按照“pwm输出”视频的例程设置,但只有void TIM13,TIM14能输出正常pwm驱动电机。其余tim1-12均无输出。。。我弄四轴啊!只有两个pwm口?
程序大意是按按钮,将pwm从4000加到9999,驱动电调。。只有tim13 14能成功,其他要么没输出,要么电调一直警报油门过小。
此刻我的内心是崩溃的
[mw_shl_code=c,true]
[mw_shl_code=c,true]int main(void)
{
int pwmval1=4000;
u8 key; //±????ü??
KEY_Init(); //????????°??ü??????????????
// LED_Init(); //??????LED
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//?è????????????????·?×é2
delay_init(168); //?????????±????
// uart_init(115200);//???????®???¨??????115200
TIM1_PWM_Init(10000-1,21-1);
delay_ms(1000);
TIM_SetCompare1(TIM1,pwmval1);
TIM_SetCompare2(TIM1,pwmval1);
TIM_SetCompare3(TIM1,pwmval1);
TIM_SetCompare4(TIM1,pwmval1);
delay_ms(1000);
uart_init(500000); //???????®???¨??????500000
// LCD_Init(); //LCD??????
while(1) //????±???????0-300????????300?ó??300-0?????????·
{
key=KEY_Scan(1); //?????ü??
if(key)
{
switch(key)
{
case KEY0_PRES: //????LED0·?×?
pwmval1++;pwmval1++;pwmval1++;pwmval1++;pwmval1++;
TIM_SetCompare1(TIM1,pwmval1);
TIM_SetCompare2(TIM1,pwmval1);
TIM_SetCompare3(TIM1,pwmval1);
TIM_SetCompare4(TIM1,pwmval1);
break;
case KEY1_PRES: //????LED1·?×?
pwmval1--;pwmval1--;
TIM_SetCompare1(TIM1,pwmval1);
TIM_SetCompare2(TIM1,pwmval1);
TIM_SetCompare3(TIM1,pwmval1);
TIM_SetCompare4(TIM1,pwmval1);
break;
}
}else delay_ms(10);
// LCD_ShowNum(30+48+8,200,pwmval1,10,16);
// LCD_ShowNum(30+48+8,220,pwmval2,10,16);
// LCD_ShowNum(30+48+8,240,pwmval3,10,16);
// LCD_ShowNum(30+48+8,260,pwmval4,10,16);
}
}[/mw_shl_code]
[/mw_shl_code]
[mw_shl_code=c,true]
[/mw_shl_code]
[mw_shl_code=c,true]
[/mw_shl_code]
[mw_shl_code=c,true]void TIM1_PWM_Init(u32 arr,u32 psc)
{
//????·??è????????IO???è??
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); //TIM14?±??????
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); //????PORTF?±??
GPIO_PinAFConfig(GPIOE,GPIO_PinSource9|GPIO_PinSource11|GPIO_PinSource13|GPIO_PinSource14,GPIO_AF_TIM1); //GPIOF9???????¨?±?÷14
GPIO_InitStructure.GPIO_Pin = GPIO_PinSource9|GPIO_PinSource11|GPIO_PinSource13|GPIO_PinSource14; //GPIOF9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //????????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //????100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //???ì????????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //????
GPIO_Init(GPIOE,&GPIO_InitStructure); //??????PF9
TIM_TimeBaseStructure.TIM_Prescaler=psc; //?¨?±?÷·???
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //?ò??????????
TIM_TimeBaseStructure.TIM_Period=arr; //×?????×°????
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);//???????¨?±?÷14
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_ClearFlag(TIM1,TIM_FLAG_CC1);//????
TIM_ITConfig(TIM1,TIM_IT_CC1,ENABLE); //????
//??????TIM14 Channel1 PWM????
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_OCInitStructure.TIM_Pulse=0;
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //?ù??T???¨???????????????èTIM1 4OC1
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //????TIM14??CCR1?????¤×°???????÷
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1,ENABLE);//ARPE????
TIM_Cmd(TIM1, ENABLE); //????TIM14
}
[/mw_shl_code]
我们的综合实验,就有TIM9和TIM12的PWM输出代码,去参考下吧。
一周热门 更多>