I'm new here and I have some questions about generating a PWM signal for dimming an led using STM32 boards. I'm trying to generate the PWM only by writing the nessasarry bits with bit manipulation. But the LED does not light. I don't know if I'm missing something for a correct PWM generation, because I'm new to this microcontroller.
I'm using a NUCLEO-L432KC with a click-shield (PA8 is connected to a green led)
- The
GPIOAclock is enabled - The PA8 pin is set to
AFR[1]to access the correct register (register for pin 8 to 17) - I'm using
TIM1because it's the only timer available for PA8.
The following code is inside the main function.
// Enable GPIOA and TIM1 clock
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
HAL_Delay(1);
// Configure PA8 for alternate function (AF1: TIM1_CH1)
GPIOA->MODER &= ~GPIO_MODER_MODE8; // Clear bits
GPIOA->MODER |= GPIO_MODER_MODE8_1; // Set PA8 to AF mode
GPIOA->AFR[1] |= (0x1 << ((8 - 8) * 4)); // Set AF1 for PA8
// Set timer properties
TIM1->PSC = 0; // Prescaler
TIM1->ARR = 4999; // Auto-reload (5kHz)
TIM1->CCR1 = 2499; // 50% duty cycle
TIM1->CCMR1 &= ~TIM_CCMR1_OC1M; // Clear output compare mode
TIM1->CCMR1 |= TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2; // Set PWM mode 1
TIM1->CCER |= TIM_CCER_CC1E; // Enable output
// Generate update event and start the timer
TIM1->EGR |= TIM_EGR_UG;
TIM1->CR1 |= TIM_CR1_CEN; // Enable Timer
Am I missing something?
~0x3 << (8*2)has a different value to~(0x3 << (8*2)), which is probably what you intend.