cancel
Showing results for 
Search instead for 
Did you mean: 

generation of 3 digital signal at at time in STM3

saurabhkore
Associate III

Hello, I am working on the STM32L476RG MCU, where I first measure the angle using the atan2 function and then convert this measured angle into UVW signals.

As we know, UVW signals are digital in nature, switching high/low based on conditions specified in the code. For this, I used digital pins 8, 9, and 10 as GPIO output pins.

However, I observed that pins 9 and 10 provide the expected output, but pin 8 remains constantly high.

Why is this happening? I am sharing my code with you—please check it once

 

/* 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 ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
ADC_HandleTypeDef hadc2;

DAC_HandleTypeDef hdac1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void PeriphCommonClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
static void MX_ADC2_Init(void);
static void MX_DAC1_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();

  /* Configure the peripherals common clocks */
  PeriphCommonClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_ADC1_Init();
  MX_ADC2_Init();
  MX_DAC1_Init();
  /* USER CODE BEGIN 2 */
  HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
	                      HAL_ADC_Start(&hadc1);
	                      HAL_ADC_PollForConversion(&hadc2, 1);
	                      raw_sin=HAL_ADC_GetValue(&hadc1);
	                      HAL_ADC_Stop(&hadc1);

	  	  	  	          HAL_ADC_Start(&hadc2);
	  	  	  	    	  HAL_ADC_PollForConversion(&hadc1, 1);
	  	  	  	    	  raw_cosin=HAL_ADC_GetValue(&hadc2);
	  	  	  	    	  HAL_ADC_Stop(&hadc2);

	  	  	  	    	  vsin=(raw_sin*(3.3/4095)-1.721);
	  	  	  	    	  vcos=(raw_cosin*(3.3/4095)-1.7465);

	  	  	  	    	  angle=atan2(vsin,vcos);
	  	  	  	    	  angle=angle*(180.0f/M_PI);
	  	  	  	    	  if(angle < 0)
	  	  	  	    	  angle+=180;
	  	  	  	    	  dac_val=(uint32_t)((angle/180)*4095.0f);
	  	  	  	    	 HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, dac_val);
//for(angle=0;angle<360;angle++)
//{
	  	  	  	    	  if(angle >=0 && angle <60)
	  	  	  	    	  {
	  	  	  	    		  HAL_GPIO_WritePin(UVW_PORT, U_PIN, 1);
	  	  	  	    		  HAL_GPIO_WritePin(UVW_PORT, V_PIN, 0);
	  	  	  	    		  HAL_GPIO_WritePin(UVW_PORT, W_PIN, 1);
	  	  	  	    	  }
	  	  	  	    	  else if(angle >= 60 && angle <120)
	  	  	  	    	  {
	  	  	  	    		  HAL_GPIO_WritePin(UVW_PORT, U_PIN, 1);
	  	  	  	    		  HAL_GPIO_WritePin(UVW_PORT, V_PIN, 0);
	  	  	  	    		  HAL_GPIO_WritePin(UVW_PORT, W_PIN, 0);
	  	  	  	    	  }
	  	  	  	    	  else if(angle >= 120 && angle <180)
	  	  	  	    	  {
	  	  	  	    		  HAL_GPIO_WritePin(UVW_PORT, U_PIN, 1);
	  	  	  	    		  HAL_GPIO_WritePin(UVW_PORT, V_PIN, 1);
	  	  	  	    		  HAL_GPIO_WritePin(UVW_PORT, W_PIN, 0);
	  	  	  	    	  }
	  	  	  	    	  else if(angle >= 180 && angle <240)
	  	  	  	    	  {
	  	  	  	    		  HAL_GPIO_WritePin(UVW_PORT, U_PIN, 0);
	  	  	  	    		  HAL_GPIO_WritePin(UVW_PORT, V_PIN, 1);
	  	  	  	    		  HAL_GPIO_WritePin(UVW_PORT, W_PIN, 0);
	  	  	  	    	  }
	  	  	  	    	  else if(angle >= 240 && angle <300)
	  	  	  	    	  {
	  	  	  	    		  HAL_GPIO_WritePin(UVW_PORT, U_PIN, 0);
	  	  	  	    		  HAL_GPIO_WritePin(UVW_PORT, V_PIN, 1);
	  	  	  	    		  HAL_GPIO_WritePin(UVW_PORT, W_PIN, 1);
	  	  	  	    	  }
	  	  	  	    	  else if(angle >= 300 && angle <360)
	  	  	  	    	  {
	  	  	  	    		  HAL_GPIO_WritePin(UVW_PORT, U_PIN, 0);
	  	  	  	    		  HAL_GPIO_WritePin(UVW_PORT, V_PIN, 0);
	  	  	  	    		  HAL_GPIO_WritePin(UVW_PORT, W_PIN, 1);
	  	  	  	    	  }


}
	/*if (angle >= 0 && angle < 60) {
	        GPIOA->BSRR = (1U << 8) | (1U << (10));  // U = HIGH, W = HIGH
	        GPIOA->BSRR = (1U << (9 + 16));          // V = LOW
	    }
	    else if (angle >= 60 && angle < 120) {
	        GPIOA->BSRR = (1U << 8);  // U = HIGH
	        GPIOA->BSRR = (1U << (9 + 16)) | (1U << (10 + 16)); // V = LOW, W = LOW
	    }
	    else if (angle >= 120 && angle < 180) {
	        GPIOA->BSRR = (1U << 8) | (1U << 9);  // U = HIGH, V = HIGH
	        GPIOA->BSRR = (1U << (10 + 16));      // W = LOW
	    }
	    else if (angle >= 180 && angle < 240) {
	        GPIOA->BSRR = (1U << 9);  // V = HIGH
	        GPIOA->BSRR = (1U << (8 + 16)) | (1U << (10 + 16)); // U = LOW, W = LOW
	    }
	    else if (angle >= 240 && angle < 300) {
	        GPIOA->BSRR = (1U << 9) | (1U << 10);  // V = HIGH, W = HIGH
	        GPIOA->BSRR = (1U << (8 + 16));        // U = LOW
	    }
	    else if (angle >= 300 && angle < 360) {
	        GPIOA->BSRR = (1U << 10);  // W = HIGH
	        GPIOA->BSRR = (1U << (8 + 16)) | (1U << (9 + 16)); // U = LOW, V = LOW
	    }*/


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

  /** Configure the main internal regulator output voltage
  */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }

  /** 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 = 1;
  RCC_OscInitStruct.PLL.PLLN = 10;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  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_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

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

/**
  * @brief Peripherals Common Clock Configuration
  * @retval None
  */
