我正在使用 Nucleo 板来实现我的恶作剧,我的芯片上有这个 PA5 引脚,它连接到板上的 LED2,可以生成 PWM 信号,但它没有,我也不清楚解决它
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
GPIOA->MODER |= ( 0x2UL << GPIO_MODER_MODE5_Pos); //alternate mode
GPIOA->AFR[0]|= 0x000000010;//alternate function for port A 5
TIM2->PSC = 840 - 1; // Set prescaler
TIM2->ARR = 1000; // Set auto-reload
TIM2->CCMR1 |= ( 0x6UL << TIM_CCMR1_OC1M_Pos );
TIM2->CCMR1 |= TIM_CCMR1_OC1PE;
TIM2->CR1 |= TIM_CR1_ARPE;
TIM2->CCER &= ~TIM_CCER_CC1P;
TIM2->CCER |= TIM_CCER_CC1E;
TIM2->CR1 |= TIM_CR1_URS;
TIM2->EGR |= TIM_EGR_UG;
TIM2->DIER |= ( TIM_DIER_CC1IE | TIM_DIER_UIE );
TIM2->SR &= ~( TIM_SR_CC1IF | TIM_SR_UIF );
这是代码,希望有人能帮忙
我尝试交换替代功能的位,但它不起作用,也许我只是不知道如何摆弄它
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
GPIOA->MODER |= ( 0x2UL << GPIO_MODER_MODE5_Pos); //alternate mode
GPIOA->AFR[0]|= 0x000100000;//alternate function for port A 5
TIM2->PSC = 840 - 1; // Set prescaler
TIM2->ARR = 1000; // Set auto-reload
TIM2->CCMR1 |= ( 0x6UL << TIM_CCMR1_OC1M_Pos );
TIM2->CCMR1 |= TIM_CCMR1_OC1PE;
TIM2->CR1 |= TIM_CR1_ARPE|TIM_CR1_CEN|TIM_CR1_URS;
TIM2->CCER &= ~TIM_CCER_CC1P;
TIM2->CCER |= TIM_CCER_CC1E;
TIM2->EGR |= TIM_EGR_UG;
TIM2->DIER |= ( TIM_DIER_CC1IE | TIM_DIER_UIE );
TIM2->SR &= ~( TIM_SR_CC1IF | TIM_SR_UIF );
这是最终有效的代码,感谢 Ilya 和 pmacfarlane