/* Realtime controller with c source code Copyright (C) 2006-2007 Wichit Sirichote, kswichit@kmitl.ac.th */ #include #include char sec10,sec,min,hour; char i; char command; /* / air.pgm / activates four devices by time setting / '1' is on and '0' is off / OUTPUT [1...8] / output 1 is a SSR controlled 12,000btu air conditioner / output 2 is a 7W night lamp at bed / output 3 is a 10W lamp at bedroom's door / output 4 is a 20W fluorescent at kitchen / output 5 reserved / output 6 high current 12Vdc solenoid driver / output 7 reserved / output 8 reserved /line day time output 1...8 :01 24 1900 0 0 1 1 0 0 0 0 / on lamp at bedroom door and kitchen :02 24 2000 0 1 1 1 1 0 0 0 / also bed lamp, and fan :03 24 2200 1 0 1 1 1 0 0 0 / off bed lamp, on air conditioner :04 24 2230 1 0 0 0 1 0 0 0 / off door lamp :05 24 2310 1 0 0 0 0 0 0 0 / off fan :06 24 0530 0 0 0 0 1 0 0 0 / off air-conditioner and on fan again :07 24 0600 0 0 0 0 0 0 0 0 / off all :00 */ char pgm[]={ 19,0, 0x30, 20,0,0x78, 22,0,0xb8,22,30,0x88,23,10,0x80,5,30,0x08,6,0,0x00}; print_time() { printf("\r%02bd:%02bd",hour,min); } void scan_pgm() { for(i=0; i<7; i++) { if(hour == pgm[i*3] && min == pgm[(i*3)+1]) P1 = ~pgm[(i*3)+2]; } } get_command() { if(RI) { RI = 0; command = SBUF; } else command = -1; } adjust_hour() { if(command == 'h') { if(++hour >=24) hour =0; print_time(); } } adjust_min() { if(command == 'm') { sec =0; if(++min>59) min =0; print_time(); } } time() { if(++sec10==10) { sec10 =0; P3 ^= 0x80; print_time(); // print time every second if ( ++sec >= 60) { sec = 0; scan_pgm(); // scan program every minute if ( ++min >= 60) { min = 0; if ( ++hour >= 24) hour = 0; } } } } void timer0int (void) interrupt 1 using 1 { TH0 = 0x8b; // reload timer 0 TL0 = 0x92; time(); // update realtime clock get_command(); adjust_hour(); adjust_min(); } void external0int (void) interrupt 0 { P1 &= ~0x80; // turn P1.7 on manually } void main() { EA = 1; ET0 = 1; // or IE |= 0x82; /* set bit EA and Timer0 enable */ EX0 = 1; // enable external interrupt 0 SCON = 0x52; // 8-bit UART mode TMOD = 0x21; // timer 1 mode 2 auto reload TH1= 0xfe; // 9600 8n1 with 3.579545MHz XTAL PCON = 0x80; //smod = 1 TR1 = 1; TR0 = 1; // run timer0 and timer1 hour = 8; // when reset set current time to 8:00 min = 0; sec = 0; while(1) continue; }