🍻 STM32 set_pwm_duty "on/off" for digital pins (#23665)

This commit is contained in:
Mike La Spina authored and Scott Lahteine committed 2022-05-07 15:48:13 -05:00
1 parent d4801461f5
commit a07d7e4b8a
3 files changed
+39 -31

No files matched your search

+12 -9
View File
@@ -39,16 +39,19 @@ inline uint8_t timer_and_index_for_pin(const pin_t pin, timer_dev **timer_ptr) {
}
void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
if (!PWM_PIN(pin)) return;
timer_dev *timer; UNUSED(timer);
if (timer_freq[timer_and_index_for_pin(pin, &timer)] == 0)
set_pwm_frequency(pin, PWM_FREQUENCY);
const uint8_t channel = PIN_MAP[pin].timer_channel;
const uint16_t duty = invert ? v_size - v : v;
timer_set_compare(timer, channel, duty);
timer_set_mode(timer, channel, TIMER_PWM); // PWM Output Mode
if (PWM_PIN(pin)) {
timer_dev *timer; UNUSED(timer);
if (timer_freq[timer_and_index_for_pin(pin, &timer)] == 0)
set_pwm_frequency(pin, PWM_FREQUENCY);
const uint8_t channel = PIN_MAP[pin].timer_channel;
timer_set_compare(timer, channel, duty);
timer_set_mode(timer, channel, TIMER_PWM); // PWM Output Mode
}
else {
pinMode(pin, OUTPUT);
digitalWrite(pin, duty < v_size / 2 ? LOW : HIGH);
}
}
void set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {