// Insert compiler directives to set the optimization to zero
// We do not want the compiler to optimize out any code
#pragma GCC push_options
#pragma GCC optimize ("O0")
// Include the header file.
// In STM32 microcontroller programming, the header files provided by the
// manufacturer (STMicroelectronics) contain the necessary definitions,
// constants, and function prototypes specific to the microcontroller's peripherals
// and features.
#include "stm32c0xx.h"
#include <AccelStepper.h>
#define MSI_PORT GPIOB
#define MS3_PIN_Pin GPIO_PIN_14
#define MS2_PIN_Pin GPIO_PIN_15
#define MS1_PIN_Pin GPIO_PIN_2
#define DIR_PORT GPIOB
#define DIR_PIN_Pin GPIO_PIN_13
#define STEP_PORT GPIOB
#define STEP_PIN GPIO_PIN_4
TIM_HandleTypeDef htim3;
void Error_Handler(void);
void SystemClock_Config(void);
static void MX_TIM3_Init(void);
float STPMOTOR_REV = 200.00;
float MAX_MICRO_STEP = 3200.00;
//AccelStepper stepper(1, STEP_PIN,DIR_PIN_Pin);
static void GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, MS1_PIN_Pin|DIR_PIN_Pin|MS3_PIN_Pin|MS2_PIN_Pin, STEP_PIN | GPIO_PIN_RESET);
/*Configure GPIO pins : MS1_PIN_Pin DIR_PIN_Pin MS3_PIN_Pin MS2_PIN_Pin */
GPIO_InitStruct.Pin = MS1_PIN_Pin|DIR_PIN_Pin|MS3_PIN_Pin|MS2_PIN_Pin | STEP_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
int main() {
// put your setup code here, to run once:
HAL_Init();
SystemClock_Config();
GPIO_Init();
MX_TIM3_Init();
/*
// put your main code here, to run repeatedly:
for(int i = 0; i < MAX_MICRO_STEP; i ++)
{
HAL_GPIO_WritePin(GPIOB, STEP_PIN, GPIO_PIN_SET);
delay(1);
HAL_GPIO_WritePin(GPIOB, STEP_PIN, GPIO_PIN_RESET);
delay(1);
}
delay(1); // this speeds up the simulation
*/
//stepper.moveTo(100);
}
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
static void MX_TIM3_Init(void)
{
/* USER CODE BEGIN TIM3_Init 0 */
/* USER CODE END TIM3_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM3_Init 1 */
/* USER CODE END TIM3_Init 1 */
htim3.Instance = TIM3;
htim3.Init.Prescaler = 48-1;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 65535;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM3_Init 2 */
/* USER CODE END TIM3_Init 2 */
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_FLASH_SET_LATENCY(FLASH_LATENCY_1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6