]> www.fi.muni.cz Git - bike-lights.git/commitdiff
pwm: 16-bit pwm and adc values
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 14 Sep 2012 20:51:12 +0000 (22:51 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 14 Sep 2012 20:51:12 +0000 (22:51 +0200)
firmware/lights.h
firmware/pwm.c
firmware/pwmled.c

index bce52154de1edc3298e2ea93478808470c7c678c..c0daaef6175e74c180dceeef2cc2f4e651d585bb 100644 (file)
@@ -29,7 +29,7 @@ void timer_start_adcs();
 /* pwm.c */
 void init_pwm();
 void pwm_off(unsigned char n);
-void pwm_set(unsigned char n, unsigned char stride);
+void pwm_set(unsigned char n, uint16_t stride);
 
 /* tmr.c */
 extern volatile uint16_t jiffies;
index 4eb2f83183a1ee411176b58815180afe12d786d0..10c03ba700677a9141ad165ef761357a64f0d3d1 100644 (file)
@@ -4,7 +4,7 @@
 
 #include "lights.h"
 
-#define PWM_MAX 0x0FF
+#define PWM_MAX 0x1FF
 
 void init_pwm()
 {
@@ -23,10 +23,11 @@ void init_pwm()
        TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(PWM1A) | _BV(PWM1B);
        TCCR1D = 0;
        TCCR1B = _BV(CS10);                     // no clock prescaling
+
        TC1H = PWM_MAX >> 8;
        OCR1C = PWM_MAX & 0xFF;                         // TOP value
 
-       TC1H = PWM_MAX >> 8;
+       TC1H = PWM_MAX >> 8;            // PWM3 is inverted
        OCR1D = PWM_MAX & 0xFF;
 
        TC1H = 0x00;
@@ -45,26 +46,34 @@ void pwm_off(unsigned char n)
        }
 }
 
-void pwm_set(unsigned char n, unsigned char stride)
+void pwm_set(unsigned char n, uint16_t stride)
 {
-       TC1H = 0x00;
+       unsigned char hi, lo;
+
+       if (stride > PWM_MAX)
+               stride = PWM_MAX;
+
+       if (n == 2)
+               stride = PWM_MAX - stride;
+
+       hi = stride >> 8;
+       lo = stride & 0xFF;
+
        switch (n) {
        case 0:
-               OCR1A = stride;
+               TC1H = hi;
+               OCR1A = lo;
                DDRB |= _BV(PB1);
                break;
-       case 1: OCR1B = stride;
+       case 1:
+               TC1H = hi;
+               OCR1B = lo;
                DDRB |= _BV(PB3);
                break;
-       case 2: {
-                       uint16_t s16 = PWM_MAX - (uint16_t)stride;
-                       volatile unsigned char hi, lo;
-                       hi = s16 >> 8;
-                       lo = s16 & 0xFF;
-                       TC1H = hi;
-                       OCR1D = lo;
-                       DDRB |= _BV(PB5);
-               }
+       case 2:
+               TC1H = hi;
+               OCR1D = lo;
+               DDRB |= _BV(PB5);
                break;
        }
 }
index 798546737d358f303889fd7490f97644f2ff91b1..06c6b1127a96a6ce4e42af3ed66f68206419e0d9 100644 (file)
@@ -2,8 +2,10 @@
 
 #include "lights.h"
 
-static unsigned char pwm_vals[N_PWMLEDS*N_PWMLED_MODES];
-static unsigned char adc_vals[N_PWMLEDS*N_PWMLED_MODES] = {
+static uint16_t pwm_vals[N_PWMLEDS*N_PWMLED_MODES];
+static uint16_t pwm_max[N_PWMLEDS] = { 0x70, 0x70, 0xF0 };
+static uint16_t adc_max[N_PWMLEDS] = { 0x70, 0x70, 0xF0 };
+static uint16_t adc_vals[N_PWMLEDS*N_PWMLED_MODES] = {
        /* pwmled0 */
        0x04, 0x14, 0x24, 0x38,
        /* pwmled1 */
@@ -22,7 +24,7 @@ static unsigned char pwmled_state[N_PWMLEDS];
 static unsigned char pwmled_mode[N_PWMLEDS];
 static unsigned char pwmled_mode_set[N_PWMLEDS];
 
-static unsigned char pwm_probes[N_PWMLEDS];
+static uint16_t pwm_probes[N_PWMLEDS];
 
 static void start_probing(unsigned char n)
 {
@@ -82,7 +84,7 @@ void pwmled_set_mode(unsigned char n, unsigned char mode)
        }
 
        if (mode <= N_PWMLED_MODES) {
-               unsigned char pwmval;
+               uint16_t pwmval;
                mode--;
                pwmval = pwm_vals[n*N_PWMLED_MODES+mode];
                pwm_set(n, pwmval);
@@ -98,9 +100,9 @@ void pwmled_set_mode(unsigned char n, unsigned char mode)
 static void inline probing_adc(unsigned char n, uint16_t adcval)
 {
        unsigned char need_bigger = 0, i;
-       unsigned char *pwm_p = &pwm_vals[n*N_PWMLED_MODES];
-       unsigned char *adc_p = &adc_vals[n*N_PWMLED_MODES];
-       unsigned char pwm = pwm_probes[n];
+       uint16_t *pwm_p = &pwm_vals[n*N_PWMLED_MODES];
+       uint16_t *adc_p = &adc_vals[n*N_PWMLED_MODES];
+       uint16_t pwm = pwm_probes[n];
 
 #if 0
        log_byte(0xF4);
@@ -108,7 +110,7 @@ static void inline probing_adc(unsigned char n, uint16_t adcval)
        log_word(adcval);
 #endif
 
-       if (adcval > 0x100 // Too high
+       if (adcval > adc_max[n] // Too high
                || (pwm == 0 && adcval > 0) // non-zero voltage with zero PWM
                ) {
                pwm_off(n);
@@ -146,7 +148,7 @@ static void inline probing_adc(unsigned char n, uint16_t adcval)
                return;
        }
 
-       if (pwm >= 0xE0) { // over the maximum!
+       if (pwm >= pwm_max[n]) { // over the maximum!
                pwm_off(n);
                pwmled_state[n] = ST_DISABLED;
                log_byte(0xF2);
@@ -166,7 +168,7 @@ static void inline on_adc(unsigned char n, uint16_t adcval)
 {
        unsigned char mode = pwmled_mode[n];
        uint16_t adc_exp     =  adc_vals[n*N_PWMLED_MODES+mode];
-       unsigned char *pwm_p = &pwm_vals[n*N_PWMLED_MODES+mode];
+       uint16_t *pwm_p      = &pwm_vals[n*N_PWMLED_MODES+mode];
        uint16_t old_pwm     = *pwm_p;
        uint16_t new_pwm = old_pwm;
 
@@ -191,8 +193,8 @@ static void inline on_adc(unsigned char n, uint16_t adcval)
        }
 
        // FIXME: better disconnect detection
-       if (new_pwm > 0xE0) { // disconnected?
-               new_pwm = 0xE0;
+       if (new_pwm > pwm_max[n]) { // FIXME: disconnected?
+               new_pwm = pwm_max[n];
        }
        if (new_pwm < 2) { // short-circuit?
                new_pwm = 2;
@@ -226,7 +228,7 @@ void pwmled_adc(unsigned char n, uint16_t adcval)
                                log_byte(pwmled_state[i]);
                                
                        for (i = 0; i < N_PWMLEDS*N_PWMLED_MODES; i++)
-                               log_byte(pwm_vals[i]);
+                               log_word(pwm_vals[i]);
                        log_flush();
                        log_set_state(4);
                }