Skip to main content
Associate II
October 11, 2024
Solved

PWM no signal

  • October 11, 2024
  • 5 replies
  • 3924 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(;;);
 
 
}

 

    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

    AScha.3
    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.

    "If you feel a post has answered your question, please click ""Accept as Solution""."
    waclawek.jan
    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
    Associate II
    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

     

     

    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    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

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    MarrkinhoAuthor
    Associate II
    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?

    waclawek.jan
    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

    gbm
    Lead III
    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).

    My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
    waclawek.jan
    Super User
    October 19, 2024

    Nice catch!

    JW