Skip to content

Instantly share code, notes, and snippets.

@eminboydak
Created October 26, 2021 09:06
Show Gist options
  • Save eminboydak/227d19e1b6a0415c48fab04b99f5003b to your computer and use it in GitHub Desktop.
Save eminboydak/227d19e1b6a0415c48fab04b99f5003b to your computer and use it in GitHub Desktop.
Stepper Ramp Driver
/* USER CODE BEGIN 0 */
void delay_us (uint16_t us)
{
__HAL_TIM_SET_COUNTER(&htim2,0); // set the counter value a 0
while (__HAL_TIM_GET_COUNTER(&htim2) < us); // wait for the counter to reach the us input in the parameter
}
void simpleAccel(int steps, GPIO_PinState dir)
{
HAL_GPIO_WritePin(Output1_GPIO_Port, Output1_Pin, dir);
int lowSpeed = 1000; // Minimum hiz. us cinsinden delay
int highSpeed = 250; // Maksinum hiz. us cinsinden delay
int change = 1; // Rampalama esnasindaki degisim orani
int rampUpStop = (lowSpeed - highSpeed) / change;
if (rampUpStop > steps / 2)
{
rampUpStop = steps / 2;
}
int rampDownStart = steps - rampUpStop;
int d = lowSpeed;
for (int i = 0; i < steps; i++)
{
HAL_GPIO_TogglePin(Output2_GPIO_Port, Output2_Pin);
delay_us(d);
HAL_GPIO_TogglePin(Output2_GPIO_Port, Output2_Pin);
delay_us(d);
if (i < rampUpStop)
{
d -= change;
}
else if (i > rampDownStart)
{
d += change;
}
}
}
/* USER CODE END 0 */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment