中级会员
- 积分
- 240
- 金钱
- 240
- 注册时间
- 2018-4-21
- 在线时间
- 105 小时
|
1金钱
void esc_init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
TIM_TimeBaseInitStructure.TIM_Period=2499;
TIM_TimeBaseInitStructure.TIM_Prescaler=83;
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=1000;
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High;
TIM_OC1Init(TIM4,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIM_OC2Init(TIM4,&TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIM_OC3Init(TIM4,&TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIM_OC4Init(TIM4,&TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIM_Cmd(TIM4,ENABLE);
delay_init(168);
TIM_SetCompare1(TIM4,2000);
TIM_SetCompare2(TIM4,2000);
TIM_SetCompare3(TIM4,2000);
TIM_SetCompare4(TIM4,2000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
TIM_SetCompare1(TIM4,1000);
TIM_SetCompare2(TIM4,1000);
TIM_SetCompare3(TIM4,1000);
TIM_SetCompare4(TIM4,1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
}这个是电调的程序
int main(void)
{
IIC_Init();
timer_init();
esc_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
TIM_SetCompare1(TIM4,1000);
TIM_SetCompare2(TIM4,1000);
TIM_SetCompare3(TIM4,1000);
TIM_SetCompare4(TIM4,1000);
delay_ms(1000);
delay_ms(1000);
while(1)
{
TIM_SetCompare1(TIM4,1500);
TIM_SetCompare2(TIM4,1500);
TIM_SetCompare3(TIM4,1500);
TIM_SetCompare4(TIM4,1500);
}
}这个是主函数
飞控还不完整 但我只想让电机转起来看看效果 但不知道这代码哪里出问题了 通电以后电机就响一声然后抖一下就没了
电调用的是新西达30A的电调 有大佬知道问题所在吗
|
最佳答案
查看完整内容[请看2#楼]
//***************************************************
//TIM4用于PWM控制马达
//arr:自动重装值
//psc:时钟预分频数
//***************************************************
void TIM4_Init(uint16_t arr,uint16_t psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// NVIC_InitTypeDef NV ...
|