程序如下:
//S加速
int value4[]={
20000, 19940, 19880, 19810, 19740, 19670, 19590, 19510, 19430, 19340, 19250, 19160, 19060, 18960, 18850, 18740, 18630, 18510, 18390, 18270, 18140, 18000, 17860, 17720, 17570, 17420, 17260, 17100, 16930, 16760, 16580, 16400, 16220, 16030, 15830, 15630, 15430, 15230, 15020, 14800, 14580, 14360, 14140, 13910, 13680, 13450, 13220, 12980, 12740, 12500, 12260, 12020, 11780, 11540, 11300, 11060, 10820, 10580, 10340, 10110, 9870, 9640, 9410, 9180, 8960, 8740, 8520, 8300, 8090, 7890, 7680, 7480, 7290, 7100, 6910, 6730, 6560, 6380, 6220, 6050, 5900, 5740, 5590, 5450, 5310, 5170, 5040, 4920, 4790, 4680, 4560, 4450, 4350, 4250, 4150, 4050, 3960, 3880, 3790, 3710, 3640, 3560, 3490, 3420, 3360, 3300, 3240, 3180, 3120, 3070, 3020, 2970, 2930, 2890, 2840, 2800, 2770, 2730, 2690, 2660, 2630, 2600, 2570, 2540, 2520, 2490, 2470, 2440, 2420, 2400, 2380, 2360, 2340, 2330, 2310, 2300, 2280, 2270, 2250, 2240, 2230, 2220, 2210, 2200, 2190, 2180, 2170, 2160, 2150, 2140, 2130, 2130, 2120, 2110, 2110, 2100, 2100, 2090, 2090, 2080, 2080, 2070, 2070, 2060, 2060, 2060, 2050, 2050, 2050, 2040, 2040, 2040, 2040, 2030, 2030, 2030, 2030, 2030, 2020, 2020, 2020, 2020, 2020, 2020, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2000, 2000, 2000, 2000, 2000, 2000, 2000
};
//S减速
int value5[]={
2000, 2000, 2000, 2000, 2000, 2000, 2000, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2020, 2020, 2020, 2020, 2020, 2020, 2030, 2030, 2030, 2030, 2030, 2040, 2040, 2040, 2040, 2050, 2050, 2050, 2060, 2060, 2060, 2070, 2070, 2080, 2080, 2090, 2090, 2100, 2100, 2110, 2110, 2120, 2130, 2130, 2140, 2150, 2160, 2170, 2180, 2190, 2200, 2210, 2220, 2230, 2240, 2250, 2270, 2280, 2300, 2310, 2330, 2340, 2360, 2380, 2400, 2420, 2440, 2470, 2490, 2520, 2540, 2570, 2600, 2630, 2660, 2690, 2730, 2770, 2800, 2840, 2890, 2930, 2970, 3020, 3070, 3120, 3180, 3240, 3300, 3360, 3420, 3490, 3560, 3640, 3710, 3790, 3880, 3960, 4050, 4150, 4250, 4350, 4450, 4560, 4680, 4790, 4920, 5040, 5170, 5310, 5450, 5590, 5740, 5900, 6050, 6220, 6380, 6560, 6730, 6910, 7100, 7290, 7480, 7680, 7890, 8090, 8300, 8520, 8740, 8960, 9180, 9410, 9640, 9870, 10110, 10340, 10580, 10820, 11060, 11300, 11540, 11780, 12020, 12260, 12500, 12740, 12980, 13220, 13450, 13680, 13910, 14140, 14360, 14580, 14800, 15020, 15230, 15430, 15630, 15830, 16030, 16220, 16400, 16580, 16760, 16930, 17100, 17260, 17420, 17570, 17720, 17860, 18000, 18140, 18270, 18390, 18510, 18630, 18740, 18850, 18960, 19060, 19160, 19250, 19340, 19430, 19510, 19590, 19670, 19740, 19810, 19880, 19940, 20000};
/* 主函数 */
int main(void)
{
u16 i;
//内部时钟为1分频 = 16Mhz
CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1);
delay(2000);
qudong_Init();
GPIO_WriteHigh(GPIOE, GPIO_PIN_0); //FREE = 1
GPIO_WriteLow (GPIOC, GPIO_PIN_1); //DIR = 0
TIM3_Cmd(ENABLE);
delay(400);
while(1)
{
TIM3_TimeBaseInit(TIM3_PRESCALER_1,value4[0] ); //16M进行1分频,
TIM3_OC2Init(TIM3_OCMODE_PWM2, TIM3_OUTPUTSTATE_ENABLE,value4[0]/2 , TIM3_OCPOLARITY_LOW);
delay(2000);
for(i=0;i<=200;i++)
{
TIM3_TimeBaseInit(TIM3_PRESCALER_1,value4 ); //16M进行1分频,
TIM3_OC2Init(TIM3_OCMODE_PWM2, TIM3_OUTPUTSTATE_ENABLE,value4/2 , TIM3_OCPOLARITY_LOW);
delay(50);
}
delay(5000);
delay(5000);
delay(5000);
delay(5000);
//减速
for(i=0;i<=200;i++)
{
TIM3_TimeBaseInit(TIM3_PRESCALER_1,value5 ); //16M进行1分频,
TIM3_OC2Init(TIM3_OCMODE_PWM2, TIM3_OUTPUTSTATE_ENABLE,value5/2 , TIM3_OCPOLARITY_LOW);
delay(50);
}
//240RPM
TIM3_TimeBaseInit(TIM3_PRESCALER_1,value4[0] ); //16M进行1分频,
TIM3_OC2Init(TIM3_OCMODE_PWM2, TIM3_OUTPUTSTATE_ENABLE,value4[0]/2 , TIM3_OCPOLARITY_LOW);
delay(5000);
delay(5000);
delay(5000);
}
//while(1);
}
启动后,电机以240RPM转动,然后加速到2400RPM,后又降到240RPM,依此循环。
现在问题是在while循环里,循环第四次时加速过程中电机卡住,给STM8S复位又可以恢复,但仍是第四次卡住,因此很不理解,我实验了十多次了,都是第四次卡住。
电机有森创的两种,都是42系列,两相的,42BYG250CK-B,42BYG250BK,都是24V,1.5A,还有一款市场随便买的,没签。这三种电机都是在循环第四次时卡住。
驱动器是森创的,无细分,电流2.1A,后续会自己设计驱动芯片,缩小空间。
使用到步进电机是考虑到240RPM过程中有采集动作,至于2400RPM是离心用途,丢步丢转也可以接受,而240RPM必须精准,后续会使用到编码器配合。
|