AnsweredAssumed Answered

TIM1 PWM output on pin (cube HAL)

Question asked by bamboosso on Dec 9, 2015
Latest reply on Dec 10, 2015 by bamboosso
Hello,

I have configured TIM1 as PWM source and enabled it. Its working becouse TIM1->CNT is incrementing and I have blinking LED based on value of TIM1->CNT. But now I need output it on PIN and its not workig. My configuration (generated mostly by CubeMX):

[code]TIM_HandleTypeDef htim1;


/* TIM1 init function */
void MX_TIM1_Init(void)
{
     GPIO_InitTypeDef GPIO_InitStruct;
     /* USER CODE BEGIN TIM1_MspInit 0 */


     /* USER CODE END TIM1_MspInit 0 */
     /* Peripheral clock enable */
     __TIM1_CLK_ENABLE();


     /**TIM1 GPIO Configuration
     PE12     ------> TIM1_CH3N
     PE13     ------> TIM1_CH3
     */
     GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;
     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
     GPIO_InitStruct.Pull = GPIO_NOPULL;
     GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
     GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
     HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);


  TIM_MasterConfigTypeDef sMasterConfig;
  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;
  TIM_OC_InitTypeDef sConfigOC;


  __HAL_RCC_TIM1_CLK_ENABLE();
  htim1.Instance = TIM1;
  htim1.Init.Prescaler = 1000;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 8399;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim1.Init.RepetitionCounter = 0;
  HAL_TIM_OC_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_HIGH;
//  sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
//  HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig);


  sConfigOC.OCMode = TIM_OCMODE_PWM2;
  sConfigOC.Pulse = 4000;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
  sConfigOC.OCNPolarity = TIM_OCNPOLARITY_LOW;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  sConfigOC.OCIdleState = TIM_OCIDLESTATE_SET;
  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_SET;
  HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3);
}


void HAL_TIM_OC_MspInit(TIM_HandleTypeDef* htim_oc)
{


  GPIO_InitTypeDef GPIO_InitStruct;
  if(htim_oc->Instance==TIM1)
  {
  /* USER CODE BEGIN TIM1_MspInit 0 */


  /* USER CODE END TIM1_MspInit 0 */
    /* Peripheral clock enable */
    __TIM1_CLK_ENABLE();
  
    /**TIM1 GPIO Configuration    
    PE12     ------> TIM1_CH3N
    PE13     ------> TIM1_CH3 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;
    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 */
  }
}[/code]

and main function:

[code]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();


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


  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM1_Init();


  HAL_TIM_OC_MspInit(&htim1);


  __HAL_RCC_GPIOE_CLK_ENABLE();


  __HAL_TIM_ENABLE(&htim1);
  /* USER CODE BEGIN 2 */


  /* USER CODE END 2 */


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


       if(TIM1->CNT > 4000)
       {
            HAL_GPIO_WritePin(GPIOE, LEDG_Pin, 0);
       }
       else
       {
            HAL_GPIO_WritePin(GPIOE, LEDG_Pin, 1);
       }
  /* USER CODE BEGIN 3 */


  }
  /* USER CODE END 3 */


}[code]

What am I missing?

Thanks for any help.

Outcomes