AnsweredAssumed Answered

STM32F407 TIM1_CH1N not working as PWM in complementary mode

Question asked by s.vinod on Jul 3, 2016
Latest reply on Jul 11, 2016 by s.vinod
Hi,
     I am trying to generate a complementary PWM output pair using TIM1 CH1 - CH1N pair.

  What every I do, always CH1 is only working and the CH1N is always in IDLE state regardless of what ever value in the TIM1->CCR1 register. The CH1 is working and giving PWM signal but at the same time I am expecting the CH1N to give inverted PWM. I didn't configured any dead time setting now. I generated the code from stm32cubeMX (latest version). If I change the   sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; etc, I can see the CH1N default stage is changing either to HIGH or LOW depending of the settings. But couldn't observe PWM signal as in CH1.

/**
  ******************************************************************************
  * File Name          : TIM.c
  * Date               : 23/04/2016 20:10:39
  * Description        : This file provides code for the configuration
  *                      of the TIM instances.
  ******************************************************************************
  *
  * COPYRIGHT(c) 2016 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
 
/* Includes ------------------------------------------------------------------*/
#include "tim.h"
 
#include "gpio.h"
 
/* USER CODE BEGIN 0 */
 
/* USER CODE END 0 */
 
TIM_HandleTypeDef htim1;
 
/* TIM1 init function */
void MX_TIM1_Init(void)
{
  TIM_ClockConfigTypeDef sClockSourceConfig;
  TIM_MasterConfigTypeDef sMasterConfig;
  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;
  TIM_OC_InitTypeDef sConfigOC;
 
  htim1.Instance = TIM1;
  htim1.Init.Prescaler = 0;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 256;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim1.Init.RepetitionCounter = 0;
  HAL_TIM_Base_Init(&htim1);
 
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig);
 
  HAL_TIM_PWM_Init(&htim1);
 
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
 
  sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
  sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
  sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
  sBreakDeadTimeConfig.DeadTime = 0;
  sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
  sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_LOW;
  sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
  HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig);
 
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 0;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1);
  HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
 
}
 
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
{
 
  GPIO_InitTypeDef GPIO_InitStruct;
  if(htim_base->Instance==TIM1)
  {
  /* USER CODE BEGIN TIM1_MspInit 0 */
 
  /* USER CODE END TIM1_MspInit 0 */
    /* Peripheral clock enable */
    __TIM1_CLK_ENABLE();
   
    /**TIM1 GPIO Configuration   
    PE8     ------> TIM1_CH1N
    PE9     ------> TIM1_CH1
    */
    GPIO_InitStruct.Pin = GPIO_PIN_8;//|GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
    GPIO_InitStruct.Pin = GPIO_PIN_13;//|GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
    /* USER CODE BEGIN TIM1_MspInit 1 */
 
  /* USER CODE END TIM1_MspInit 1 */
  }
}
 
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_pwm)
{
 
  GPIO_InitTypeDef GPIO_InitStruct;
  if(htim_pwm->Instance==TIM1)
  {
  /* USER CODE BEGIN TIM1_MspInit 0 */
 
  /* USER CODE END TIM1_MspInit 0 */
    /* Peripheral clock enable */
    __TIM1_CLK_ENABLE();
   
    /**TIM1 GPIO Configuration   
    PE8     ------> TIM1_CH1N
    PE9     ------> TIM1_CH1
    */
//    GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
//    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
//    GPIO_InitStruct.Pull = GPIO_NOPULL;
//    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
//    GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
//    HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
 
 
  /* USER CODE BEGIN TIM1_MspInit 1 */
 
  /* USER CODE END TIM1_MspInit 1 */
  }
}
 
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
{
 
  if(htim_base->Instance==TIM1)
  {
  /* USER CODE BEGIN TIM1_MspDeInit 0 */
 
  /* USER CODE END TIM1_MspDeInit 0 */
    /* Peripheral clock disable */
    __TIM1_CLK_DISABLE();
   
    /**TIM1 GPIO Configuration   
    PE8     ------> TIM1_CH1N
    PE9     ------> TIM1_CH1
    */
    HAL_GPIO_DeInit(GPIOE, GPIO_PIN_8|GPIO_PIN_9);
 
  /* USER CODE BEGIN TIM1_MspDeInit 1 */
 
  /* USER CODE END TIM1_MspDeInit 1 */
  }
}
 
void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* htim_pwm)
{
 
  if(htim_pwm->Instance==TIM1)
  {
  /* USER CODE BEGIN TIM1_MspDeInit 0 */
 
  /* USER CODE END TIM1_MspDeInit 0 */
    /* Peripheral clock disable */
    __TIM1_CLK_DISABLE();
   
    /**TIM1 GPIO Configuration   
    PE8     ------> TIM1_CH1N
    PE9     ------> TIM1_CH1
    */
    HAL_GPIO_DeInit(GPIOE, GPIO_PIN_8|GPIO_PIN_9);
 
  /* USER CODE BEGIN TIM1_MspDeInit 1 */
 
  /* USER CODE END TIM1_MspDeInit 1 */
  }
}
 
/* USER CODE BEGIN 1 */
 
/* USER CODE END 1 */
 
/**
  * @}
  */
 
/**
  * @}
  */
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

001./**
002.  ******************************************************************************
003.  * File Name          : main.c
004.  * Date               : 23/04/2016 20:10:40
005.  * Description        : Main program body
006.  ******************************************************************************
007.  *
008.  * COPYRIGHT(c) 2016 STMicroelectronics
009.  *
010.  * Redistribution and use in source and binary forms, with or without modification,
011.  * are permitted provided that the following conditions are met:
012.  *   1. Redistributions of source code must retain the above copyright notice,
013.  *      this list of conditions and the following disclaimer.
014.  *   2. Redistributions in binary form must reproduce the above copyright notice,
015.  *      this list of conditions and the following disclaimer in the documentation
016.  *      and/or other materials provided with the distribution.
017.  *   3. Neither the name of STMicroelectronics nor the names of its contributors
018.  *      may be used to endorse or promote products derived from this software
019.  *      without specific prior written permission.
020.  *
021.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
022.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
023.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
024.  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
025.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
026.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
027.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
028.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
029.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
030.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031.  *
032.  ******************************************************************************
033.  */
034. 
035./* Includes ------------------------------------------------------------------*/
036.#include "stm32f4xx_hal.h"
037.#include "adc.h"
038.#include "tim.h"
039.#include "gpio.h"
040. 
041.uint32_t adv;
042. 
043./* USER CODE BEGIN Includes */
044. 
045./* USER CODE END Includes */
046. 
047./* Private variables ---------------------------------------------------------*/
048. 
049./* USER CODE BEGIN PV */
050. 
051./* USER CODE END PV */
052. 
053./* Private function prototypes -----------------------------------------------*/
054.void SystemClock_Config(void);
055. 
056./* USER CODE BEGIN PFP */
057. 
058./* USER CODE END PFP */
059. 
060./* USER CODE BEGIN 0 */
061. 
062./* USER CODE END 0 */
063. 
064.int main(void)
065.{
066. 
067.  /* USER CODE BEGIN 1 */
068. 
069.  /* USER CODE END 1 */
070. 
071.  /* MCU Configuration----------------------------------------------------------*/
072. 
073.  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
074.  HAL_Init();
075. 
076.  /* Configure the system clock */
077.  SystemClock_Config();
078. 
079.  /* Initialize all configured peripherals */
080.  MX_GPIO_Init();
081.  MX_ADC1_Init();
082.  MX_TIM1_Init();
083. 
084.  /* USER CODE BEGIN 2 */
085. 
086.  /* USER CODE END 2 */
087. 
088.  /* Infinite loop */
089.  /* USER CODE BEGIN WHILE */
090.  while (1)
091.  {
092.  /* USER CODE END WHILE */
093. //   HAL_ADC_Start(&hadc1);
094.  while(HAL_ADC_PollForConversion(&hadc1, 1000) != HAL_OK);
095.  adv = HAL_ADC_GetValue(&hadc1);
096.  TIM1->CCR1 = (adv>>4);
097.  
098.  /* USER CODE BEGIN 3 */
099. 
100.  }
101.  /* USER CODE END 3 */
102. 
103.}
104. 
105./** System Clock Configuration
106.*/
107.void SystemClock_Config(void)
108.{
109. 
110.  RCC_OscInitTypeDef RCC_OscInitStruct;
111.  RCC_ClkInitTypeDef RCC_ClkInitStruct;
112. 
113.  __PWR_CLK_ENABLE();
114. 
115.  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
116. 
117.  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
118.  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
119.  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
120.  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
121.  RCC_OscInitStruct.PLL.PLLM = 8;
122.  RCC_OscInitStruct.PLL.PLLN = 336;
123.  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
124.  RCC_OscInitStruct.PLL.PLLQ = 7;
125.  HAL_RCC_OscConfig(&RCC_OscInitStruct);
126. 
127.  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
128.                              |RCC_CLOCKTYPE_PCLK2;
129.  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
130.  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
131.  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
132.  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
133.  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
134. 
135.}
136. 
137./* USER CODE BEGIN 4 */
138. 
139./* USER CODE END 4 */
140. 
141.#ifdef USE_FULL_ASSERT
142. 
143./**
144.   * @brief Reports the name of the source file and the source line number
145.   * where the assert_param error has occurred.
146.   * @param file: pointer to the source file name
147.   * @param line: assert_param error line source number
148.   * @retval None
149.   */
150.void assert_failed(uint8_t* file, uint32_t line)
151.{
152.  /* USER CODE BEGIN 6 */
153.  /* User can add his own implementation to report the file name and line number,
154.    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
155.  /* USER CODE END 6 */
156. 
157.}
158. 
159.#endif
160. 
161./**
162.  * @}
163.  */
164. 
165./**
166.  * @}
167.*/
168. 
169./************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Outcomes