stm32f103 pwm complementary cikisi bir turlu aktif olmuyor.(DMA)

Başlatan Yuunus, 14 Nisan 2022, 01:06:33

Yuunus

cube ide ve hal kutuphanesi ile yaptigim pwm uygulamasinda ne yaptimsa pwm in complement cikisini aktif edemedim, pwm'i tim1 ile kurdum iki adet pwm generator cikisim var, iki de complement olmasi gerekirken bir turlu aktif olmuyor.










/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 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 */
/* USER CODE END PD */

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

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
 TIM_HandleTypeDef htim1;
DMA_HandleTypeDef hdma_tim1_ch1;
DMA_HandleTypeDef hdma_tim1_ch2;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_TIM1_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 */
	const uint16_t dizi[201]={100,103,106,109,112,115,119,122,125,128,
			                     131,134,136,139,142,145,148,150,153,
								 156,158,161,163,165,168,170,172,
				                 174,176,178,180,182,184,185,187,188,
			                     190,191,192,193,194,195,196,197,197,
				                 198,198,199,199,199,199,199,199,199,
				                 198,198,197,197,196,195,194,193,192,
				                 191,190,188,187,185,184,182,180,178,
				                 176,174,172,170,168,165,163,161,158,
				                 156,153,150,148,145,142,139,136,134,
				                 131,128,125,122,119,115,112,109,106,
				                 103,100,97,94,91,88,85,81,78,
				                 75,72,69,66,64,61,58,55,52,
				                 50,47,44,42,39,37,35,32,30,
				                 28,26,24,22,20,18,16,15,13,
				                 12,10,9,8,7,6,5,4,3,
				                 3,2,2,1,1,1,1,1,1,
				                 1,2,2,3,3,4,5,6,7,
				                 8,9,10,12,13,15,16,18,20,
				                 22,24,26,28,30,32,35,37,39,
				                 42,44,47,50,52,55,58,61,64,
				                 66,69,72,75,78,81,85,88,91,
				                 94,97,100};


	const uint16_t dizi2[201]={100,97,94,91,88,85,81,78,75,
			                  72,69,66,64,61,58,55,52,50,
			                  47,44,42,39,37,35,32,30,28,
			                  26,24,22,20,18,16,15,13,12,
			                  10,9,8,7,6,5,4,3,3,
			                  2,2,1,1,1,1,1,1,1,
			                  2,2,3,3,4,5,6,7,8,
			                  9,10,12,13,15,16,18,20,22,
			                  24,26,28,30,32,35,37,39,42,
			                  44,47,50,52,55,58,61,64,66,
			                  69,72,75,78,81,85,88,91,94,
			                  97,100,103,106,109,112,115,119,122,
			                  125,128,131,134,136,139,142,145,148,
			                  150,153,156,158,161,163,165,168,170,
			                  172,174,176,178,180,182,184,185,187,
			                  188,190,191,192,193,194,195,196,197,
			                  197,198,198,199,199,199,199,199,199,
			                  199,198,198,197,197,196,195,194,193,
			                  192,191,190,188,187,185,184,182,180,
			                  178,176,174,172,170,168,165,163,161,
			                  158,156,153,150,148,145,142,139,136,
			                  134,131,128,125,122,119,115,112,109,
			                  106,103,100};
  /* 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_DMA_Init();
  MX_TIM1_Init();
  /* USER CODE BEGIN 2 */

  HAL_TIM_PWM_Start_DMA(&htim1,TIM_CHANNEL_1,(uint32_t*)dizi2,201);
  HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);

  HAL_TIM_PWM_Start_DMA(&htim1,TIM_CHANNEL_2,(uint32_t*)dizi,201);
  HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);



  GPIOC->ODR &=0x20000000;//SET bit
  GPIOC->ODR |=0x20000000;//CLEAR bit

  //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);


  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* 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};

  /** 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_DIV2;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  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_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != 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 = (12-1);
  htim1.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED2;
  htim1.Init.Period = (200-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_PWM2;
  sConfigOC.Pulse = 0;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
  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();
  }
  sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
  sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
  sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
  sBreakDeadTimeConfig.DeadTime = 50;
  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 */
/*
	//TIM_BDTRInitStructure.TIM_OSSIState=tim_oss
	TIM1->BDTR=0x00;//ALL BIT CLEAR
	TIM1->BDTR|=(1<<11);//OSSR BIT CLEAR
	TIM1->BDTR|=(1<<10);//OSSI BIT CLEAR
	TIM1->BDTR&=0xFFFFFF00;
	TIM1->BDTR|=0x00000080;
	TIM1->BDTR|=500;
	TIM1->BDTR|=(1<<15);//MOE
*/
  /* USER CODE END TIM1_Init 2 */
  HAL_TIM_MspPostInit(&htim1);

}

/**
  * Enable DMA controller clock
  */
static void MX_DMA_Init(void)
{

  /* DMA controller clock enable */
  __HAL_RCC_DMA1_CLK_ENABLE();

  /* DMA interrupt init */
  /* DMA1_Channel2_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn);
  /* DMA1_Channel3_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel3_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel3_IRQn);

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);

  /*Configure GPIO pin : PC13 */
  GPIO_InitStruct.Pin = GPIO_PIN_13;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

}

/* 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 */
dead time koymak istemiyorsaniz bu satiri bulup(sBreakDeadTimeConfig.DeadTime = 50;) "0" yapin

Yuunus

peki elinizde stm32f103 icin complement cikisida olan bir ornek varmi, register seviyeside olur.

magnetron

hocam belki biliyorsunuz

kullanıcılar > siz > stm32cube > repository de örnek programlar var

orda olabilir

Yuunus

anlamadim ki st nin kendi videosunda da ayni yapilandirma farkli mcular icin var ama gel gorki bende iki pwm de calisiyor ama pwm_n ler bombos.bir de 32f4 disco ile deneyecegim bakalim onda calisacak mi.

Yuunus

sorun benim donanimdaymis bir ara mcu yu sokup tekrar lehimlemistim, padlar lehimi tam almamis, mcu nun uzerine bastirinca sinyaller geldi, kodda da bir iki noktayi degistirdim, H bridge ile sinus olusturmak isteyen arkadas olursa kullanabilir, zaten cubeide baya bir kolaylastirmis isleri, yukaridaki kod kisaca DMA vasitasi ile dizideki degerleri 2 adet pwm cikisina aktariyor, toplamda iki adet pwm cikisi(ana cikislar PA8 ve PA9), complementary cikislari sirasiyla(PA7 ve PB0) olmak uzere 4 adet cikisa sahip, complementary cikislari kullanmak istemez ve bu isi bir degil kapisina yaptirmak isterseniz siz bilirsiniz ama dead time ekleme isini donanima nasil yaptirirsiniz orasini bilmem.