简体   繁体   中英

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. I try to get the TIM2 to work with three channels , but .. nothing happens.

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
  2. same on CR1 although here it is not necessary
  3. set PE bits on each enabled channel in CCMR (preload enable)
  4. at the end set UE bit in EGR register (update generation)

Hope thats all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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