Skip to main content
Visitor II
August 29, 2024
Question

I was trying to write code for my custom esc but i am getting issue in it. can someone help me in solving the problem

  • August 29, 2024
  • 3 replies
  • 903 views

 

/* 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 */

/* USER CODE END Includes */

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

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define TIMCLOCK 12500000
#define PRESCALAR 250

/* USER CODE END PD */

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

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim1;
TIM_HandleTypeDef htim3;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

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

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/*---------------------------------------------------------------------------*/
uint8_t current_step = 0;

uint32_t dutyCycle = 0;
uint32_t pulseWidth; 
uint32_t compareValue = 0;

volatile uint8_t newDataAvailable = 0;

uint32_t IC_Val1 = 0;
uint32_t IC_Val2 = 0;
uint32_t Difference = 0;
int Is_First_Captured = 0;

volatile uint32_t usWidth = 0;

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
	if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) // if the interrupt source is channel1
	{
		if (Is_First_Captured==0) // if the first value is not captured
		{
			IC_Val1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); // read the first value
			Is_First_Captured = 1; // set the first captured as true
		}

		else // if the first is already captured
		{
			IC_Val2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); // read second value

			if (IC_Val2 > IC_Val1)
			{
				Difference = IC_Val2-IC_Val1;
			}

			else if (IC_Val1 > IC_Val2)
			{
				Difference = (0xffffffff - IC_Val1) + IC_Val2;
			}

			float refClock = TIMCLOCK/(PRESCALAR);
			float mFactor = 1000000/refClock;

			usWidth = Difference*mFactor;
			
			newDataAvailable = 1;
						
			__HAL_TIM_SET_COUNTER(htim, 0); // reset the counter
			Is_First_Captured = 0; // set it back to false
		}
	}
}

/*************************** Commutation step function ************************************/
void AH_BL()
{
	// This is step 1 of commutation
	
	// Set all channels to 0 first
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 0);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 0);
 __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, 0);
	
	// Enable TIM1 CH1 (Phase A High-Side)
	HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, compareValue); // Phase A High-Side ON
	
	
	// Enable TIM2 CH2N (Phase B Low-Side)
	HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2); 
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, compareValue); // Phase B Low-Side ON

	
	// Disable other channels
	HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_2); // Phase B High-Side OFF
	HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1); // Phase A Low-Side OFF
	HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_3); // Phase C High-Side OFF
	HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3); // Phase C Low-Side OFF
}

void AH_CL()
{
	// This is step 2 of commutation
	
	// Set all channels to 0 first
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 0);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 0);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, 0);
	
	
	// Enable TIM1 CH1 (Phase A High-Side)
	HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, compareValue); // Phase A High-Side ON

	
	// Enable TIM3 CH2N (Phase C Low-Side)
	HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3); 
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, compareValue); // Phase C Low-Side ON

	
	// Disable other channels
	HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_3); // Phase C High-Side OFF
	HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1); // Phase A Low-Side OFF
	HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_2); // Phase B High-Side OFF
	HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2); // Phase B Low-Side OFF
}

void BH_CL()
{
	// This is step 3 of commutation
	
	// Set all channels to 0 first
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 0);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 0);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, 0);
	
	// Enable TIM2 CH1 (Phase B High-Side)
	HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, compareValue); // Phase B High-Side ON

	
	// Enable TIM3 CH2N (Phase C Low-Side)
	HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3); 
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, compareValue); // Phase C Low-Side ON

	
	// Disable other channels
	HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_3); // Phase C High-Side OFF
	HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2); // Phase B Low-Side OFF
	HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_1); // Phase A High-Side OFF
	HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1); // Phase A Low-Side OFF
}

void BH_AL()
{
	// This is step 4 of commutation
	
	// Set all channels to 0 first
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 0);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 0);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, 0);
	
	// Enable TIM2 CH1 (Phase B High-Side)
	HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, compareValue); // Phase B High-Side ON

	
	// Enable TIM1 CH2N (Phase A Low-Side)
	HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1); 
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, compareValue); // Phase A Low-Side ON

	
	// Disable other channels
	HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_1); // Phase A High-Side OFF
	HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2); // Phase B Low-Side OFF
	HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_3); // Phase C High-Side OFF
	HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3); // Phase C Low-Side OFF
}

void CH_AL()
{
	// This is step 5 of commutation
	
	// Set all channels to 0 first
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 0);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 0);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, 0);
	
	// Enable TIM3 CH1 (Phase C High-Side)
	HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, compareValue); // Phase C High-Side ON

	
	// Enable TIM1 CH2N (Phase A Low-Side)
	HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1); 
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, compareValue); // Phase A Low-Side ON

	
	// Disable other channels
	HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_1); // Phase A High-Side OFF
	HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3); // Phase C Low-Side OFF
	HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_2); // Phase B High-Side OFF
	HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2); // Phase B Low-Side OFF
}

void CH_BL()
{
	// This is step 6 of commutation
	
	// Set all channels to 0 first
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 0);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 0);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, 0);
	
	// Enable TIM3 CH1 (Phase C High-Side)
	HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, compareValue); // Phase C High-Side ON

	
	// Enable TIM2 CH2N (Phase B Low-Side)
	HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2); 
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, compareValue); // Phase B Low-Side ON

	
	// Disable other channels
	HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_2); // Phase B High-Side OFF
	HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3); // Phase C Low-Side OFF
	HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_1); // Phase A High-Side OFF
	HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1); // Phase A Low-Side OFF
}

/********************************* Commutation Step function End ****************************************************/

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
//	if (isOpenLoop) return; // Ignore EXTI during open-loop startup
	
	if(GPIO_Pin == PHASE_A_EXTI_Pin)
	{
		if(HAL_GPIO_ReadPin(PHASE_A_EXTI_GPIO_Port,PHASE_A_EXTI_Pin) == GPIO_PIN_SET)
		{
			// Rising Edge
			CH_BL(); // Step 6
			current_step = 6;
		}
		else
		{
			// Falling Edge
			BH_CL(); // Step 3
			current_step = 3;
		}
	}
	else if(GPIO_Pin == PHASE_B_EXTI_Pin)
	{
		if(HAL_GPIO_ReadPin(PHASE_B_EXTI_GPIO_Port,PHASE_B_EXTI_Pin) == GPIO_PIN_SET)
		{
			// Rising Edge
			AH_CL(); // Step 2
			current_step = 2;
		}
		else
		{
			// Falling Edge
			CH_AL(); // Step 5
			current_step = 5;
		}
	}
	else if(GPIO_Pin == PHASE_C_EXTI_Pin)
	{
		if(HAL_GPIO_ReadPin(PHASE_C_EXTI_GPIO_Port,PHASE_C_EXTI_Pin) == GPIO_PIN_SET)
		{
			// Rising Edge
			BH_AL(); // Step4
			current_step = 4;
		}
		else
		{
			// Falling Edge
			AH_BL(); // Step 1
			current_step = 1;
		}
	}
}

void openLoopCommute(uint8_t step)
{
	 // Set all channels to 0 before applying the new step
 __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 0);
 __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 0);
 __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, 0);
	
		switch (step)
		{
			case 1:
				AH_BL();
				break;
			case 2:
				AH_CL();
				break;
			case 3:
				BH_CL();
				break;
			case 4:
				BH_AL();
				break;
			case 5:
				CH_AL();
				break;
			case 6:
				CH_BL();
				break;
			case 7:
				break;
		}
//		HAL_Delay(10);
}



/*---------------------------------------------------------------------------*/
/* 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_TIM1_Init();
 MX_TIM3_Init();
 /* USER CODE BEGIN 2 */
	HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1);
	
	//	
	
	HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
	HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
	
	
	HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
	HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);
	
	
	HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
	HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3);
	

//	uint32_t compareValue = 0;

	int counter = 0;
	
	
	
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 0);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 0);
	__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, 0);
	
	
	
 /* USER CODE END 2 */

 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
 /* USER CODE END WHILE */

 /* USER CODE BEGIN 3 */
		/**/
		if (newDataAvailable) 
		{
							newDataAvailable = 0; // Reset the flag
							
							// Map input PWM (0.998ms to 2ms) to TIM1 duty cycle (0 to 999)
							if (usWidth >= 998 && usWidth <= 2000) {
									compareValue = ((usWidth - 998) * 999) / (2000 - 998);
							} else if (usWidth < 998) {
									compareValue = 0;
							} else {
									compareValue = 999;
							}
							
//							__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, compareValue);
//							__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, compareValue);
//							__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, compareValue);
							
//							AH_BL();
//							HAL_Delay(1000);
//							AH_CL();
//							HAL_Delay(1000);
//								openLoopCommute(counter + 1);
//								counter = (counter + 1) % 6;
				
		}
		/**/
 }
 /* USER CODE END 3 */
}

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

 /** Configure the main internal regulator output voltage
 */
 __HAL_RCC_PWR_CLK_ENABLE();
 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

 /** 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.PLLM = 8;
 RCC_OscInitStruct.PLL.PLLN = 100;
 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
 RCC_OscInitStruct.PLL.PLLQ = 4;
 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_CLOCKTYPE_PCLK2;
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV16;
 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
 {
 Error_Handler();
 }
}

/**
 * @brief TIM1 Initialization Function
 * @PAram None
 * @retval None
 */
