From 6b1ec9a54e9428a5c2db26e628a8aed7c93e7c3b Mon Sep 17 00:00:00 2001 From: Stacey Sheldon Date: Sat, 30 Jul 2011 16:59:57 -0400 Subject: [PATCH] bl: fix LED pwm computation with new stopwatch --- flight/Bootloaders/CopterControl/main.c | 12 +++++++----- flight/Bootloaders/PipXtreme/main.c | 12 +++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/flight/Bootloaders/CopterControl/main.c b/flight/Bootloaders/CopterControl/main.c index d8d06424e..aa375a49c 100644 --- a/flight/Bootloaders/CopterControl/main.c +++ b/flight/Bootloaders/CopterControl/main.c @@ -187,11 +187,13 @@ void jump_to_app() { } } uint32_t LedPWM(uint32_t pwm_period, uint32_t pwm_sweep_steps, uint32_t count) { - uint32_t pwm_duty = ((count / pwm_period) % pwm_sweep_steps) - / (pwm_sweep_steps / pwm_period); - if ((count % (2 * pwm_period * pwm_sweep_steps)) > pwm_period - * pwm_sweep_steps) - pwm_duty = pwm_period - pwm_duty; // negative direction each 50*100 ticks + uint32_t curr_step = (count / pwm_period) % pwm_sweep_steps; /* 0 - pwm_sweep_steps */ + uint32_t pwm_duty = pwm_period * curr_step / pwm_sweep_steps; /* fraction of pwm_period */ + + uint32_t curr_sweep = (count / (pwm_period * pwm_sweep_steps)); /* ticks once per full sweep */ + if (curr_sweep & 1) { + pwm_duty = pwm_period - pwm_duty; /* reverse direction in odd sweeps */ + } return ((count % pwm_period) > pwm_duty) ? 1 : 0; } diff --git a/flight/Bootloaders/PipXtreme/main.c b/flight/Bootloaders/PipXtreme/main.c index 0337c51a5..3f1c4bd54 100644 --- a/flight/Bootloaders/PipXtreme/main.c +++ b/flight/Bootloaders/PipXtreme/main.c @@ -193,11 +193,13 @@ void jump_to_app() { } } uint32_t LedPWM(uint32_t pwm_period, uint32_t pwm_sweep_steps, uint32_t count) { - uint32_t pwm_duty = ((count / pwm_period) % pwm_sweep_steps) - / (pwm_sweep_steps / pwm_period); - if ((count % (2 * pwm_period * pwm_sweep_steps)) > pwm_period - * pwm_sweep_steps) - pwm_duty = pwm_period - pwm_duty; // negative direction each 50*100 ticks + uint32_t curr_step = (count / pwm_period) % pwm_sweep_steps; /* 0 - pwm_sweep_steps */ + uint32_t pwm_duty = pwm_period * curr_step / pwm_sweep_steps; /* fraction of pwm_period */ + + uint32_t curr_sweep = (count / (pwm_period * pwm_sweep_steps)); /* ticks once per full sweep */ + if (curr_sweep & 1) { + pwm_duty = pwm_period - pwm_duty; /* reverse direction in odd sweeps */ + } return ((count % pwm_period) > pwm_duty) ? 1 : 0; }