新手上路
- 积分
- 35
- 金钱
- 35
- 注册时间
- 2015-11-20
- 在线时间
- 0 小时
|
楼主 |
发表于 2015-11-21 13:13:26
|
显示全部楼层
回复【2楼】正点原子:
<br>---------------------------------
<br>原子哥 原子哥 我最近在写四轴飞行器
<br>我的程序 启动4个电机后 程序就死了 完全不动需要重启
<br>
<br>别人的程序 启动电机没问题
<br>
<br>同样的板子(都是改变PWM 占空比) 这事什么问题嘞
<br>
<br>
<br>void Motor_Init(void ){
<br> GPIO_InitTypeDef GPIO_InitStructure; //IO口配置参数
<br> TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; //定时器配置参数
<br> TIM_OCInitTypeDef TIM_OCInitStructure; //
<br>
<br>
<br> //使能时钟
<br> RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
<br> RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
<br> //RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
<br>
<br> //设置电机使用到得管脚
<br> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 ;
<br> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
<br> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
<br> GPIO_Init(GPIOA, &GPIO_InitStructure);
<br>
<br> TIM_TimeBaseInitStructure.TIM_Period = eriod_Max;
<br> TIM_TimeBaseInitStructure.TIM_Prescaler = 2;
<br> TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
<br> TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
<br> TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
<br>
<br> TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
<br> TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
<br>
<br> TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
<br> TIM_OCInitStructure.TIM_Pulse = 0;
<br> TIM_OC1Init(TIM2,&TIM_OCInitStructure);
<br> TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);
<br>
<br>
<br> TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
<br> TIM_OCInitStructure.TIM_Pulse = 0;
<br> TIM_OC2Init(TIM2,&TIM_OCInitStructure);
<br> TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable);
<br>
<br>
<br> TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
<br> TIM_OCInitStructure.TIM_Pulse = 0;
<br> TIM_OC3Init(TIM2,&TIM_OCInitStructure);
<br> TIM_OC3PreloadConfig(TIM2,TIM_OCPreload_Enable);
<br>
<br> TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
<br> TIM_OCInitStructure.TIM_Pulse = 0;
<br> TIM_OC4Init(TIM2,&TIM_OCInitStructure);
<br> TIM_OC4PreloadConfig(TIM2,TIM_OCPreload_Enable);
<br>
<br> TIM_ARRPreloadConfig(TIM2, ENABLE);
<br> TIM_Cmd(TIM2,ENABLE);
<br>
<br>
<br>}
<br>
<br>
<br>void Motor_Working(uint16_t DutyC1,uint16_t DutyC2,uint16_t DutyC3,uint16_t DutyC4){
<br> static uint16_t lastDuty1=0,lastDuty2=0,lastDuty3=0,lastDuty4=0;
<br> //DutyC1 = DutyC1>eriod_Max?Period_MaxutyC1;
<br> //DutyC2 = DutyC2>eriod_Max?Period_MaxutyC2;
<br> //DutyC3 = DutyC3>eriod_Max?Period_MaxutyC3;
<br> //DutyC4 = DutyC4>eriod_Max?Period_MaxutyC4; //校正
<br>
<br>
<br> if(lastDuty1!=DutyC1){TIM2->CCR2 = DutyC1;lastDuty1=DutyC1;}
<br> if(lastDuty2!=DutyC2){TIM2->CCR1 = DutyC2;lastDuty2=DutyC2;}
<br> if(lastDuty3!=DutyC3){TIM2->CCR3 = DutyC3;lastDuty3=DutyC3;}
<br> if(lastDuty4!=DutyC4){TIM2->CCR4 = DutyC4;lastDuty4=DutyC4;}//更新占空比
<br>}
<br>
<br>这事程序 |
|