]> www.fi.muni.cz Git - bike-lights.git/commitdiff
pwm.c: publish only PWM_MAX as non-internal value
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 7 Dec 2012 22:03:55 +0000 (23:03 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 7 Dec 2012 22:03:55 +0000 (23:03 +0100)
The TOP value of Timer/Counter 1 is computed inside pwm.c and it is
purely internal to this module.

firmware/lights.h
firmware/pwm.c
firmware/pwmled.c

index 4a23436ea79dd84b8b21a055c70890cce5127fbe..fe8b59f10730c097394cec835b3dbab7206796d1 100644 (file)
@@ -27,11 +27,12 @@ void init_adc();
 void susp_adc();
 
 /* pwm.c */
-#define PWM_MAX 0x1E4 /* This should be different than ADC frequency 125 kHz */
-#define PWM_STEP_SHIFT 2 /* second parameter of pwm_set is shifted by
-                         * PWM_STEP_SHIFT bits to the right before setting
-                         * into HW */
-
+/*
+ * The real Timer/Counter 1 frequency should not be too close to the
+ * A/D converter frequency (125 kHz). Note that this is not the Top
+ * value of T/C 1, it is shifted by PWM_STEP_SHIFT as described in pwm.c
+ */
+#define PWM_MAX 0x780
 void init_pwm();
 void susp_pwm();
 void pwm_off(unsigned char n);
index 56fb0884e125113a6885a3deb45cba9efe5ad6c8..d68eeb2c3fdf370f69f32a8aa10b1afd68ff48a9 100644 (file)
@@ -5,6 +5,12 @@
 
 #include "lights.h"
 
+#define PWM_STEP_SHIFT 2 /* sub-LSB precision */
+#define PWM_TOP (((PWM_MAX) + (4 << (PWM_STEP_SHIFT))) >> (PWM_STEP_SHIFT))
+#if PWM_TOP > 0x1FF
+#error PWM_TOP too high
+#endif
+
 static uint16_t pwm[N_PWMLEDS];
 static volatile unsigned char step;
 
@@ -38,11 +44,11 @@ void init_pwm()
        TCCR1D = 0;
        TCCR1B = _BV(CS10);                     // no clock prescaling
 
-       TC1H = PWM_MAX >> 8;
-       OCR1C = PWM_MAX & 0xFF;                         // TOP value
+       TC1H = PWM_TOP >> 8;
+       OCR1C = PWM_TOP & 0xFF;                         // TOP value
 
-       TC1H = PWM_MAX >> 8;            // PWM3 is inverted
-       OCR1D = PWM_MAX & 0xFF;
+       TC1H = PWM_TOP >> 8;            // PWM3 is inverted
+       OCR1D = PWM_TOP & 0xFF;
 
        TC1H = 0x00;
        OCR1B = OCR1A = 0;              // initial stride is 0
@@ -85,7 +91,7 @@ static void pwm_update_hw(unsigned char n)
        uint16_t stride = (pwm[n] + step) >> PWM_STEP_SHIFT;
 
        if (n == 2)
-               stride = PWM_MAX - stride;
+               stride = PWM_TOP - stride;
 
        hi = stride >> 8;
        lo = stride & 0xFF;
@@ -108,8 +114,8 @@ static void pwm_update_hw(unsigned char n)
 
 void pwm_set(unsigned char n, uint16_t stride)
 {
-       if (((stride + (1 << PWM_STEP_SHIFT)) >> PWM_STEP_SHIFT) >= PWM_MAX)
-               stride = PWM_MAX << PWM_STEP_SHIFT;
+       if (stride > PWM_MAX)
+               stride = PWM_MAX;
 
        ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
                pwm[n] = stride;
index 076d032c7b4a713e4a9db98e79ddf6381ed835ab..d85087edcf81bc35d3765f419e8babfd24217fe2 100644 (file)
@@ -204,7 +204,7 @@ void pwmled_adc(unsigned char n, uint16_t adcval)
        sum -= led->pwm << shift;
        led->err_sum = sum;
 
-       if (led->pwm > (PWM_MAX << PWM_STEP_SHIFT)-5) {
+       if (led->pwm >= PWM_MAX) {
                pwmled_err(n);
                return;
        }