I've been practicing bare-metal programming with a custom board that I bought from a friend.
My idea is to control a LED brightness, connected to the pin PA4, over pwm. After flashing the device (STM32F042F4), the LED doesn't change its brightness, it remains turned off.
I used the following configurations:
- General purpose timer 14 in PWM mode 1 and clocked by HSI source (8MHz);
- PA4 selected as alternate function 4 (TIM14 Channel 1) and clocked by HSI source (8MHz) as well;
The code is shown below. I used the example provided in the reference manual as a guide.
#include "stm32f042x6.h"
void ConfigurePA4asAF2()
{
// enabling clock of GPIOA
RCC->AHBENR |= RCC_AHBENR_GPIOAEN_Msk;
GPIOA->MODER |= GPIO_MODER_MODER4_1; // alternate function
GPIOA->OTYPER &= ~GPIO_OTYPER_OT_4; // push-pull
GPIOA->OSPEEDR |= GPIO_OSPEEDR_OSPEEDR4_1; // low speed
GPIOA->PUPDR |= GPIO_PUPDR_PUPDR4_1; // pull-down
GPIOA->AFR[0] |= GPIO_AFRL_AFSEL4; //Alternate function 4, TIM14_CH1
}
void ConfigureTIM14()
{
//Enabling TIM14 clock
RCC->APB1ENR |= RCC_APB1ENR_TIM14EN;
TIM14->PSC = 7; // Pre scaler (PSC + 1), PSC final value = 8, CC module clock = 1MHz
TIM14->ARR = 1000-1; // auto reload value, period of PWM
TIM14->CCR1 = 500-1; // capture compare register, DutyCycle = 50%
TIM14->CCMR1 |= TIM_CCMR1_OC1M_2 + TIM_CCMR1_OC1M_1 + TIM_CCMR1_OC1PE; // (PWM mode 1)
TIM14->CCER |= TIM_CCER_CC1E; //capture compare enable
TIM14->BDTR |= TIM_BDTR_MOE;
TIM14->EGR |= TIM_EGR_UG;
TIM14->CR1 |= TIM_CR1_CEN; //Enabling timer
}
int main(void)
{
ConfigurePA4asAF2();
ConfigureTIM14();
while(1);
return 0;
}
In the example provided by the documentation (A.9.8 Edge-aligned PWM configuration example), the bit TIM_BDTR_MOE is set on register BDTR, and the description of this line says: "(6) Enable output (MOE = 1)".
However, Timer 14 doesn't have the register BDTR, does it mean that it's impossible to output pwm signal over PA4?
PS: My board doesn't support debugging feature.
|
instead of+
for using multiple flags at the same time. \$\endgroup\$