2024-02-06 05:11 AM
Solved! Go to Solution.
2024-02-06 05:46 AM
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
2024-02-06 05:29 AM
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
2024-02-06 05:46 AM
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
2024-02-06 06:11 AM
@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