1
\$\begingroup\$

I am trying to run a BLDC motor using my STM32 controller. I am using five N-channel MOSFETs for each phase, two for the high side and three for the low side. There is also a MOSFETs driver IC between the controller and the MOSFETs. The low side is inverted. Right now, I am just trying to run the motor according to thd Hall-effect sensor output

FROM TEXAS WEBSITE

FROM TEXAS WEBSITE: https://e2e.ti.com/support/sensors-group/sensors/f/sensors-forum/1038666/faq-what-hall-effect-sensors-are-used-for-brushless-dc-motor-commutation

I am following this table. I just connected phase U high-side MOSFET to controller GPIO output pin 1 and low-side MOSFET to controller GPIO output 2, and did the same for all other phases. But when I run the code, the MOSFETs fused. I am unable to get the reason. Here is my code:

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */


UART_HandleTypeDef huart1;

#define HALL_SENSOR_A_PIN GPIO_PIN_1
#define HALL_SENSOR_B_PIN GPIO_PIN_2
#define HALL_SENSOR_C_PIN GPIO_PIN_10
#define HALL_SENSOR_PORT GPIOB


enum MotorStage {
    STAGE_0,
    STAGE_1,
    STAGE_2,
    STAGE_3,
    STAGE_4,
    STAGE_5,
    STAGE_6
};


volatile enum MotorStage motor_stage=STAGE_0;

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {

    if ((GPIO_Pin == HALL_SENSOR_A_PIN)||(GPIO_Pin == HALL_SENSOR_B_PIN)||(GPIO_Pin == HALL_SENSOR_C_PIN)) {
          uint8_t sensor_A = HAL_GPIO_ReadPin(HALL_SENSOR_PORT, HALL_SENSOR_A_PIN);
          uint8_t sensor_B = HAL_GPIO_ReadPin(HALL_SENSOR_PORT, HALL_SENSOR_B_PIN);
          uint8_t sensor_C = HAL_GPIO_ReadPin(HALL_SENSOR_PORT, HALL_SENSOR_C_PIN);

          uint8_t data_to_send[6];  // Assuming each sensor value can be represented by a single character
          data_to_send[0] = sensor_A ? '1' : '0';  // Convert sensor_A to '1' or '0'
          data_to_send[1] = sensor_B ? '1' : '0';  // Convert sensor_B to '1' or '0'
          data_to_send[2] = sensor_C ? '1' : '0';  // Convert sensor_C to '1' or '0'
          data_to_send[3] = '\n';  // Add newline character for formatting
          data_to_send[4] = '\r';  // Add carriage return character for formatting
          data_to_send[5] = '\0';  // Null terminator for string

                 // Transmit sensor values over UART
          HAL_UART_Transmit(&huart1, data_to_send, sizeof(data_to_send), 5);

        // Determine motor stage based on sensor inputs
          if (sensor_A && !sensor_B && !sensor_C) {
            motor_stage = STAGE_1;
          } else if (sensor_A && !sensor_B && sensor_C) {
            motor_stage = STAGE_2;
          } else if (!sensor_A && !sensor_B && sensor_C) {
            motor_stage = STAGE_3;
          } else if (!sensor_A && sensor_B && sensor_C) {
            motor_stage = STAGE_4;
          } else if (!sensor_A && sensor_B && !sensor_C) {
            motor_stage = STAGE_5;
          } else if (sensor_A && sensor_B && !sensor_C) {
            motor_stage = STAGE_6;
       }
    }
}
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1) {
     switch(motor_stage) {
     case STAGE_0:
          uint8_t buffer[27]="stage0\n\r";
          HAL_UART_Transmit(&huart1,buffer,27,10);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
          break;
      case STAGE_1:
          uint8_t buffer1[27]="stage1\n\r";
          HAL_UART_Transmit(&huart1,buffer1,27,10);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
          break;
      case STAGE_2:
          uint8_t buffer2[27]="stage2\n\r";
          HAL_UART_Transmit(&huart1,buffer2,27,10);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
          break;
      case STAGE_3:
          uint8_t buffer3[27]="stage3\n\r";
          HAL_UART_Transmit(&huart1,buffer3,27,10);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
          break;
      case STAGE_4:
          uint8_t buffer4[27]="stage4\n\r";
          HAL_UART_Transmit(&huart1,buffer4,27,10);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
          break;
      case STAGE_5:
          uint8_t buffer5[27]="stage5\n\r";
          HAL_UART_Transmit(&huart1,buffer5,27,10);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
          break;
      case STAGE_6:
          uint8_t buffer6[27]="stage6\n\r";
          HAL_UART_Transmit(&huart1,buffer6,27,10);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
          break;
      }
      HAL_Delay(1000);
    /* USER CODE END WHILE */

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


/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void) {
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  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_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
    Error_Handler();
  }
}

/**
  * @brief USART1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_USART1_UART_Init(void) {

  /* USER CODE BEGIN USART1_Init 0 */

  /* USER CODE END USART1_Init 0 */

  /* USER CODE BEGIN USART1_Init 1 */

  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart1) != HAL_OK) {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */

  /* USER CODE END USART1_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_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_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_6
                          |GPIO_PIN_7, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);

  /*Configure GPIO pins : PA1 PA2 PA3 PA6
                           PA7 */
  GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_6
                          |GPIO_PIN_7;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pin : PB0 */
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  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);

  /*Configure GPIO pins : PB1 PB2 PB10 */
  GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_10;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI0_1_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI0_1_IRQn);

  HAL_NVIC_SetPriority(EXTI2_3_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI2_3_IRQn);

  HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);

