Rudimentary firmware infrastructure.
[heater.git] / firmware / main.c
1 #include <avr/io.h>
2 #include <avr/eeprom.h>
3 #include <util/delay.h>
4
5 static uint16_t adcval EEMEM;
6
7 int main()
8 {
9         DDRB |= _BV(PB2) | _BV(PB4);
10         TCCR1 = _BV(CS10); // clk/1 = 1 MHz
11         // TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz
12         GTCCR = _BV(COM1B1) | _BV(PWM1B);
13         OCR1C = 255;
14         OCR1B = 9;
15
16         ADCSRA = _BV(ADEN) | _BV(ADPS1) | _BV(ADPS0); // clk/8 = 125 kHz
17         ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0); // 1.1V ref., PB3 single-ended
18         DIDR0 = _BV(ADC3D);
19         ADCSRA |= _BV(ADSC);
20         while (!(ADCSRA & _BV(ADIF)))
21                 ;
22         ADCSRA |= _BV(ADSC);
23         while (!(ADCSRA & _BV(ADIF)))
24                 ;
25         eeprom_write_word(&adcval, ADCW);
26
27         while(1) {
28                 PORTB |= _BV(PB2);
29                 _delay_ms(500);
30                 PORTB &= ~_BV(PB2);
31                 _delay_ms(1000);
32         }
33 }