71 lines
2.0 KiB
C
71 lines
2.0 KiB
C
#include <StepperMotorDriver.h>
|
|
|
|
int16_t speedStepper1 = 0;
|
|
int16_t setSpeed1 = 200;
|
|
int16_t speedStepper2 = 0;
|
|
int16_t setSpeed2 = 200;
|
|
|
|
#define minSpeed 10.0f
|
|
#define maxSpeed 400.0f
|
|
|
|
void SetStepper1RotateSpeed(int16_t* speedStepper1)
|
|
{
|
|
if (abs(*speedStepper1) <= minSpeed | abs(*speedStepper1) >= maxSpeed)
|
|
{
|
|
TIM2->CCR1 = 0;
|
|
TIM2->EGR |= TIM_EGR_UG;
|
|
return;
|
|
}
|
|
// Управление направлением
|
|
if (*speedStepper1 > 0)
|
|
GPIOA->BSRR = GPIO_BSRR_BR1; // DIR = 0 (LOW)
|
|
else
|
|
GPIOA->BSRR = GPIO_BSRR_BS1; // DIR = 1 (HIGH)
|
|
|
|
uint32_t absSpeed = (*speedStepper1 > 0) ? *speedStepper1 : -(*speedStepper1);
|
|
|
|
uint32_t F_set = (N_FULL_STEP * MICROSTEPPING * absSpeed) / 60;
|
|
if (F_set == 0) return;
|
|
|
|
uint32_t arr_set = F_CLK / (PWM_PSC * F_set);
|
|
uint32_t ccr_set = (uint32_t)(arr_set * Duty);
|
|
|
|
if (arr_set < 10) arr_set = 10;
|
|
if (ccr_set < 1) ccr_set = 1;
|
|
|
|
TIM2->ARR = arr_set - 1;
|
|
TIM2->CCR1 = ccr_set;
|
|
|
|
TIM2->EGR |= TIM_EGR_UG;
|
|
}
|
|
//---------------------------------------------------------------------------------------------------------------
|
|
void SetStepper2RotateSpeed(int16_t* speedStepper2)
|
|
{
|
|
if (abs(*speedStepper2) <= minSpeed & abs(*speedStepper2) >= maxSpeed)
|
|
{
|
|
TIM2->CCR1 = 0;
|
|
TIM2->EGR |= TIM_EGR_UG;
|
|
return;
|
|
}
|
|
// Управление направлением
|
|
if (*speedStepper2 > 0)
|
|
GPIOB->BSRR = GPIO_BSRR_BR1; // DIR = 0 (LOW)
|
|
else
|
|
GPIOB->BSRR = GPIO_BSRR_BS1; // DIR = 1 (HIGH)
|
|
|
|
uint32_t absSpeed = (*speedStepper2 > 0) ? *speedStepper2 : -(*speedStepper2);
|
|
|
|
uint32_t F_set = (N_FULL_STEP * MICROSTEPPING * absSpeed) / 60;
|
|
if (F_set == 0) return;
|
|
|
|
uint32_t arr_set = F_CLK / (PWM_PSC * F_set);
|
|
uint32_t ccr_set = (uint32_t)(arr_set * Duty);
|
|
|
|
if (arr_set < 10) arr_set = 10;
|
|
if (ccr_set < 1) ccr_set = 1;
|
|
|
|
TIM3->ARR = arr_set - 1;
|
|
TIM3->CCR3 = ccr_set;
|
|
|
|
TIM3->EGR |= TIM_EGR_UG;
|
|
} |