cancel
Showing results for 
Search instead for 
Did you mean: 

How to start PWM using the registers.

DK.7
Senior

What am I doing wrong?

Why is PWM don't work on the STM32F767ZI?

 

RCC->APB2ENR |= 1; // Enable clock for TIM1 on APB2.
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOEEN; // Enable clock for GPIOE port on AHB1.

GPIOE->MODER &= ~(1 << 18); // Clear/reset PE9 mode.
GPIOE->MODER |= (2 << 18); // Set PE9 mode.

// Enable alternative function AFIO. Connect TIM to the pin accordingly!
GPIOE->AFR[1] &= ~(0xF << 4); // Clear bits 4 to 7.
GPIOE->AFR[1] |= (1 << 4); // Set bit 9.

TIM1->PSC = 216 - 1; // Set prescaler for > 10KHz.
TIM1->ARR = 100 - 1; // Set auto-reload value for 1Hz.
TIM1->CCR1 = 50; // Set initial comparison value (PWM duty cycle).
TIM1->CNT = 0; // Reset the current count.

TIM1->CCMR1 |= (0x6UL << TIM_CCMR1_OC1M_Pos ); // Set PWM mode 1.
TIM1->CCMR1 |= (0x68 << 0);

TIM1->CCER = 1; // Enable PWM Channel 1.
TIM1->CR1 = 1; // Enable timer.

 

 

Q1.pngQ2.pngQ3.png

13 REPLIES 13
LCE
Principal

When I compare my code to yours:

- TIM: you have not set the ARPE bit in CR1, not sure though if that is really needed, check RM

RM:

You must enable the corresponding preload register by setting the
OCxPE bit in the TIMx_CCMRx register, and eventually the auto-reload preload register (in
upcounting or center-aligned modes) by setting the ARPE bit in the TIMx_CR1 register.

IDK if that applies to your stuff.

- clock enables and GPIO:  that's where I am lazy and use the HAL stuff. Maybe just for checking, you use HAL stuff for that, then see if your PWM starts doing something.

Thank you, I will verify everything and I'll reply to you!

MHank.1
Associate III

I wrote an article on ARM Timers using registers, NOT HAL, that might be of some help.

 

https://jaxcoder.com/Post/Index?guid=456f6a89-1a93-4a72-bab9-4cf3a9e7f9b6 

 

 

Read out and check/post TIM and GPIO registers' content. That's always the first step in debugging software on mcu, as it works out of registers, not out of the source code.

JW