Skip to main content
Graduate
December 29, 2024
Question

STM32F030T8C6 PWM Not Setting new PWM Value.

  • December 29, 2024
  • 1 reply
  • 475 views

Hi.

I have the following code. However when I try and update the arb value with anything it just does not reflect on the pin.
Currently it is running a stepper motor. The stepper motor moves one step every time the pulse goes high.
With that in mind I am suspecting that the rotation speed should dramatically change when I do increments of 1000 on the "change_pwm_frequency()" function. BUT it does not even change at all.

On my stm32f411 I can set ONLY the PSC value without setting the EGR and I can hear the motor go slower or faster simply in the vibration but on this specific MCU it just ignores my changes to the TIMER3 values.
I have tried with the current state and ALSO have commented out the EGR but still 

 

 

void setup_pwm() {
 // Enable clocks
 RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // Enable clock for GPIOB
 RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; // Enable clock for TIM3

 // Configure PB1 as alternate function (AF1)
 GPIOB->MODER &= ~(GPIO_MODER_MODER1); // Clear mode bits for PB1
 GPIOB->MODER |= GPIO_MODER_MODER1_1; // Set PB1 to alternate function mode
 GPIOB->AFR[0] &= ~(GPIO_AFRL_AFRL1); // Clear alternate function for PB1
 GPIOB->AFR[0] |= (1 << GPIO_AFRL_AFRL1_Pos); // Set AF1 (TIM3_CH4)

 // Configure TIM3 for PWM
 //TIM3->PSC = 1600; // Default prescaler (no prescaling initially)
 TIM3->PSC = 5000-1; // Default prescaler (no prescaling initially)
 TIM3->ARR = 50; // Default auto-reload value (1 kHz frequency by default)
 TIM3->CCR4 = 25; // Default duty cycle (50%)


 // Configure PWM mode for TIM3 channel 4
 TIM3->CCMR2 &= ~(TIM_CCMR2_OC4M); // Clear output compare mode bits
 TIM3->CCMR2 |= (6 << TIM_CCMR2_OC4M_Pos); // Set PWM mode 1 (OC4M = 110)
 TIM3->CCMR2 |= TIM_CCMR2_OC4PE; // Enable output compare preload
 TIM3->CCER |= TIM_CCER_CC4E; // Enable capture/compare output for channel 4

 TIM3->CR1 |= TIM_CR1_ARPE; // Enable auto-reload preload
 TIM3->CR1 |= TIM_CR1_CEN; // Enable the timer
}

uint8_t change_pwm_frequency(uint32_t arb) {
	if (arb >= 1000 && arb <= 20000) {
		TIM3->PSC = arb; // Default prescaler (no prescaling initially)
		TIM3->EGR |= TIM_EGR_UG; // Generate an update event to apply changes
		return 1;
	}
	return 0;
}

 

 

 

    This topic has been closed for replies.

    1 reply

    Super User
    December 29, 2024

    Try to toggle a GPIO output pin in change_pwm_frequency() and observe using oscilloscope/logic analyzer.

    JW