From b1355d3cdba26fe18c5a69fc6d94c93478559788 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Fri, 8 Jun 2012 19:17:30 +0200 Subject: [PATCH] ADC-driven PWM --- lights.c | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/lights.c b/lights.c index 49bf4ce..653e8d4 100644 --- a/lights.c +++ b/lights.c @@ -1,29 +1,55 @@ #include #include #include +#include +#include -uint16_t adcval EEMEM; +uint16_t pwmee EEMEM; int main(void) { + char seen = 0; + TCCR1C = _BV(COM1D0) | _BV(COM1D1) | _BV(PWM1D); TCCR1A = _BV(PWM1A) | _BV(PWM1B); - TCCR1B = 0x80 | _BV(CS13) | _BV(CS12); + TCCR1B = 0x80| _BV(CS13) | _BV(CS11); + TC1H = 0x03; OCR1C = 0xFF; OCR1D = 0x20; - TCNT1 = 3; + TCNT1 = 0; DDRB |= _BV( PB5 ); PORTB &= ~_BV( PB5 ); ADCSRA = _BV(ADEN) | _BV(ADATE) | _BV(ADPS1) | _BV(ADPS0); ADMUX = _BV(REFS1) | _BV(MUX0); - ADCSRB = _BV(REFS2); + // ADCSRB = _BV(REFS2); DIDR0 = _BV(ADC1D) | _BV(AREFD); - ADCSRA |= _BV(ADSC); - _delay_ms(1000); - eeprom_write_word(&adcval, ADCW); + while (1) { + uint16_t pwm; + unsigned char pwmhi, pwmlo; + + ADCSRA |= _BV(ADSC); + + _delay_ms(100); + + pwm = ADCW; + if (!seen) { + seen = 1; + eeprom_write_word(&pwmee, pwm); + } + if (pwm > 0x3C0) + pwm = 0x3C0; + if (pwm < 1) + pwm = 1; + pwmhi = pwm >> 8; + pwmlo = pwm & 0xFF; + + TC1H = pwmhi; + OCR1D = pwmlo; + } + sleep_mode(); DDRA |= _BV( PA0 ); while( 1 ) { PORTA |= _BV( PA0 ); -- 2.43.5