running pwm on stm32 with arduino IDE

Post here all questions related to LibMaple core if you can't find a relevant section!
Post Reply
amin5659
Posts: 1
Joined: Wed Aug 05, 2020 11:14 am

running pwm on stm32 with arduino IDE

Post by amin5659 »

hi all. im new with programming stm32 with arduino.
i want to run pwm function on PA2 pin with variable frequency and 1% duty cycle.
i write the code bellow but the problem is i just can change the frequency in void setup. but when i try to change the timer2.setPeriod value in void loop{} pwm frequency doesn't change.
where is the problem?
this is my code

Code: Select all

#define pin  PA2

void setup() {
    pinMode(PA2 , PWM);
    Timer2.pause();
    Timer2.setPeriod(10); // in microseconds
    Timer2.setMode(TIMER_CH1,TIMER_OUTPUTCOMPARE);
    Timer2.setCompare(TIMER_CH1, 1); 
    Timer2.attachInterrupt(TIMER_CH1,handler_led);
    Timer2.refresh();
    Timer2.resume();
    Timer4.setPrescaleFactor(prescaler);

}
void loop() {
  }
void handler_led(void) {
pwmWrite(PA2,167);   
}
any help would be appreciated...

stevestrong
Posts: 315
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 7
Location: Munich, Germany
Contact:

Re: running pwm on stm32 with arduino IDE

Post by stevestrong »

You have to pause the timer before writing the new period value.
And your code does not seem to change the frequency.

Post Reply

Return to “General discussion”