0
\$\begingroup\$

I use STM32F030F4 in TSSOP-20 package and the timer doesn't work. I try to find an example for STM32F103 and it work very good.

Here is the code sample that I use:

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);

GPIO_InitTypeDef GPIO_Config;
GPIO_Config.GPIO_Pin = GPIO_Pin_6;

GPIO_Config.GPIO_Mode = GPIO_Mode_AF;
GPIO_Config.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Config.GPIO_OType = GPIO_OType_PP;
GPIO_Config.GPIO_PuPd = GPIO_PuPd_NOPULL;

TIM_TimeBaseInitTypeDef timer;
TIM_OCInitTypeDef timerPWM;

TIM_TimeBaseStructInit(&timer);
timer.TIM_Prescaler = 0;
timer.TIM_Period = 100;
timer.TIM_ClockDivision = 0;
timer.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &timer);

TIM_OCStructInit(&timerPWM);
timerPWM.TIM_OCMode = TIM_OCMode_PWM1;
timerPWM.TIM_OutputState = TIM_OutputState_Enable;
timerPWM.TIM_Pulse = 50;
timerPWM.TIM_OCPolarity = TIM_OCPolarity_High;

TIM_OC1Init(TIM3, &timerPWM);
TIM_Cmd(TIM3, ENABLE);

TIM3->CCR1 = 50;
\$\endgroup\$
  • \$\begingroup\$ Your STM32F0 is substantially different from an STM32F1, especially with regard to how GPIO alternate functions are configured. Your TSSOP-20 would be one of the lowest end variants of the chip, so it's important to check which peripherals may be deleted - however you are in luck, timer 3 is there, it is timers 6, 7, and 15 which are not. \$\endgroup\$ – Chris Stratton Dec 4 '18 at 22:51
2
\$\begingroup\$

Here is the Timer 3 initialization code as generated by the STM32CubeMX:

/* TIM3 init function */
void MX_TIM3_Init(void)
{
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};

  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 5;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 400;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 0;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  {
    Error_Handler();
  }
  HAL_TIM_MspPostInit(&htim3);

}

And within the HAL_TIM_MspPostInit(&htim3) here is the related code to the pin initialization:

__HAL_RCC_GPIOA_CLK_ENABLE();
/**TIM3 GPIO Configuration    
PA6     ------> TIM3_CH1
PA7     ------> TIM3_CH2 
*/
GPIO_InitStruct.Pin = PWM_ISO_Pin|PWM_LED_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM3;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

I noticed that you're missing this statement: 'GPIO_InitStruct.Alternate = GPIO_AF1_TIM3;' which means that you set the pin as Alternate_Function but forget to set which AF it should be mapped to. Hope that helps.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thank you very much for the detail post. I havn't used HAL library yet, so it's time to try :) \$\endgroup\$ – user3771501 Dec 5 '18 at 21:07
  • \$\begingroup\$ And one more about GPIO structure: typedef struct { uint32_t GPIO_Pin; GPIOMode_TypeDef GPIO_Mode; GPIOSpeed_TypeDef GPIO_Speed; GPIOOType_TypeDef GPIO_OType; GPIOPuPd_TypeDef GPIO_PuPd; }GPIO_InitTypeDef; It doesn't has Alternate_Function field, but all other field I initialized. \$\endgroup\$ – user3771501 Dec 5 '18 at 21:13
  • \$\begingroup\$ I tried this code, but it completely doesn't work. I don't know, maybe the MC is not work correctly and I should replace it. But write 1 to pin_0 is work perfect. Anybody has some ideas? \$\endgroup\$ – user3771501 Dec 5 '18 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.