简体   繁体   English

如何在没有 CubeMX 的情况下在 STM32F3 上实现 PWM?

[英]How to realise on a STM32F3 a PWM without CubeMX?

first of all, thx to everyone who read this an trys to help.首先,感谢所有阅读本文的人,并尝试提供帮助。

I got a little Project.我有一个小项目。 A 3x3x3 LED Cupe, build with the STM32F303.使用 STM32F303 构建的 3x3x3 LED 杯。 I try to get the TIM2 to work with three channels , but .. nothing happens.我尝试让 TIM2 与三个通道一起工作,但是 .. 没有任何反应。

Here's the Code:这是代码:

#include "stm32f3xx.h" 
#include "stm32f3xx_nucleo.h"

int main(void){ 
RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;

GPIOA->MODER |= 0b10;           //PA0
GPIOA->MODER |= 0b10 << 2;      //PA1
GPIOA->MODER |= 0b10 << 4;      //PA2

GPIOA->AFR[0] |= 0b0001;
GPIOA->AFR[0] |= 0b0001 << 4;
GPIOA->AFR[0] |= 0b0001 << 8;

TIM2->CCMR1 = (0b0110 << 4) | (0b0110 << 12);
TIM2->CCMR2 = 0b0110 << 4;

TIM2->CCER = TIM_CCER_CC1E;
TIM2->CCER = TIM_CCER_CC2E;
TIM2->CCER = TIM_CCER_CC3E;


TIM2->PSC = 7999;
TIM2->ARR = 999;

TIM2->CCR1 = 99;
TIM2->CCR2 = 399;
TIM2->CCR3 = 699;

TIM2->CR1 = TIM_CR1_CEN;

while (1)
{
    ;
}

Any ideas?有任何想法吗?

Seems you miss something.好像你错过了什么。

  1. as Bence mentioned: use |= for CCER正如本斯提到的:使用 |= 用于 CCER
  2. same on CR1 although here it is not necessary CR1 上也一样,虽然这里没有必要
  3. set PE bits on each enabled channel in CCMR (preload enable)在 CCMR 中的每个启用通道上设置 PE 位(预加载启用)
  4. at the end set UE bit in EGR register (update generation)最后在 EGR 寄存器中设置 UE 位(更新生成)

Hope thats all.希望这就是全部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM