cancel
Showing results for 
Search instead for 
Did you mean: 

issue PWM output with STM32F215 microcontroller.

I have noted in my program following behaviour.

Fist time microcontroller is programmed with entire FLASH erased before, Program runs right.

If I disconnect device or it is reprogrammed, PWM does not work.

PWM will work again if previously, all FLASH is erased completely.

The program modifies some region in FLASH, but it is in part far away from microcontroller registers.

I use TIMER3 CHANNEL4 over PC9 pin.

Someone has watched a similar behaviour to this?

-------------------------------------------------------------------------------------------------------------------------

Basic functions I use are following:

void initializePWM(void)

{

MX_TIM3_Init();

}

* TIM3 init function */

static void MX_TIM3_Init(void)

{

 TIM_MasterConfigTypeDef sMasterConfig;

 htim3.Instance = TIM3;

 htim3.Init.Prescaler = 0;

 htim3.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim3.Init.Period = PERIOD_PWM;

 htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

 htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

 if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)

 {

  _Error_Handler(__FILE__, __LINE__);

 }

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

 if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)

 {

  _Error_Handler(__FILE__, __LINE__);

 }

 sConfig.OCMode = TIM_OCMODE_PWM1;

 sConfig.Pulse = 0;

 sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;

 sConfig.OCFastMode = TIM_OCFAST_DISABLE;

 //if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfig, TIM_CHANNEL_3) != HAL_OK)

 if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfig, TIM_CHANNEL_4) != HAL_OK)

 {

  _Error_Handler(__FILE__, __LINE__);

 }

 HAL_TIM_MspPostInit(&htim3);

}

void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)

{

 GPIO_InitTypeDef GPIO_InitStruct;

 if(htim->Instance==TIM3)

 {

 /* USER CODE BEGIN TIM3_MspPostInit 0 */

 /* USER CODE END TIM3_MspPostInit 0 */

  /**TIM3 GPIO Configuration

  PC8   ------> TIM3_CH3

  */

  GPIO_InitStruct.Pin = GPIO_PIN_9;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

  GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;

  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

 /* USER CODE BEGIN TIM3_MspPostInit 1 */

 /* USER CODE END TIM3_MspPostInit 1 */

 }

}

int dimming(int regulation)

{

/* Set the pulse value for channel 2 */

float temp=0.0;

if (regulation>100) return -1;

temp= (PERIOD_PWM/100.0)*(100-(regulation*0.9)); 

sConfig.Pulse = (uint32_t)temp;

 if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfig, TIM_CHANNEL_4) != HAL_OK)

 {

  /* Configuration Error */

  Error_Handler();

  return -1;

 }

 /* Start channel 4 */

 if (HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4) != HAL_OK)

 {

  /* PWM Generation Error */

  Error_Handler();

  return -1;

 }

 return 1;

}

1 REPLY 1

Likely something else.

Clear local/automatic variables properly.

Enable all clocks used.

Dump related register content until you see differences​.

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