#include <stm32c0xx_hal.h>
void GPIO_Clock_Enable(GPIO_TypeDef* GPIOx) {
if (GPIOx == GPIOA) {
__HAL_RCC_GPIOA_CLK_ENABLE();
}
}
void GPIO_Pin_Mode(GPIO_TypeDef* GPIOx, uint16_t pin, uint32_t mode) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = pin;
GPIO_InitStruct.Mode = mode;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
}
int A_table[5] = {49, 37, 24, 12, 0};
int B_table[5] = {0, 12, 24, 37, 49};
int current_step = 0;
uint8_t forward = 1;
int main(void) {
HAL_Init();
GPIO_Clock_Enable(GPIOA);
GPIO_Pin_Mode(GPIOA, GPIO_PIN_0, GPIO_MODE_OUTPUT_PP);
GPIO_Pin_Mode(GPIOA, GPIO_PIN_1, GPIO_MODE_OUTPUT_PP);
GPIO_Pin_Mode(GPIOA, GPIO_PIN_2, GPIO_MODE_OUTPUT_PP);
GPIO_Pin_Mode(GPIOA, GPIO_PIN_3, GPIO_MODE_OUTPUT_PP);
while (1) {
update_microstep(current_step);
if (forward) {
current_step++;
} else {
current_step--;
}
if (current_step == 0) {
forward = 1;
} else if (current_step == 4) {
forward = 0;
}
HAL_Delay(1);
}
}
void update_microstep(uint8_t step) {
/* Canal A+ */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET); /* Canal A+ HIGH */
HAL_Delay(A_table[step]); // Duty cycle
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET); /* Canal A+ LOW */
/* Canal B+ */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET); /* Canal B+ HIGH */
HAL_Delay(B_table[step]); // Duty cycle
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); /* Canal B+ LOW */
/* Canal A- */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_SET); /* Canal A- HIGH */
HAL_Delay(49 - A_table[step]); // Duty cycle complemento
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET); /* Canal A- LOW */
/* Canal B- */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET); /* Canal B- HIGH */
HAL_Delay(49 - B_table[step]); // Duty cycle complemento
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET); /* Canal B- LOW */
}