/* Control a fan installed in the roof depending on roof & room temperature * If the temp above is higher than the one below and the room is not hotter than 22°C * the fan activates. * a simple dimmer control is included * 1mhz CLK * Temperatures are measured with 2 identical KTY Sensors * connected to ADC 2 and 3 */ #include #include #include #include #include #include // Ports #define ROOM_PIN PB4 #define ROOF_PIN PB3 #define FAN_OUT PB1 // which is OC0B and OC1A - low active #define TEST_PIN PB0 // very simple serial output #define MAINS_PIN PB2 // gets sine wave from mains to synchronize the dimmer // ADC // if differential mode is chosen and roof is hotter than room, give a positive output #define ROOF_ADC 2 // 0010 #define ROOM_ADC 3 // 0011 #define DIFF_ADC 6 // 0110 mux setting #define ROOMTEMPMAX 522 // globals volatile uint8_t roomtemp,rooftemp,difftemp; volatile uint8_t pwm,buf = 0; register volatile uint8_t ticker asm("r9"); register volatile uint8_t cnt asm("r8"); /* Once again the multi channel ADC Readout and Restart Routine. * it examines the MUX setting, stores the corresponding value * and then switches to the next channel * The ISR ends with restarting the ADC * a little bit of averaging is done,too. * note that this routine will never fire if you miss to start the ADC * once after initializing with the ADIE flag set. */ ISR(ADC_vect) { uint8_t mux = ADMUX; switch (mux & 0x07) { case ROOM_ADC : roomtemp = (roomtemp + ADCH) >> 1; // averaging ADMUX = (mux & 0xF0) | ROOF_ADC; break; case ROOF_ADC : rooftemp = (rooftemp + ADCH) >> 1; // averaging ADMUX = (mux & 0xF0) | ROOM_ADC; break; // case DIFF_ADC : difftemp = (difftemp + ADC) >> 1; // ADMUX = (mux & 0xF0) | ROOM_ADC; // break; default : ADMUX = (mux & 0xF0) | ROOM_ADC; break; } ADCSRA = (1< 0) return; // transmission in progress buf = data; cnt=9; // 9 bits to send - this includes the start bit } /* Init routines */ // initiate the ADC to start a single conversion with IRQ enabled void initADC(void) { DIDR0 = (1<