From: Jan "Yenya" Kasprzak Date: Fri, 14 Sep 2012 20:51:12 +0000 (+0200) Subject: pwm: 16-bit pwm and adc values X-Git-Tag: gerber-fab-20121029-1711~37 X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=38a3c5783c24a17e6cf1fbf0af40ae1dec805419;p=bike-lights.git pwm: 16-bit pwm and adc values --- diff --git a/firmware/lights.h b/firmware/lights.h index bce5215..c0daaef 100644 --- a/firmware/lights.h +++ b/firmware/lights.h @@ -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; diff --git a/firmware/pwm.c b/firmware/pwm.c index 4eb2f83..10c03ba 100644 --- a/firmware/pwm.c +++ b/firmware/pwm.c @@ -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; } } diff --git a/firmware/pwmled.c b/firmware/pwmled.c index 7985467..06c6b11 100644 --- a/firmware/pwmled.c +++ b/firmware/pwmled.c @@ -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); }