Skip to main content
Graduate
October 11, 2024
Solved

PWM no signal

  • October 11, 2024
  • 5 replies
  • 3923 views

I've wrote a code without using Hal to produce a signal on the scope but i'm not getting any signal on my pin.

This is on a NUCLEO F401RE. Using TIMER3_CHANNEL1 and GPIOB for my Alternate function.

 

#include <stdint.h>
#include "stm32f4xx.h"
#include "stm32f401xe.h"
 
#define CMS_6 (1U<<6)
#define CMS_5 (1U<<5)
 
int main(void)
{
 
 
//Enable clock access to TIMER3/PWM
RCC->AHB1ENR |= (1U<<1); //GPIO Port B enable
RCC->APB1ENR |= (1U<<1); //Timer clock enable
 
 
//Set alternate function mode PB4(D5)
GPIOB->MODER |= (1U<<9);
GPIOB->MODER&=~ (1U<<8);
GPIOB->AFR[0]|= (1U<<18);
 
//Enable Timer
TIM3->CR1 |= (1U<<0)|(1U<<7); // Counter Enable , 
TIM3->CR1 &=~CMS_6 ;/*Edge aligned mode
TIM3->CR1 &=~CMS_5;                     */
TIM3->CR1 &=~ (1U<<4);// Direction Enable used as upconter 
 
//Setting PWM mode 1 (OC1M),(OC1PE)
TIM3->CCMR1 |=((1U<<6)|(1U<<5))| (1U<<3);
 
//Event Generation register
TIM3->EGR |= (1<<0);
 
//Capture Compare enable register/Enabled Channel 1
TIM3->CCER |= (1U<<1);
 
//PWM frequency  = Frequency clock/PSC/ARR 42Mhz/1000
//PWM Duty cycle = CCR1/ARR = 50% Duty cycle.
TIM3->PSC  =42;
TIM3->ARR  = 1000;
TIM3->CCR1 = 500;  //Duty cycle 50% , counting from 0 to a 1000;
for(;;);
 
 
}

 

    This topic has been closed for replies.
    Best answer by Tesla DeLorean

    Is the goal to write the most inefficient code, that's hard to maintain too?

    TIM3_CH1 is AF2 on PB4

    ARR and PSC are set as N-1 values

    5 replies

    Super User
    October 11, 2024

    >I've wrote a code without using Hal to produce a signal on the scope but i'm not getting any signal 

    So use Cube/HAL , to see, how its done. Even if you dont like it.

    Super User
    October 12, 2024

    Read out and check/post content of TIM and relevant GPIO registers.

    > GPIOB->AFR[0]|= (1U<<18);

    This sets AFR for PB4 to 0b0100 = 4, that's not what you want.

    JW

    MarrkinhoAuthor
    Graduate
    October 13, 2024

    This is a reference of the datasheet. The reset values are 0's hence, I am trying to SET pin 18 and leave the rest to 0 as highlighted. Not sure what  you meant by saying that's not what I want ? Any further explanation would be helpful . Thanks

     

     

    Marrkinho_0-1728816203310.png

     

     

    Graduate II
    October 13, 2024

    Is the goal to write the most inefficient code, that's hard to maintain too?

    TIM3_CH1 is AF2 on PB4

    ARR and PSC are set as N-1 values

    MarrkinhoAuthor
    Graduate
    October 18, 2024

    I've made changes and took on your advice , wasn't getting a signal still when I run it . I know when using CUBE/HAL you need to set the clock config tree, and as I'm writing manually I don't have anything set for it ,could that be the issue?

    Super User
    October 19, 2024

    You should have some output without configuring the clocks in RCC, too, as the whole mcu runs out of the default HSI RC oscillator at 16MHz.

    Read out and check/post content of TIM and relevant GPIO registers.

    JW

    Graduate
    October 19, 2024

    How about:

    TIM3->CCER = TIM_CCER_CC1E;

    Use bit names instead of magic numbers!

     

    Also, timer clock frequency is 84 MHz (2 x ABP1 frequency).

    Super User
    October 19, 2024

    Nice catch!

    JW