#include "lights.h"
-#define PWM_MAX 0x0FF
+#define PWM_MAX 0x1FF
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;
}
}
-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;
}
}
#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 */
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)
{
}
if (mode <= N_PWMLED_MODES) {
- unsigned char pwmval;
+ uint16_t pwmval;
mode--;
pwmval = pwm_vals[n*N_PWMLED_MODES+mode];
pwm_set(n, pwmval);
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);
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);
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);
{
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;
}
// 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;
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);
}