AnsweredAssumed Answered

STM8 PWM output TIM2_CH3 over PD2

Question asked by boeni.franz on Dec 10, 2015
Latest reply on Feb 22, 2016 by kasar.sunil
Dear all
TIM2_CH3 normally is connected to PA3. But PD2 has an alternate function for this PWM output. I cannot find any notice in the manual how to activate TIM2_CH3 over GPIOD_2.
Here is my code which is working
TIM2_CH1 --> PD4
TIM2_CH2 --> PD3
TIM2_CH3 --> PA3

/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
#include "stm8s_tim2.h"
#include "stm8s_gpio.h"
#include "main.h"
#include "stm8s_conf.h"

/****************************************************************************/
/* Definitions                                                              */
/****************************************************************************/
#define MAX_PWM   255

/*** EXTERN FUNCTION, VARAIBLES *********************************************/
/****************************************************************************/
/* Variables                                                                */
/****************************************************************************/
//stControlParameter  ControlPar = stControlParameter_default;

uint16_t CCR21_Val  = 50;
uint16_t CCR22_Val  = 100;
uint16_t CCR23_Val  = 200;
/**********************************************************************



/**
  * @brief  Initializes the TIM2 Time Base Unit according to the specified parameters.
  * @param    TIM2_Prescaler specifies the Prescaler from TIM2_Prescaler_TypeDef.
  * @param    TIM2_Period specifies the Period value.
  * @retval None
  */
void TIM2_TimeBaseInit( TIM2_Prescaler_TypeDef TIM2_Prescaler,
                        uint16_t TIM2_Period)
{
    /* Set the Prescaler value */
    TIM2->PSCR = (uint8_t)(TIM2_Prescaler);
    /* Set the Autoreload value */
    TIM2->ARRH = (uint8_t)(TIM2_Period >> 8);
    TIM2->ARRL = (uint8_t)(TIM2_Period);
}

/**
  * @brief  Initializes the TIM2 Channel1 according to the specified parameters.
  * @param   TIM2_OCMode specifies the Output Compare mode  from @ref TIM2_OCMode_TypeDef.
  * @param   TIM2_OutputState specifies the Output State  from @ref TIM2_OutputState_TypeDef.
  * @param   TIM2_Pulse specifies the Pulse width  value.
  * @param   TIM2_OCPolarity specifies the Output Compare Polarity  from @ref TIM2_OCPolarity_TypeDef.
  * @retval None
  */
void TIM2_OC1Init(TIM2_OCMode_TypeDef TIM2_OCMode,
                  TIM2_OutputState_TypeDef TIM2_OutputState,
                  uint16_t TIM2_Pulse,
                  TIM2_OCPolarity_TypeDef TIM2_OCPolarity)
{
    /* Check the parameters */
    //assert_param(IS_TIM2_OC_MODE_OK(TIM2_OCMode));
   // assert_param(IS_TIM2_OUTPUT_STATE_OK(TIM2_OutputState));
 //   assert_param(IS_TIM2_OC_POLARITY_OK(TIM2_OCPolarity));

    /* Disable the Channel 1: Reset the CCE Bit, Set the Output State , the Output Polarity */
    TIM2->CCER1 &= (uint8_t)(~( TIM2_CCER1_CC1E | TIM2_CCER1_CC1P));
    /* Set the Output State &  Set the Output Polarity  */
    TIM2->CCER1 |= (uint8_t)((uint8_t)(TIM2_OutputState & TIM2_CCER1_CC1E ) |
                             (uint8_t)(TIM2_OCPolarity & TIM2_CCER1_CC1P));

    /* Reset the Output Compare Bits  & Set the Ouput Compare Mode */
    TIM2->CCMR1 = (uint8_t)((uint8_t)(TIM2->CCMR1 & (uint8_t)(~TIM2_CCMR_OCM)) |
                            (uint8_t)TIM2_OCMode);

    /* Set the Pulse value */
    TIM2->CCR1H = (uint8_t)(TIM2_Pulse >> 8);
    TIM2->CCR1L = (uint8_t)(TIM2_Pulse);
}


/**
  * @brief  Initializes the TIM2 Channel2 according to the specified parameters.
  * @param   TIM2_OCMode specifies the Output Compare mode  from @ref TIM2_OCMode_TypeDef.
  * @param   TIM2_OutputState specifies the Output State  from @ref TIM2_OutputState_TypeDef.
  * @param   TIM2_Pulse specifies the Pulse width  value.
  * @param   TIM2_OCPolarity specifies the Output Compare Polarity  from @ref TIM2_OCPolarity_TypeDef.
  * @retval None
  */
