2 PWM output

Post here all questions related to STM32 core if you can't find a relevant section!
Post Reply
madhavan
Posts: 29
Joined: Fri May 21, 2021 1:22 am

2 PWM output

Post by madhavan »

hi , I am little afraid !!! :| how to ask !!!.

I am trying to get 2 PWM output . the following code is working fine but how to get second PWM on PB1 pin .

how modify following line for 2 Pwm or Timer or Counter .

HardwareTimer *MyTim = new HardwareTimer(Instance);

How to change the *MyTim & instance incase of 2nd Timer or PWM or Counter has to work

Code: Select all

  

#define pin PA0


void setup()
{
  // no need to configure pin, it will be done by HardwareTimer configuration
  // pinMode(pin, OUTPUT);

  // Automatically retrieve TIM instance and channel associated to pin
  // This is used to be compatible with all STM32 series automatically.
  TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(pin), PinMap_PWM);
  uint32_t channel = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(pin), PinMap_PWM));


  // Instantiate HardwareTimer object. Thanks to 'new' instantiation, HardwareTimer is not destructed when setup() function is finished.
  HardwareTimer *MyTim = new HardwareTimer(Instance);

  // Configure and start PWM
  // MyTim->setPWM(channel, pin, 5, 10, NULL, NULL); // No callback required, we can simplify the function call
  MyTim->setPWM(channel, pin, 5, 10); // 5 Hertz, 10% dutycycle
  
  
}


void loop()
{
  /* Nothing to do all is done by hardware. Even no interrupt required. */
}
khoih-prog
Posts: 102
Joined: Thu Feb 27, 2020 7:54 am
Location: Toronto

Re: 2 PWM output

Post by khoih-prog »

You can try something similar to this code, just change the pins and/or Timer (if select manually) depending on the board

Code: Select all

uint32_t pins[]       = { PA0, PB1 };

#define NUM_OF_PINS   ( sizeof(pins) / sizeof(uint32_t) )

// 10% and 20%
uint32_t dutyCycle[NUM_OF_PINS] = { 10, 20 };

// 5 and 10 Hz
uint32_t freq[NUM_OF_PINS] = { 5, 10 };

// Select manually Timer to used
TIM_TypeDef *TimerInstance[NUM_OF_PINS] = { TIM1, TIM2 };

void setup()
{
  for (uint8_t index = 0; index < NUM_OF_PINS; index++)
  {
    // Select manually Timer to used
    // TIM_TypeDef *Instance = TimerInstance[index];
    // or
    // Automatically retrieve TIM instance and channel associated to pin
    // This is used to be compatible with all STM32 series automatically.
    TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(pins[index]), PinMap_PWM);

    uint32_t channel = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(pins[index]), PinMap_PWM));

    Serial.print("Index = "); Serial.print(index);
    Serial.print(", Instance = 0x"); Serial.print( (uint32_t) Instance, HEX);
    Serial.print(", channel = "); Serial.print(channel);
    Serial.print(", TimerIndex = "); Serial.println(get_timer_index(Instance));

    HardwareTimer *MyTim = new HardwareTimer(Instance);

    MyTim->setPWM(channel, pins[index], freq[index], dutyCycle[index]);
  }
}

void loop()
{
  /* Nothing to do all is done by hardware. Even no interrupt required. */
}
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: 2 PWM output

Post by GonzoG »

The easiest way is to use analogWrite().

Code: Select all

 analogWrite(PA1,1000);
 analogWrite(PA2,2000);

You can change frequency with

Code: Select all

analogWriteFrequency(herz);
and resolution with

Code: Select all

analogWriteResolution(bits);
Both commands need to be before analogWrite()
zitronic
Posts: 1
Joined: Fri May 12, 2023 9:20 am

Re: 2 PWM output

Post by zitronic »

Hi,
how can I set two (or more) different pwm-frequencys for two pwm-pins on STM32G071?
I prefer analogWriteFrequency(herz), but I assume is only for all pwms - correct?
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: 2 PWM output

Post by GonzoG »

zitronic wrote: Fri May 12, 2023 9:58 am Hi,
how can I set two (or more) different pwm-frequencys for two pwm-pins on STM32G071?
I prefer analogWriteFrequency(herz), but I assume is only for all pwms - correct?
analogWriteFrequency() is the easiest way. But it does not change frequency until analogWrite() i called and frequency changes only for pins that share a timer with pin used in analogWrite().
So if you want to use PWM with 2 frequencies you need to choose pins that are on 2 different timers and call analogWriteFrequency() before analogWrite().
Post Reply

Return to “General discussion”