my guess is you can tryGVisser wrote: Sat Jun 13, 2020 5:23 pm Hi @ag123 ,
Thank you for your reply and yes I will look further into the setPWM function.
As for the rest, the PWM is working properly and the reading of the decoder is working properly, the issue is that I cannot find a way to alter the PWM signal with values from the main loop. Once setup is done, the PWM signal runs regardless and I am way too new to programming to know how to fix it
My aim is to be able to starts and stop the PWM output from within the main loop and, use the encoders (or potentiometers) to set and vary the PWM frequency and duty cycle (or preferably, the actual +pulse time).![]()
something like this
Code: Select all
void setPeriod(uint32 microseconds) {
int CYCLES_PER_MICROSECOND = 72; //i.e. stm32f103 72mhz
int MAX_RELOAD = 65535;
uint32 period_cyc = microseconds * CYCLES_PER_MICROSECOND;
uint16 prescaler = (uint16)(period_cyc / MAX_RELOAD + 1);
uint16 overflow = (uint16)((period_cyc + (prescaler / 2)) / prescaler);
MyTim->setPrescaleFactor(prescaler);
MyTim->setOverflow(overflow);
}
void loop() {
...
microsconds = 100;
setPeriod(microseconds);
}
review how
Code: Select all
void setPWM(uint32_t channel, PinName pin, uint32_t frequency, ...
and duty cycle needs to be separately set in the capture compare registers, cross reference the manuals the hardware timers for more info
the duty cycle is the fraction of that overflow i.e. capture_compare = duty_cycle x overflow / 100
assuming that duty_cycle is a number between 0 .. 100