void TIM2_OC2Init(TIM2_OCMode_TypeDef TIM2_OCMode,
                  TIM2_OutputState_TypeDef TIM2_OutputState,
                  uint16_t TIM2_Pulse,
                  TIM2_OCPolarity_TypeDef TIM2_OCPolarity)
{
    /* Check the parameters */
    //assert_param(IS_TIM2_OC_MODE_OK(TIM2_OCMode));
    //assert_param(IS_TIM2_OUTPUT_STATE_OK(TIM2_OutputState));
  //  assert_param(IS_TIM2_OC_POLARITY_OK(TIM2_OCPolarity));


    /* Disable the Channel 1: Reset the CCE Bit, Set the Output State, the Output Polarity */
    TIM2->CCER1 &= (uint8_t)(~( TIM2_CCER1_CC2E |  TIM2_CCER1_CC2P ));
    /* Set the Output State & Set the Output Polarity */
    TIM2->CCER1 |= (uint8_t)((uint8_t)(TIM2_OutputState  & TIM2_CCER1_CC2E ) |
                        (uint8_t)(TIM2_OCPolarity & TIM2_CCER1_CC2P));


    /* Reset the Output Compare Bits & Set the Output Compare Mode */
    TIM2->CCMR2 = (uint8_t)((uint8_t)(TIM2->CCMR2 & (uint8_t)(~TIM2_CCMR_OCM)) |
                            (uint8_t)TIM2_OCMode);


    /* Set the Pulse value */
    TIM2->CCR2H = (uint8_t)(TIM2_Pulse >> 8);
    TIM2->CCR2L = (uint8_t)(TIM2_Pulse);
}


/**
  * @brief  Initializes the TIM2 Channel3 according to the specified parameters.
  * @param   TIM2_OCMode specifies the Output Compare mode from @ref TIM2_OCMode_TypeDef.
  * @param   TIM2_OutputState specifies the Output State from @ref TIM2_OutputState_TypeDef.
  * @param   TIM2_Pulse specifies the Pulse width value.
  * @param   TIM2_OCPolarity specifies the Output Compare Polarity  from @ref TIM2_OCPolarity_TypeDef.
  * @retval None
  */
void TIM2_OC3Init(TIM2_OCMode_TypeDef TIM2_OCMode,
                  TIM2_OutputState_TypeDef TIM2_OutputState,
                  uint16_t TIM2_Pulse,
                  TIM2_OCPolarity_TypeDef TIM2_OCPolarity)
{
    /* Check the parameters */
    //assert_param(IS_TIM2_OC_MODE_OK(TIM2_OCMode));
   // assert_param(IS_TIM2_OUTPUT_STATE_OK(TIM2_OutputState));
 //   assert_param(IS_TIM2_OC_POLARITY_OK(TIM2_OCPolarity));
    /* Disable the Channel 1: Reset the CCE Bit, Set the Output State, the Output Polarity */
    TIM2->CCER2 &= (uint8_t)(~( TIM2_CCER2_CC3E  | TIM2_CCER2_CC3P));
    /* Set the Output State & Set the Output Polarity */
    TIM2->CCER2 |= (uint8_t)((uint8_t)(TIM2_OutputState & TIM2_CCER2_CC3E) |  
                             (uint8_t)(TIM2_OCPolarity & TIM2_CCER2_CC3P));

    /* Reset the Output Compare Bits & Set the Output Compare Mode */
    TIM2->CCMR3 = (uint8_t)((uint8_t)(TIM2->CCMR3 & (uint8_t)(~TIM2_CCMR_OCM)) |
                            (uint8_t)TIM2_OCMode);

    /* Set the Pulse value */
    TIM2->CCR3H = (uint8_t)(TIM2_Pulse >> 8);
    TIM2->CCR3L = (uint8_t)(TIM2_Pulse);

}

/**
  * @brief  Enables or disables the TIM2 peripheral.
  * @param   NewState new state of the TIM2 peripheral. This parameter can
  * be ENABLE or DISABLE.
  * @retval None
  */
void TIM2_Cmd(FunctionalState NewState)
{
    /* Check the parameters */
   // assert_param(IS_FUNCTIONALSTATE_OK(NewState));

    /* set or Reset the CEN Bit */
    if (NewState != DISABLE)
    {
        TIM2->CR1 |= (uint8_t)TIM2_CR1_CEN;
    }
    else
    {
        TIM2->CR1 &= (uint8_t)(~TIM2_CR1_CEN);
    }
}


/**
  * @brief  Enables or disables TIM2 peripheral Preload register on ARR.
  * @param   NewState new state of the TIM2 peripheral Preload register.
  * This parameter can be ENABLE or DISABLE.
  * @retval None
  */
void TIM2_ARRPreloadConfig(FunctionalState NewState)
{
    /* Check the parameters */
 //  assert_param(IS_FUNCTIONALSTATE_OK(NewState));

    /* Set or Reset the ARPE Bit */
    if (NewState != DISABLE)
    {
        TIM2->CR1 |= (uint8_t)TIM2_CR1_ARPE;
    }
    else
    {
        TIM2->CR1 &= (uint8_t)(~TIM2_CR1_ARPE);
    }
}


/**
  * @brief  Enables or disables the TIM2 peripheral Preload Register on CCR1.
  * @param   NewState new state of the Capture Compare Preload register.
  * This parameter can be ENABLE or DISABLE.
  * @retval None
  */
void TIM2_OC1PreloadConfig(FunctionalState NewState)
{
    /* Check the parameters */
   // assert_param(IS_FUNCTIONALSTATE_OK(NewState));

    /* Set or Reset the OC1PE Bit */
    if (NewState != DISABLE)
    {
        TIM2->CCMR1 |= (uint8_t)TIM2_CCMR_OCxPE;
    }
    else
    {
        TIM2->CCMR1 &= (uint8_t)(~TIM2_CCMR_OCxPE);
    }
}


/**
  * @brief  Enables or disables the TIM2 peripheral Preload Register on CCR2.
  * @param   NewState new state of the Capture Compare Preload register.
  * This parameter can be ENABLE or DISABLE.
  * @retval None
  */
void TIM2_OC2PreloadConfig(FunctionalState NewState)
{
    /* Check the parameters */
 //   assert_param(IS_FUNCTIONALSTATE_OK(NewState));

    /* Set or Reset the OC2PE Bit */
    if (NewState != DISABLE)
    {
        TIM2->CCMR2 |= (uint8_t)TIM2_CCMR_OCxPE;
    }
    else
    {
        TIM2->CCMR2 &= (uint8_t)(~TIM2_CCMR_OCxPE);
    }
}


/**
  * @brief  Enables or disables the TIM2 peripheral Preload Register on CCR3.
  * @param   NewState new state of the Capture Compare Preload register.
  * This parameter can be ENABLE or DISABLE.
  * @retval None
  */
void TIM2_OC3PreloadConfig(FunctionalState NewState)
{
    /* Check the parameters */
  //  assert_param(IS_FUNCTIONALSTATE_OK(NewState));

    /* Set or Reset the OC3PE Bit */
    if (NewState != DISABLE)
    {
        TIM2->CCMR3 |= (uint8_t)TIM2_CCMR_OCxPE;
    }
    else
    {
        TIM2->CCMR3 &= (uint8_t)(~TIM2_CCMR_OCxPE);
    }
}



/**********************************************************************
  function TIM1_Config
  input   :  -
  output  :  -
  purpose : Configure TIM2 peripheral in PWM mode
  call    : Init
***********************************************************************/
void TIM2_Config(void)
{
   // Time base configuration
  TIM2_TimeBaseInit(TIM2_PRESCALER_4, MAX_PWM);

  // PWM1 Mode configuration: Channel1
  TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR21_Val, TIM2_OCPOLARITY_HIGH);
  TIM2_OC1PreloadConfig(ENABLE);

  // PWM1 Mode configuration: Channel2
  TIM2_OC2Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR22_Val, TIM2_OCPOLARITY_HIGH);
  TIM2_OC2PreloadConfig(ENABLE);

  // PWM1 Mode configuration: Channel2
  TIM2_OC3Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR23_Val, TIM2_OCPOLARITY_HIGH);
  TIM2_OC3PreloadConfig(ENABLE);

  TIM2_ARRPreloadConfig(ENABLE);

  // TIM2 enable counter
  TIM2_Cmd(ENABLE);
    
}

Can somebody help. Thank you.
Franz

Outcomes