void PeriphCommonClock_Config(void)
{
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Initializes the peripherals clock
  */
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_PLLSAI1;
  PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_HSI;
  PeriphClkInit.PLLSAI1.PLLSAI1M = 1;
  PeriphClkInit.PLLSAI1.PLLSAI1N = 8;
  PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV7;
  PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;
  PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
  PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_ADC1CLK;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief ADC1 Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_ADC1_Init(void)
{

  /* USER CODE BEGIN ADC1_Init 0 */

  /* USER CODE END ADC1_Init 0 */

  ADC_MultiModeTypeDef multimode = {0};
  ADC_ChannelConfTypeDef sConfig = {0};

  /* USER CODE BEGIN ADC1_Init 1 */

  /* USER CODE END ADC1_Init 1 */

  /** Common config
  */
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc1.Init.LowPowerAutoWait = DISABLE;
  hadc1.Init.ContinuousConvMode = ENABLE;
  hadc1.Init.NbrOfConversion = 1;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.DMAContinuousRequests = DISABLE;
  hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  hadc1.Init.OversamplingMode = DISABLE;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure the ADC multi-mode
  */
  multimode.Mode = ADC_MODE_INDEPENDENT;
  if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_5;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
  sConfig.SingleDiff = ADC_SINGLE_ENDED;
  sConfig.OffsetNumber = ADC_OFFSET_NONE;
  sConfig.Offset = 0;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN ADC1_Init 2 */

  /* USER CODE END ADC1_Init 2 */

}

/**
  * @brief ADC2 Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_ADC2_Init(void)
{

  /* USER CODE BEGIN ADC2_Init 0 */

  /* USER CODE END ADC2_Init 0 */

  ADC_ChannelConfTypeDef sConfig = {0};

  /* USER CODE BEGIN ADC2_Init 1 */

  /* USER CODE END ADC2_Init 1 */

  /** Common config
  */
  hadc2.Instance = ADC2;
  hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  hadc2.Init.Resolution = ADC_RESOLUTION_12B;
  hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE;
  hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc2.Init.LowPowerAutoWait = DISABLE;
  hadc2.Init.ContinuousConvMode = ENABLE;
  hadc2.Init.NbrOfConversion = 1;
  hadc2.Init.DiscontinuousConvMode = DISABLE;
  hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc2.Init.DMAContinuousRequests = DISABLE;
  hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  hadc2.Init.OversamplingMode = DISABLE;
  if (HAL_ADC_Init(&hadc2) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_6;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
  sConfig.SingleDiff = ADC_SINGLE_ENDED;
  sConfig.OffsetNumber = ADC_OFFSET_NONE;
  sConfig.Offset = 0;
  if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN ADC2_Init 2 */

  /* USER CODE END ADC2_Init 2 */

}

/**
  * @brief DAC1 Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_DAC1_Init(void)
{

  /* USER CODE BEGIN DAC1_Init 0 */

  /* USER CODE END DAC1_Init 0 */

  DAC_ChannelConfTypeDef sConfig = {0};

  /* USER CODE BEGIN DAC1_Init 1 */

  /* USER CODE END DAC1_Init 1 */

  /** DAC Initialization
  */
  hdac1.Instance = DAC1;
  if (HAL_DAC_Init(&hdac1) != HAL_OK)
  {
    Error_Handler();
  }

  /** DAC channel OUT1 config
  */
  sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE;
  sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
  sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_ENABLE;
  sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;
  if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN DAC1_Init 2 */

  /* USER CODE END DAC1_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();


  /*Configure GPIO pin Output Level */
//  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10, GPIO_PIN_RESET);


  /*Configure GPIO pins : PA8 PA9 PA10 */
  GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
  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);
  HAL_GPIO_WritePin(UVW_PORT, U_PIN, GPIO_PIN_RESET);
  HAL_GPIO_WritePin(UVW_PORT, V_PIN, GPIO_PIN_RESET);
  HAL_GPIO_WritePin(UVW_PORT, W_PIN, GPIO_PIN_RESET);
  // Enable GPIOA clock
  RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;

//  // Set PA8, PA9, PA10 as outputs
//  GPIOA->MODER &= ~(3U << (8 * 2));  // Clear bits for PA8
//  GPIOA->MODER |= (1U << (8 * 2));   // Set as output
//
//  GPIOA->MODER &= ~(3U << (9 * 2));
//  GPIOA->MODER |= (1U << (9 * 2));
//
//  GPIOA->MODER &= ~(3U << (10 * 2));
//  GPIOA->MODER |= (1U << (10 * 2));
//
//  GPIOA->BSRR =(1U <<(8+16)) | (1U <<(9+16)) | (1U << (10+16));

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* 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 */

 

0 REPLIES 0