Page 1 of 1

question related to PWM_FullConfiguration

Posted: Thu May 07, 2020 3:55 pm
by jenspogo
Hey :)
I'm working with the example https://github.com/stm32duino/STM32Exam ... ration.ino and it works quite well on my BluePill board!
question: Is it possible to update frequency and DutyCycle in the loop Function? because of the following line in the setup functions, i thought, tat it should be.

Code: Select all

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

Code: Select all

MyTim->setPWM(channel, pin, 1000, 50);
but the error log says:

Code: Select all

error: 'MyTim' was not declared in this scope
   50 |   MyTim->setPWM(channel, pin, 150000, 50);
      |   ^~~~~
exit status 1
'MyTim' was not declared in this scope

Does somebody know what am I doing or understanding wrong?

greetings, Jens

Re: question related to PWM_FullConfiguration

Posted: Thu May 07, 2020 4:47 pm
by stas2z
Make MyTim global, put it before setup

Code: Select all

HardwareTimer *MyTim;
And remove HardwareTimer * before MyTim in setup()

Code: Select all

MyTim = new HardwareTimer(Instance);

Re: question related to PWM_FullConfiguration

Posted: Fri May 08, 2020 6:26 am
by jenspogo
Thanks! it works fine!
I was confused because of the "new" in Hardwaretimer *MyTim = new HardwareTimer(Instance);
I think I lack the deeper understanding of programming...
if anyone would like to explain it, I would be grateful - but it is not necesseray to mark this post as solved.

greets, Jens

Re: question related to PWM_FullConfiguration

Posted: Fri May 08, 2020 6:49 am
by stas2z
jenspogo wrote: Fri May 08, 2020 6:26 am Thanks! it works fine!
I was confused because of the "new" in Hardwaretimer *MyTim = new HardwareTimer(Instance);
I think I lack the deeper understanding of programming...
if anyone would like to explain it, I would be grateful - but it is not necesseray to mark this post as solved.

greets, Jens
Oh, this is how pointers and dynamic memory allocation work
you can't define HardwareTimer object statically there as constructor parameter is not constant, but depends on pin, so it allocated dynamically, at runtime