Skip to main content
MArdm.1
Associate II
January 17, 2023
Solved

STM32F401xB/C output a PWM signal on pin B14

  • January 17, 2023
  • 1 reply
  • 782 views

So, the issue is as follows, according to datasheet the timer for pin B14 AF1, which is the PWM signal I believe, is the TIM1_CH2N. I have gone out of my way to try and output something, I just can't seem to understand what the problem is

#define AF01 0x01
 
void ms_delay(int ms) {
 while (ms-- > 0) {
 volatile int x = 500;
 while(x-- > 0)
 __asm("nop");
 }
}
 
void setup() {
 RCC->AHB1ENR |= 1<<1; //Enable clock for GPIOB
 RCC->APB2ENR |= 1<<0; //Enable clock for TIM1
 
 GPIOB->MODER |= 1<<29; //Set pin B14 to alternate function
 GPIOB->MODER &= ~(1<<28);
 
 GPIOB->MODER |= 1<<0;
 GPIOB->OTYPER &= ~(1<<0);
 
 GPIOB->AFR[1] |= AF01<<24; //Set the alternate function register for pin 14 to 0001
 
 TIM1->PSC=0;
 TIM1->ARR=255;
 TIM1->CNT=0;
 
 TIM1->CCMR1 |= (1<<13)|(1<<14); //Set channel 2 to pwm mode 1
 TIM1->CCER |= 1<<4; //Enable channel 2
 TIM1->CCER |= 1<<6; //Enable channel 2N ??
 TIM1->CR1 |= 1<<0; // Counter enable for TIM1
 TIM1->BDTR |= 1<<15; 
 
 TIM1->CCR2 = 5;
 GPIOB->ODR |= 1;
}
 
void loop() {
 ms_delay(3000);
}

I am coding by putting value directly into registers sorry if this is less than readable, I can't use HAL or any other library, any help is much appreciated

This topic has been closed for replies.
Best answer by MArdm.1

Well, problem, found, in case anyone is interested, this code has no issues, it will activate the TIM1 Channel 2N, problem was external (:

1 reply

MArdm.1
MArdm.1AuthorBest answer
Associate II
January 18, 2023

Well, problem, found, in case anyone is interested, this code has no issues, it will activate the TIM1 Channel 2N, problem was external (: