cancel
Showing results for 
Search instead for 
Did you mean: 

PWM not work by using TIM10_CH1

blacksu6
Associate II
Posted on September 04, 2013 at 08:22

My platform's MCU is STM32f205.

I try to modify TIM10_PWMOutput example from STM32F2xx Standard Peripherals Firmware Library to generate one PWM signal by using TIM10_CH1function of pin PB8. But it doesn't work? Can somebody help me to fix this issue? please see the reference codes as below.

int main(void)

{

  /* TIM Configuration */

  TIM_Config();

 

  /* ---------------------------------------------------------------------------

    TIM10 Configuration: generate 1 PWM signal:

    

    In this example TIM10 input clock (TIM10CLK) is set to 2 * APB2 clock (PCLK2),

    since APB2 prescaler is different from 1.   

      TIM10CLK = 2 * PCLK2  

      PCLK2 = HCLK / 2

      => TIM10CLK = HCLK = SystemCoreClock

          

    To get TIM10 counter clock at 20 MHz, the prescaler is computed as follows:

       Prescaler = (TIM10CLK / TIM10 counter clock) - 1

       Prescaler = (SystemCoreClock /20 MHz) - 1

                                              

    To get TIM10 output clock at 30 KHz, the period (TIM10_ARR) is computed as follows:

       ARR = (TIM10 counter clock / TIM10 output clock) - 1

           = 665

                  

    TIM10 Channel1 duty cycle = (TIM10_CCR1/ TIM10_ARR)* 100 = 50%

    Note:

     SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f2xx.c file.

     Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()

     function to update SystemCoreClock variable value. Otherwise, any configuration

     based on this variable will be incorrect.    

  --------------------------------------------------------------------------- */   

  /* Compute the prescaler value */

  PrescalerValue = (uint16_t) (SystemCoreClock / 20000000) - 1;

  /* Time base configuration */

  TIM_TimeBaseStructure.TIM_Period = 665;

  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM10, &TIM_TimeBaseStructure);

  /* PWM1 Mode configuration: Channel1 */

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC1Init(TIM10, &TIM_OCInitStructure);

  TIM_OC1PreloadConfig(TIM10, TIM_OCPreload_Enable);

  TIM_ARRPreloadConfig(TIM10, ENABLE);

  /* TIM10 enable counter */

  TIM_Cmd(TIM10, ENABLE);

  while (1)

  {}

}

/**

  * @brief  Configure the TIM10 Ouput Channels.

  * @param  None

  * @retval None

  */

void TIM_Config(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

 

  /* TIM10 clock enable */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10, ENABLE);

  /* GPIOF clock enable */

  //RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

 

  /* GPIOF Configuration: TIM10 CH1 (PF6) */

  //GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 ;

    /* GPIOF Configuration: TIM10 CH1 (PB8) */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;

  //GPIO_Init(GPIOF, &GPIO_InitStructure);

    GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* Connect TIM pins to AF3 */

  //GPIO_PinAFConfig(GPIOF, GPIO_PinSource6, GPIO_AF_TIM10);

    GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_TIM10);

}

#pwm-stm32-tim10
1 REPLY 1
Posted on September 04, 2013 at 16:39

The following worked for me on an F4, should be directly portable to an F2

// STM32 PWM (TIM10 30KHz PB8) STM32F4 Discovery - sourcer32@gmail.com
#include ''stm32f4_discovery.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* TIM10 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10, ENABLE);
/* GPIOB clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Connect TIM10 pins to AF */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_TIM10);
}
/**************************************************************************************/
void TIM10_Configuration(void)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
uint16_t Period;
Period = SystemCoreClock / 30000; // Accurate 30 KHz from 120 or 168 MHz
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_Period = Period - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM10, &TIM_TimeBaseStructure);
/* Enable TIM10 Preload register on ARR */
TIM_ARRPreloadConfig(TIM10, ENABLE);
/* TIM PWM1 Mode configuration */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = Period / 2; // 50%
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
/* Output Compare PWM1 Mode configuration: Channel1 PB.8 */
TIM_OC1Init(TIM10, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM10, TIM_OCPreload_Enable);
/* TIM10 enable counter */
TIM_Cmd(TIM10, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TIM10_Configuration();
while(1); // Don't want to exit
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..