Page 1 of 1

running pwm on stm32 with arduino IDE

Posted: Wed Aug 05, 2020 11:27 am
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...

Re: running pwm on stm32 with arduino IDE

Posted: Thu Aug 06, 2020 2:07 pm
by stevestrong
You have to pause the timer before writing the new period value.
And your code does not seem to change the frequency.