static void MX_TIM1_Init(void)
{

 /* USER CODE BEGIN TIM1_Init 0 */

 /* USER CODE END TIM1_Init 0 */

 TIM_MasterConfigTypeDef sMasterConfig = {0};
 TIM_OC_InitTypeDef sConfigOC = {0};
 TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};

 /* USER CODE BEGIN TIM1_Init 1 */

 /* USER CODE END TIM1_Init 1 */
 htim1.Instance = TIM1;
 htim1.Init.Prescaler = 10-1;
 htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
 htim1.Init.Period = 1000-1;
 htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 htim1.Init.RepetitionCounter = 0;
 htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
 {
 Error_Handler();
 }
 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
 {
 Error_Handler();
 }
 sConfigOC.OCMode = TIM_OCMODE_PWM1;
 sConfigOC.Pulse = 0;
 sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
 sConfigOC.OCNPolarity = TIM_OCNPOLARITY_LOW;
 sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
 sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
 sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
 if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
 {
 Error_Handler();
 }
 if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
 {
 Error_Handler();
 }
 if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
 {
 Error_Handler();
 }
 sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
 sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
 sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
 sBreakDeadTimeConfig.DeadTime = 80;
 sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
 sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
 sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
 if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN TIM1_Init 2 */

 /* USER CODE END TIM1_Init 2 */
 HAL_TIM_MspPostInit(&htim1);

}

/**
 * @brief TIM3 Initialization Function
 * @PAram None
 * @retval None
 */
static void MX_TIM3_Init(void)
{

 /* USER CODE BEGIN TIM3_Init 0 */

 /* USER CODE END TIM3_Init 0 */

 TIM_MasterConfigTypeDef sMasterConfig = {0};
 TIM_IC_InitTypeDef sConfigIC = {0};

 /* USER CODE BEGIN TIM3_Init 1 */

 /* USER CODE END TIM3_Init 1 */
 htim3.Instance = TIM3;
 htim3.Init.Prescaler = 250-1;
 htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
 htim3.Init.Period = 1000-1;
 htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 if (HAL_TIM_IC_Init(&htim3) != HAL_OK)
 {
 Error_Handler();
 }
 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
 {
 Error_Handler();
 }
 sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_BOTHEDGE;
 sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
 sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
 sConfigIC.ICFilter = 0;
 if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN TIM3_Init 2 */

 /* USER CODE END TIM3_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_GPIOH_CLK_ENABLE();
 __HAL_RCC_GPIOA_CLK_ENABLE();
 __HAL_RCC_GPIOB_CLK_ENABLE();

 /*Configure GPIO pins : PHASE_A_EXTI_Pin PHASE_B_EXTI_Pin PHASE_C_EXTI_Pin */
 GPIO_InitStruct.Pin = PHASE_A_EXTI_Pin|PHASE_B_EXTI_Pin|PHASE_C_EXTI_Pin;
 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(EXTI15_10_IRQn, 1, 0);
 HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);

/* USER CODE BEGIN MX_GPIO_Init_2 */
//	 /*Configure GPIO pin : PA8 (TIM1_CH1) */
// GPIO_InitStruct.Pin = GPIO_PIN_8;
// GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
// GPIO_InitStruct.Pull = GPIO_PULLDOWN;
// GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
// GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
// HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */

/* 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 topic has been closed for replies.

    3 replies

    Shubham08Author
    Visitor II
    August 29, 2024

    This is the extended explanation for the problem and above is the code which i have tried, due to messge limit i have extended this

    I have tried to write code in stm32f411ceu6 (blackpill) for generating PWM pulse which will be given to my esc using ir2101. I am using stm32cubeMX for generating code. In stm32cubeMX i have initailized TIM 1 , CH1,CH2, CH3 with complementry mode, pin as Pulldowm, which will be given to three ir2101 HIN and LIN pin for turning on and off of n channel mosfet. The issue i am getting is in the code the TIM1 CH1 pin is pulled to high and after two step it should turn off the PWM but it generate dummy pulse which lead B phase mosfet to heat. when i set the dutycycle to 10 then CH1 duty cycle is 10 and CH1N is 90 but this is not turning mosfet correctly, I need is both CH1 and CH1N duty cycle should be 10. 

    Super User
    August 29, 2024

    >  __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 0);

    Do you ever set the value to something other than 0? I can't see any places where this is done. Setting CCR1 to 0 will output a DC signal, not a pulse.

    If your period is 1000 and you want a 10% duty cycle, set CCR1 to 100.

    Shubham08Author
    Visitor II
    August 30, 2024

    Initially i am making 0 and then setting the value calculated from compareValue in it