#include #include #include #define TEMP_PIN 0 #define IMPULS0_PIN 1 #define IMPULS1_PIN 2 #define TAST_PIN 3 #define FET0_PIN 0 #define FET1_PIN 1 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 */ PORTB = 0; DDRA = (1 << OC0B);/* led outputs ... */ DDRB = (1 << OC0A) | (1 << FET0_PIN) | (1 << FET1_PIN);/* ... and FET output */ } 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 */ OCR0A = OCR0B = led_color; } void led_set(uint16_t value) { OCR0A = OCR0B = led_color = value >> 8; } void adc_init(void) { ADMUX = (1 << REFS1) | (TEMP_PIN << MUX0);/* Vref = 1.1, pin selection */ ADCSRA = (1 << ADEN) | (1 << ADATE);/* adc enable, triggered */ ADCSRB = (1 << ADTS2);/* trigger on counter 0 overflow */ DIDR0 = (1 << TEMP_PIN);/* disable digital input on adc pin */ /* 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 */ PCMSK0 = (1 << TAST_PIN) | (1 << IMPULS1_PIN) | (1 << IMPULS0_PIN); } int main(void) { /* 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; }