question related to PWM_FullConfiguration

Post here all questions related to STM32 core if you can't find a relevant section!
Post Reply
jenspogo
Posts: 30
Joined: Mon Feb 24, 2020 10:42 am

question related to PWM_FullConfiguration

Post 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
by stas2z » Thu May 07, 2020 4:47 pm
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);
Go to full post
stas2z
Posts: 131
Joined: Mon Feb 24, 2020 8:17 pm
Answers: 8

Re: question related to PWM_FullConfiguration

Post 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);
jenspogo
Posts: 30
Joined: Mon Feb 24, 2020 10:42 am

Re: question related to PWM_FullConfiguration

Post 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
stas2z
Posts: 131
Joined: Mon Feb 24, 2020 8:17 pm
Answers: 8

Re: question related to PWM_FullConfiguration

Post 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
Post Reply

Return to “General discussion”