cancel
Showing results for 
Search instead for 
Did you mean: 

stm32g071rb problem with PWM generation using TIM1 and PA8 pin

dmrsim
Associate III
Hello,
 
I'm studing timers with stm32g071rb and I wanna generate a PWM signal using PA8 pin.
At the moment I write this code:

GPIOA->MODER &= 0xeffcffff;
GPIOA->MODER |= 2<<16; //alternate function mode for PA8
GPIOA->OSPEEDR |= 1<<17; //PA8 at high speed
GPIOA->AFR[1] |= 2; //alternate function for PA8(TIM1_CH1)
 
rcc->APBENR2 |= RCC_APBENR2_TIM1EN_Msk;
rcc->IOPENR |= RCC_IOPENR_GPIOAEN_Msk;
timer->ARR = 20;
timer->PSC = 16000 - 1;
timer->CCR1 = 1;
timer->CCMR1 |=  0x60; //PWM mode 1
timer->CCMR1 |= TIM_CCMR1_OC1PE_Msk;
timer->CR1 |= TIM_CR1_ARPE_Msk;
timer->EGR = TIM_EGR_UG_Msk;
timer->CCER |= 1 << 0;
timer->CR1 |= TIM_CR1_CEN_Msk;
 
I connected an oscilloscope in PA8 pin but there is no signal...
Please could you help me?

Thanks a lot!
1 ACCEPTED SOLUTION

Accepted Solutions

You need to enable GPIOA clock *before* you write to GPIOA registers.

Also, TIM1 is an Advanced timer and you need to set TIM1_BDTR.MOE to get it output PWM.

What are "rcc" and "timer"?

Always start debugging by reading out and checking the relevant registers content, e.g. in debugger.

JW

View solution in original post

3 REPLIES 3
dmrsim
Associate III
GPIOA->MODER &= 0xeffcffff;
GPIOA->MODER |= 2<<16; //alternate function mode for PA8
GPIOA->OSPEEDR |= 1<<17; //PA8 at high speed
GPIOA->AFR[1] |= 2; //alternate function for PA8(TIM1_CH1)
 
rcc->APBENR2 |= RCC_APBENR2_TIM1EN_Msk;
rcc->IOPENR |= RCC_IOPENR_GPIOAEN_Msk;
timer->ARR = 20;
timer->PSC = 16000 - 1;
timer->CCR1 = 1;
timer->CCMR1 |=  0x60; //PWM mode 1
timer->CCMR1 |= TIM_CCMR1_OC1PE_Msk;
timer->CR1 |= TIM_CR1_ARPE_Msk;
timer->EGR = TIM_EGR_UG_Msk;
timer->CCER |= 1 << 0;
timer->CR1 |= TIM_CR1_CEN_Msk;

I repost the code using tag

You need to enable GPIOA clock *before* you write to GPIOA registers.

Also, TIM1 is an Advanced timer and you need to set TIM1_BDTR.MOE to get it output PWM.

What are "rcc" and "timer"?

Always start debugging by reading out and checking the relevant registers content, e.g. in debugger.

JW

@waclawek.jan Thanks a lot, you solved me a problem that keep me stuck for 2 days!!
For your information, rcc and timer are:

RCC_TypeDef *rcc = RCC;
TIM_TypeDef *timer = TIM1;

 I missing these lines in the documentation:

 OCx output is enabled by a combination oft the CCxE, CCxNE, MOE, OSSI and OSSR bits (TIMx_CCER and TIMx_BDTR registers)

So after I put this line as you as you said, I solved my problem:

timer->BDTR |= TIM_BDTR_MOE_Msk;

 

Thanks again @waclawek.jan