2021-10-19 04:38 AM
#define CR1_CEN (1U<<0)
/* *************** Enabling PA6, PA7 as OUTPUT PINs *************** */
/* Enabling clock access AHB1EN for GPIOC */
RCC->AHB1ENR |= (1U<<2);
/* *************** Setup TIMER8 for PWM control *************** */
/* Enable clock access to tim8 */
RCC->APB2ENR |= (1U<<1);
/* Set PC6 as AF3 for TIM3_CH1 */
GPIOC->MODER |= (1U << 13);
GPIOC->MODER &=~ (1U << 12);
/* Choose AF3(0b0011) for PC6 (AFR[1], it's used for pins from 8 to 15) */
GPIOC->AFR[0] |= (0x3 << 24);
/* *************** TIM3 settings for PWM *************** */
/* Set prescaler value */
TIM8->PSC = 16000 - 1;
/* Set auto-reload value */
TIM8->ARR = 100 - 1;
/* Set CCR1 as a preload value, it's used to determine the DUTY CYCLE */
TIM8->CCR1 = 90;
/* Set CCMR1 for output compare PWM mode 1 */
TIM8->CCMR1 |= (1U << 6) | (1U << 5);
/* *************** Compare MODE / interrupt status *************** */
/* Enable use of preload register */
TIM8->CCMR1 |= (1U<<3); // CCMR1_OC1PE_EN
/* Enable CH1 as Output Compare */
TIM8->CCER |= (1U<<0); // CC1EEN
/* Enable TIM interrupt */
//TIM8->DIER |= (1U<<0);
/* Enable TIM interrupt in NVIC */
//NVIC_EnableIRQ(TIM8_IRQn);
/* Enable timer*/
TIM8->CR1 |= CR1_CEN;
Unfortunately I can't see any change in the voltage level at the output or any change in the frequency level acording to multimeter.
I can't turn on an external LED as well no matter what values i set as DC or PSC etc.
What is wrong with my code?
To note that I tried with TIM1 initially at PA8 and now i switched to TIM8 but nothing changed.
I want to say as well that when I was using TIM1 I could have an high enough voltage output by setting everything for the TIM1 to work but AFR[0] = 0b0001 << 28; And it was odd because I got confused by that thinking that maybe the AFR started from PA1 for some reason. I could light the LED with those settings (but not as much as I'd like)
Since this pwm will be used for a H-Bridge, it's important that it works at higher voltages (at least 3V.. as a pwm)
Thank you!!
Solved! Go to Solution.
2021-10-19 06:18 AM
TIM8 is an advanced timer (as is TIM1) and has an MOE (main output enable) bit to globally enable PWMs. You'll need to set it.
SET_BIT(TIMx->BDTR, TIM_BDTR_MOE);
2021-10-19 06:18 AM
TIM8 is an advanced timer (as is TIM1) and has an MOE (main output enable) bit to globally enable PWMs. You'll need to set it.
SET_BIT(TIMx->BDTR, TIM_BDTR_MOE);
2021-10-19 06:38 AM
That's what I was thinking as well but I didn't try. As soon as I can I'll let you know
2021-10-19 08:12 AM
It worked, thanks a lot!