Docs

Micro-controllers, wireless transmission and database

Head

NEMA 17 Motor & A4988 with Blue Pill using STM32CubeIDE

Prerequisites

This project assumes you have already installed STM32CubeIDE. You need to have previously done a basic blink sketch with blue-pill using STM32CubeIDE. I have made a complete video from installing STM32CubeIDE to LED blink program. You can watch it by clicking this link. https://www.youtube.com/watch?v=kXg467nVd_A

Wiring Diagram

Diagram

Image1

Image2

STM32CubeIDE Settings

Click RCC → High Speed Clock (HSE) to Crystal/Ceramic Resonator

Click Clock Configuration tab → HCLK (MHz) to 72

Click Pinout and Configuration tab

Click Timer → Click TIM1 →

Clock Source set to Internal Clock

Configuration → Parameter Settings →

Prescaler set to 71

Set PA1 and PA2 GPIO_Output

Additional code on top of STM32CubeIDE generated code

/* USER CODE BEGIN 0 */
#define DIR_PIN GPIO_PIN_1
#define DIR_PORT GPIOA
#define STEP_PIN GPIO_PIN_2
#define STEP_PORT GPIOA
int stepDelay = 1000; // 1000us more delay means less speed

void microDelay (uint16_t delay)
{
  __HAL_TIM_SET_COUNTER(&htim1, 0);
  while (__HAL_TIM_GET_COUNTER(&htim1) < delay);
}
/* USER CODE END 0 */

  /* USER CODE BEGIN 2 */
  HAL_TIM_Base_Start(&htim1);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    int x;
    HAL_GPIO_WritePin(DIR_PORT, DIR_PIN, GPIO_PIN_SET);
    for(x=0; x<200; x=x+1)
    {
      HAL_GPIO_WritePin(STEP_PORT, STEP_PIN, GPIO_PIN_SET);
      microDelay(stepDelay);
      HAL_GPIO_WritePin(STEP_PORT, STEP_PIN, GPIO_PIN_RESET);
      microDelay(stepDelay);
    }
    HAL_Delay(1000);
    HAL_GPIO_WritePin(DIR_PORT, DIR_PIN, GPIO_PIN_RESET);
    for(x=0; x<200; x=x+1)
    {
      HAL_GPIO_WritePin(STEP_PORT, STEP_PIN, GPIO_PIN_SET);
      microDelay(stepDelay);
      HAL_GPIO_WritePin(STEP_PORT, STEP_PIN, GPIO_PIN_RESET);
      microDelay(stepDelay);
    }
    HAL_Delay(1000);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */

Updated code for USER CODE 0 & USER CODE WHILE

/* USER CODE BEGIN 0 */
#define DIR_PIN GPIO_PIN_1
#define DIR_PORT GPIOA
#define STEP_PIN GPIO_PIN_2
#define STEP_PORT GPIOA

void microDelay (uint16_t delay)
{
  __HAL_TIM_SET_COUNTER(&htim1, 0);
  while (__HAL_TIM_GET_COUNTER(&htim1) < delay);
}

void step (int steps, uint8_t direction, uint16_t delay)
{
  int x;
  if (direction == 0)
    HAL_GPIO_WritePin(DIR_PORT, DIR_PIN, GPIO_PIN_SET);
  else
    HAL_GPIO_WritePin(DIR_PORT, DIR_PIN, GPIO_PIN_RESET);
  for(x=0; x<steps; x=x+1)
  {
    HAL_GPIO_WritePin(STEP_PORT, STEP_PIN, GPIO_PIN_SET);
    microDelay(delay);
    HAL_GPIO_WritePin(STEP_PORT, STEP_PIN, GPIO_PIN_RESET);
    microDelay(delay);
  }
}
/* USER CODE END 0 */

  /* USER CODE BEGIN WHILE */
  while (1)
  {
    int y;
    for(y=0; y<8; y=y+1) // 8 times
    {
      step(25, 0, 800); // 25 steps (45 degrees) CCV
      HAL_Delay(500);
    }
    step(800, 1, 5000); // 800 steps (4 revolutions ) CV
    HAL_Delay(1000);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */