#include <avr/io.h>
#include <avr/eeprom.h>
#include <util/delay.h>
+#include <avr/sleep.h>
+#include <avr/interrupt.h>
-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 );