2 #include <avr/eeprom.h>
3 #include <util/delay.h>
5 #include <avr/interrupt.h>
7 volatile uint16_t adcval;
8 unsigned char pwmval = 0x28;
19 static void inline led_on()
24 static void inline led_off()
29 /* ------------ Logging/Debugging ----------- */
31 unsigned char debug_state EEMEM;
33 static void inline debug_setstate(unsigned char val)
35 eeprom_write_byte(&debug_state, val);
38 #define LOG_BUFFER 192
39 uint16_t log_buffer[LOG_BUFFER] EEMEM;
40 uint16_t log_buffer_count;
42 static void inline init_log()
48 static void log_word(uint16_t word) {
49 if (log_buffer_count == LOG_BUFFER) {
53 if (log_buffer_count >= LOG_BUFFER)
56 eeprom_write_word(&log_buffer[log_buffer_count], word);
60 /* ------------ Timer ----------- */
64 static void inline init_tmr()
67 TCCR0B = _BV(CS02) | _BV(CS00); // 1 kHz
75 static void inline tmr_handler()
77 unsigned char c = clock & 0x7F;
80 if (c == 10 || c == 30)
83 if (c == 20 || c == 40)
87 if (adcval != 0xFFEE) {
90 ADCSRA |= _BV(ADIE) | _BV(ADSC);
94 ISR(TIMER0_COMPA_vect)
99 /* ------------ PWM ----------- */
101 static void inline init_pwm()
104 PLLCSR = _BV(LSM) | _BV(PLLE);
106 while (PLLCSR & _BV(PLOCK) == 0)
110 TCCR1C = _BV(COM1D0) | _BV(COM1D1) | _BV(PWM1D);
111 TCCR1A = _BV(COM1A0) | _BV(COM1A1) | _BV(COM1B0) | _BV(COM1B1) | _BV(PWM1A) | _BV(PWM1B);
112 // TCCR1B = 0x80| _BV(CS13) | _BV(CS11);
113 TCCR1B = 0x80 | _BV(CS10);
115 OCR1D = OCR1B = OCR1A = pwmval;
118 PORTB &= ~(_BV( PB5 ) | _BV( PB1 ) | _BV( PB3 ));
121 // TIMSK |= _BV(TOIE1);
125 static void inline pwm_handler()
133 TIMSK &= ~_BV(TOIE1);
138 /* ------------ A/D Converter ----------- */
140 static void inline init_adc()
142 ADCSRA = _BV(ADEN) | _BV(ADATE) | _BV(ADPS1) | _BV(ADPS0);
143 ADMUX = _BV(REFS1) | _BV(MUX0);
144 // ADCSRB = _BV(REFS2);
145 DIDR0 = _BV(ADC1D) | _BV(AREFD);
148 static void inline adc_handler()
150 static unsigned char last_pwmval = 0;
151 static unsigned char counter = 0;
152 uint16_t tmp = pwmval;
153 static uint16_t discard = 5;
156 ADCSRA &= ~_BV(ADIE); /* disable ADC interrupt */
163 else if (adcval > 0x280)
165 else if (adcval > 0x201)
167 else if (adcval < 0x100)
169 else if (adcval < 0x180)
171 else if (adcval < 0x1ff)
180 if ((last_pwmval > tmp && last_pwmval - tmp > 10)
181 || (last_pwmval < tmp && tmp - last_pwmval > 10)
185 if (rv_count < BUFFER-1) {
186 eeprom_write_word(&readval[rv_count++], adcval);
187 eeprom_write_word(&readval[rv_count++], pwmval);
188 if (rv_count >= BUFFER)
189 eeprom_write_byte(&debug, 42);
193 last_pwmval = pwmval;
196 if (pwmval != last_pwmval) {
205 //TIMSK |= _BV(TOIE1);
206 ADCSRA |= _BV(ADIE) | _BV(ADSC);
218 pcount = pattern[ppos].length;
221 pcount = pattern[ppos].length;
223 if (pattern[ppos].intensity) {
235 unsigned char adcidx = 0;
236 uint16_t adcvals[16];
252 eeprom_write_byte(&debug, 2);
260 PORTA &=~ _BV( PA0 );