#define BASEADDR 0x300 /*declare base address for IOD-24 card*/ #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: SEPTEMBER 4, 1991 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/ 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*/ void initialize(){ /*this procedure sets MODE 0 as active, */ outportb(BASEADDR+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 char tempA; unsigned char tempB; tempA = inportb(BASEADDR); tempB = inportb(BASEADDR+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(){ int temp; /*this sets status to the lower half */ temp=inportb(BASEADDR+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(){ long int temp=0; sound(2000); /*this starts the computer's speaker */ outportb(BASEADDR+2,0xF0); /*this sets port C's upper nybble's */ do{ /*bits to ON (1111 binary = F hex). */ arming_stations=get_status(); /*This activates four alarm outputs, */ if (arming_stations != old_arming_stations) temp=2000000000;/*dis-armed*/ /*and then toggles port C Hi's LSB */ outportb(BASEADDR+2,inportb(BASEADDR+2) ^ 0x10); }while (temp++ < 200000000); /*which might be used with an external*/ nosound(); /*speaker to produce a siren*/ } /*procedure ALARM*/ /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/ main() { time_t start; initialize(); clrscr(); gotoxy(5,5); 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. The base address should be\n"); printf("set to 300 hex.\n"); printf("\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(); do{ read_sensors(sensors_now); 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(); 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); old_arming_stations=get_status(); 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); for(i=1;i<=15;i++) { if(sensors_now[i-1] != sensors_at_arm[i-1]) j = i; } /*for*/ arming_stations = get_status(); 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(); ALARM(); delay(150); } /*else*/ } /* for(;;) this end is used to send program back to await re-arming*/ }