diff --git a/firmware/cup.c b/firmware/cup.c index d24fe62..a7ed9ef 100644 --- a/firmware/cup.c +++ b/firmware/cup.c @@ -12,6 +12,28 @@ uint8_t led_color = 0; +uint8_t setting_timeout = 0; + +uint8_t adc_pos = 0; +uint16_t adc_sum = 0; +uint16_t temp = 0; +uint16_t target = 0; + +uint8_t heat_pwm = 0; +uint8_t heat_on[2] = {0, 0}; +uint8_t heat_off[2] = {0, 0}; + +ISR(timer1 overflow) +{ + if(setting_timeout) {setting_timeout--;} + heat_pwm_cnt++; + + if(heat_pwm == heat_on[0]) {PORTB |= (1 << FET0);} + if(heat_pwm == heat_off[0]) {PORTB &= ~(1 << FET0);} + if(heat_pwm == heat_on[1]) {PORTB |= (1 << FET1);} + if(heat_pwm == heat_off[1]) {PORTB &= ~(1 << FET1);} +} + void io_init(void) { PORTA = (1 << TAST_PIN) | (1 << IMPULS1_PIN) | (1 << IMPULS0_PIN);/* enable pullups on inputs */ @@ -24,13 +46,12 @@ void led_init(void) { TCCR0A = (1 << COM0A1) | (1 << COM0B1) | (1 << COM0B0) | (1 << WGM1) | (1 << WGM0);/* pwm enable: fast, A not inverted, B inverted */ TCCR0B = (1 << CS02) | (1 << CS00);/* prescaler / 1024 */ - TCNT0 = 0;/* reset counter */ OCR0A = OCR0B = led_color; } -void led_set(void) +void led_set(uint16_t value) { - OCR0A = OCR0B = led_color; + OCR0A = OCR0B = led_color = value >> 8; } void adc_init(void) @@ -42,6 +63,13 @@ void adc_init(void) /* result in (ADCH << 8) | ADCL, accessable as ADC? */ } +void heat_init(void) +{ + TCCRB1 = (1 << CS12) | (1 << CS10);/* prescaler / 1024 */ + TIMSK1 = (1 << TOIE1);/* interupt on overflow */ + /* insert loading of heat setting here */ +} + void input_init(void) { GIMSK = (1 << PCIE0);/* interrupt on change on PORT A */ @@ -50,13 +78,30 @@ void input_init(void) int main(void) { - /* if we store the last setting somewhere get calculate led_color from it before calling led_init() */ + /* if we store the last setting somewhere calculate led_color from it before calling led_init() */ io_init(); - led_init(); adc_init(); + heat_init(); input_init(); + for(;;) + { + if(ADCSRA & (1 << ADIF)) + { + /* new adc result */ + ADCSRA |= (1 << ADIF); + adc_sum[adc_pos++] += ADC; + if(!adc_pos) + { + temp = adc_sum; + } + } + + if(setting_timeout) {led_set(target);} + else {led_set(temp);} + } + return 0; }