#define ON 1 /*create some useful constants*/ #define OFF 0 /*...*/ #include "stdio.h" #include "conio.h" #include "time.h" #include "dos.h" /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- This program is a demonstration of the Digital Input/Output circuits. The program is a rendition of a security system, that allows you to monitor the status of 16 security sensors, and to automatically trigger four alarms that can be used to turn on the lights, activate a siren, or send a triggering pulse to a silent alarm. The alarm system in this demonstration has four arming stations, which toggle the alarm on or off. LAST MODIFIED ON: August 21, 1995 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/ int sensors_at_arm[15]; int sensors_now[15]; /*bit by bit status of sensors at current */ /*time. When compared against status of */ /*sensors at arm, indicates break-in if */ /*there is a change. */ int arming_stations; /*a variable representing all four arming */ int old_arming_stations; /*stations. If value changes, toggle alarm*/ /*ON / OFF. */ char key; /*useful temporary variable*/ int i; /*useful temporary variable used in loops*/ int j; /*useful temporary variable*/ unsigned AskForBaseAddress(unsigned int OldOne) { char msg[6]; int NewOne = 0, Success = 0, Dummy; int AddrInputPosX, AddrInputPosY; puts("\nPlease enter the Base Address (100-3F8) for your card (in hex)"); printf("or press ENTER for %X.\n>", OldOne); AddrInputPosX = wherex(); AddrInputPosY = wherey(); do { gotoxy(AddrInputPosX, AddrInputPosY); clreol(); msg[0] = 4; msg[1] = 0; cgets(msg); sscanf(msg + 2, "%x", &NewOne); if ((NewOne <= 0x3f8) && (NewOne >= 0x100)) { Success = 1; Dummy = NewOne; } else if (msg[1] == 0) { gotoxy(AddrInputPosX, AddrInputPosY); printf("%X", OldOne); Success = 1; Dummy = OldOne; } } while(!Success); return (Dummy); } /* end of AskForBaseAddress */ void initialize(unsigned int Address){ /*this procedure sets MODE 0 as active, */ outportb(Address+3,0x93);/*and sets Port A, B, and C Lo as as input, with*/ /*outportb(addr,byte) is */ /*Port C Hi being output. */ /*C's method of accessing*/ /*the bit pattern required to set the desired */ /*port memory. The above*/ /*mode and port designations is 10010011=93 hex */ /*code sets port memory */ /*Refer to the User Manual for a description of */ /*at 0x303 to 93 hex. */ /*what each bit does. */ /*This address is the */ /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/ /*IOD board's control */ /*register, assuming the base address is 300 hex.*/ } /*procedure initialize */ void read_sensors(int *ary,unsigned int Address){ unsigned char tempA; unsigned char tempB; tempA = inportb(Address); tempB = inportb(Address+1); for(i=0;i<8;i++){ if((tempA >> i) & ON){ /*this determinesif bit # i is ON */ *ary++=ON;} /*and sets the corresponding array */ else{ /*element to ON if it is ... else */ *ary++=OFF;} /*the array element is set to OFF. */ } for(i=0;i<8;i++){ if ((tempB >> i) & ON){ *ary++=ON; } else{ *ary++=OFF;} } } /*procedure read_sensors*/ get_status(unsigned int Address){ int temp; /*this sets status to the lower half */ temp=inportb(Address+2); /*(lower nybble) of Port C, the half*/ return temp & 0x0F; /*defined in Initialize to be input,*/ } /*function get_arming_status*/ /*for four (4) arming switches. */ void ALARM(unsigned int Address){ long int temp=0; sound(2000); /*this starts the computer's speaker */ outportb(Address+2,0xF0); /*this sets port C's upper nybble's */ do{ /*bits to ON (1111 binary = F hex). */ arming_stations=get_status(Address); /*This activates four alarm outputs, */ if (arming_stations != old_arming_stations) temp=2000000000;/*dis-armed*/ /*and then toggles port C Hi's LSB */ outportb(Address+2,inportb(Address+2) ^ 0x10); }while (temp++ < 200000000); /*which might be used with an external*/ nosound(); /*speaker to produce a siren*/ } /*procedure ALARM*/ /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/ main() { unsigned int Address; time_t start; initialize(Address); clrscr(); gotoxy(1,2); printf(" EVENT-C.C : IOD-24\n\n"); printf("This is the IOD demonstration program. This program will simulate\n"); printf("an alarm system program, containing sixteen sensors and four arming\n"); printf("stations, along with four separate alarm outputs, which could be routed\n"); printf("to a siren, lights, silent alarm, etc.\n"); printf("\n\n"); Address=AskForBaseAddress(0x354); clrscr(); puts("\nBoard Configuration:\n"); printf(" -- Base Address is %X hex\n",Address); puts(" -- Jumper IN5 should be installed (required)"); puts(" -- Jumper IP should be installed (required)"); puts(" -- All remaining jumper settings are irrelevant\n\n\n"); puts(" To arm the system, ground one of the arming stations (pins 9,11,13"); puts(" or 15), when prompted to arm. To trigger an alarm, ground any odd"); puts(" pin between 17 and 47, inclusive. To stop the alarm, either wait,"); puts(" or ground an arming station again. To exit without triggering an "); puts(" alarm, ground an arming station. Then press a key when the prompt"); puts(" appears. \n"); printf("THIS PROGRAM IS INTENDED ONLY FOR DEMONSTRATION PURPOSES, AND IS NOT\n"); printf("MEANT TO BE USED AS AN ACTUAL ALARM SYSTEM.\n"); printf("\n");printf("\n"); printf("Press any key to begin program.\n"); key=getch(); clrscr(); puts("The program will now check for 'open' sensors. If any of the"); puts("digital input lines is grounded, it will read as an open sensor."); puts("Make desired connections and press RETURN to continue."); old_arming_stations=get_status(Address); do{ read_sensors(sensors_now,Address); for(i=0;i<=15;i++) { if (!sensors_now[i]) printf("Sensor #%d %s\n",i,"is open"); } printf("\n"); printf("Press ESC to re-scan, RETURN to begin alarm scanning."); key=getch(); clrscr(); }while(key!=13); for(;;){ /*this creates an infinite loop*/ printf("Waiting to be armed, press any key halt program.\n"); do{ arming_stations=get_status(Address); if (kbhit()) getch(),exit(0); }while(arming_stations == old_arming_stations); sound(1000); delay(300); nosound(); printf("Alarm system will activate in 15 seconds"); read_sensors(sensors_at_arm,Address); old_arming_stations=get_status(Address); start=time(NULL); do{ }while(difftime(time(NULL),start) != 15); printf("\n"); printf("ALARM SYSTEM ACTIVE AND ARMED\n\n"); sound(900); delay(300); nosound(); j=0; do{ read_sensors(sensors_now,Address); for(i=1;i<=15;i++) { if(sensors_now[i-1] != sensors_at_arm[i-1]) j = i; } /*for*/ arming_stations = get_status(Address); if (arming_stations != old_arming_stations) j = -1; /*flag used to signal alarm is de-activated*/ }while(!j); if (j == -1) { start=time(NULL); printf("Alarm deactivated at %s",(asctime(gmtime(&start)))); sound(900); delay(100); nosound(); delay(50); sound(900); delay(100); nosound(); } else { printf("Sensor #%d has been activated!!\n\n",j); start=time(NULL); printf("The time of alarm is %s",asctime(gmtime(&start))); old_arming_stations=get_status(Address); ALARM(Address); delay(150); } /*else*/ } /* for(;;) this end is used to send program back to await re-arming*/ }