/* USER CODE BEGIN MX_GPIO_Init_2 */


/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
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 */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line) {
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

This is the output I get on the serial monitor:

stage0
110
stage6
100
101
001
101
100
101
100
101
stage2
001
011
001
011
001
011
001
011
001
011
001
011
010
stage5
stage5

which is totaly wrong. The motor only rotated two steps.

Here is update code please review it. should i have to put polarity changing part in interrupt handler function.

#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */


UART_HandleTypeDef huart1;

#define HALL_SENSOR_A_PIN GPIO_PIN_1
#define HALL_SENSOR_B_PIN GPIO_PIN_2
#define HALL_SENSOR_C_PIN GPIO_PIN_10
#define HALL_SENSOR_PORT GPIOB


enum MotorStage {
    STAGE_0,
    STAGE_1,
    STAGE_2,
    STAGE_3,
    STAGE_4,
    STAGE_5,
    STAGE_6
};


volatile enum MotorStage motor_stage=STAGE_0;

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {

    if ((GPIO_Pin == HALL_SENSOR_A_PIN)||(GPIO_Pin == HALL_SENSOR_B_PIN)||(GPIO_Pin == HALL_SENSOR_C_PIN))
       {
          uint8_t sensor_A = HAL_GPIO_ReadPin(HALL_SENSOR_PORT, HALL_SENSOR_A_PIN);
          uint8_t sensor_B = HAL_GPIO_ReadPin(HALL_SENSOR_PORT, HALL_SENSOR_B_PIN);
          uint8_t sensor_C = HAL_GPIO_ReadPin(HALL_SENSOR_PORT, HALL_SENSOR_C_PIN);

          uint8_t data_to_send[6];  // Assuming each sensor value can be represented by a single character
          data_to_send[0] = sensor_A ? '1' : '0';  // Convert sensor_A to '1' or '0'
          data_to_send[1] = sensor_B ? '1' : '0';  // Convert sensor_B to '1' or '0'
          data_to_send[2] = sensor_C ? '1' : '0';  // Convert sensor_C to '1' or '0'
          data_to_send[3] = '\n';  // Add newline character for formatting
          data_to_send[4] = '\r';  // Add carriage return character for formatting
          data_to_send[5] = '\0';  // Null terminator for string

                 // Transmit sensor values over UART
          HAL_UART_Transmit(&huart1, data_to_send, sizeof(data_to_send), 5);

        // Determine motor stage based on sensor inputs
          if (sensor_A && !sensor_B && !sensor_C) {
            motor_stage = STAGE_1;
          } else if (sensor_A && !sensor_B && sensor_C) {
            motor_stage = STAGE_2;
          } else if (!sensor_A && !sensor_B && sensor_C) {
            motor_stage = STAGE_3;
          } else if (!sensor_A && sensor_B && sensor_C) {
            motor_stage = STAGE_4;
          } else if (!sensor_A && sensor_B && !sensor_C) {
            motor_stage = STAGE_5;
          } else if (sensor_A && sensor_B && !sensor_C) {
            motor_stage = STAGE_6;
       }

    }
}
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
 

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
     switch(motor_stage)
     {
     case STAGE_0:
          uint8_t buffer[27]="stage0\n\r";
            
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
          break;
      case STAGE_1:
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);

        HAL_UART_Transmit(&huart1,buffer1,27,10);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
          break;
      case STAGE_2:
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
        
        HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
        HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
        break;
      case STAGE_3:
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
       
        HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
        break;
      case STAGE_4:
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
        
        HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
        break;
      case STAGE_5:
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
        
        HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
        break;
      case STAGE_6:
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
        
        HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_SET);
          HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
        break;
      }
     
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

Some thougts about possible issues her:

  • Well, this is a motor, and we expect hundreds of level changes at the hall sensor signals per second if it is spinning. So it is not useful to send serial data on each level change. You will miss many events because the serial transmitter code will block.
  • I see a HAL_Delay(1000) in your MotorStage handler. This might work if your motor rotates the seconds hand of a clock, but nothing much faster. This code misses nearly all Hall sensor events.
  • During a change in the motor stage you must turn off some FET and turn on others. For safety, first turn off all FETs and then turn on all FETs needed for that stage. Optimization can be done later. These HAL_GPIO_WritePin() calls need some time and during this there may occur a shoot through, where both FETs of a leg are conducting.
  • Check your output values. It does not look plausible to me, but without a schematic showing the connetions to the gate drivers and FETs there is not much to say.
\$\endgroup\$
1
  • \$\begingroup\$ hey jens. thank you . i dont have schematic because hardware was designed by someone else. let me tell you about connection. five N-channel FETs are used for each phase, two for the high side and three for the low side. low side are inverted .What i did is i just connect phase u high side fets to controller gpio output pin 1 and low side fets to controller gpio output 2. when I set the output pin 1 high and and output pin 2 high. So in result it activated the high side fets. i get output voltage on phase u. i have added another version. please review it and let me know anything to change \$\endgroup\$ Commented May 11 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.