'*********************************************************************************************************************** ' KAPbell_FINAL_1.bs2 written by marco garza dba picotek modified for keypad input on 01/10/09 ' later modified FOR schedule ' last modified on 05/03/2010 to test with 1 sec time base. ' last modified: 05/19/09 since 04/06/08 since 02/02/09 ' this is the kapschool automated bell system program accepting time base input in pin 12 ' further modified to initiate the time base circuit via pin 18/input 13 for reset pulse ' last updated 09/21/09 for output voltage on pin 12 (originally input sec now to source +5V schedule) and change pin 13 ' to input second pulse '*********************************************************************************************************************** '{$STAMP BS2e} '{$PBASIC 2.5} txt_pin CON 14 ' sets the LCD pin to pin 14 txt_baud CON 84 ' sets the LCD baud to 9600 hours VAR Byte ' stores hours minutes VAR Byte ' stores minutes seconds VAR Byte ' stores 1/10 seconds h_tens VAR Nib ' stores tens and units digits of h_units VAR Nib ' hours, minutes, and seconds m_tens VAR Nib m_units VAR Nib s_tens VAR Nib s_units VAR Nib btn_1 VAR Byte ' necessary allocation for button command btn_2 VAR Byte key_pad VAR Byte per_hour VAR Byte ' assigns values in array to reg_hours per_minute VAR Byte ' assigns values in array to reg_minutes per_step VAR Byte ' reg_step steps though the tables to see if any ordered pairs apply to hours and minutes A VAR Byte ' A, B, C, D can probably be nibbles B VAR Byte C VAR Byte D VAR Byte INPUT 0 ' makes pins 0-4 inputs INPUT 1 INPUT 2 INPUT 3 INPUT 4 ' insert your DIRS here/ sets the inputs abd outputs if the BS2 btn_1 = 0 btn_2 = 0 ' initializes the button workspace HIGH txt_pin ' sets to not recieve data PAUSE 200 ' allows the LCD to initiate SEROUT txt_pin, txt_baud, [17, 22, 12] ' turn backlight on, start LCD, clear display HIGH 12 ' output high for schedule voltage PAUSE 10 ' pause 5 ms for clear display 'program starts here SEROUT txt_pin, txt_baud, [" Hello", 13] SEROUT txt_pin, txt_baud, [" Kapschool"] PAUSE 3000 Ent_Hours: SEROUT txt_pin, txt_baud, [12] ' clears display PAUSE 10 ' allows display to clear SEROUT txt_pin, txt_baud, [" Set Time "] SEROUT txt_pin, txt_baud, [148, "hours: 00 "] ' entering hours and displaying on LCD SEROUT txt_pin, txt_baud, [25, 155] GOSUB Get_Data LOOKDOWN key_pad, [13,2,1,0,6,5,4,10,9,8,14,12], h_tens ' converts BCS input or 74C922 to keypad numbers IF h_tens > 9 THEN Ent_Hours SEROUT txt_pin, txt_baud, [155, DEC1 h_tens] SEROUT txt_pin, txt_baud, [156] GOSUB Get_Data LOOKDOWN key_pad, [13,2,1,0,6,5,4,10,9,8,14,12], h_units IF h_units > 9 THEN Ent_Hours SEROUT txt_pin, txt_baud, [156, DEC1 h_units, 156] hours = 10 * h_tens + h_units IF hours < 24 THEN Ent_Minutes IF hours >= 24 THEN GOSUB Inv_Data GOTO Ent_Hours Ent_Minutes: PAUSE 500 PAUSE 10 ' allows display to clear SEROUT txt_pin, txt_baud, [148, "minutes: 00 "] SEROUT txt_pin, txt_baud, [157] ' places blinking cursor in tens digit GOSUB Get_Data LOOKDOWN key_pad, [13,2,1,0,6,5,4,10,9,8,14,12], m_tens IF m_tens > 9 THEN Ent_Hours SEROUT txt_pin, txt_baud, [157, DEC1 m_tens] SEROUT txt_pin, txt_baud, [158] ' places blinking cursor in units digit GOSUB Get_Data LOOKDOWN key_pad, [13,2,1,0,6,5,4,10,9,8,14,12], m_units IF m_units > 9 THEN Ent_Minutes SEROUT txt_pin, txt_baud, [158, DEC1 m_units, 158] minutes = 10 * m_tens + m_units IF minutes < 60 THEN Ent_Seconds IF minutes >= 60 THEN GOSUB Inv_Data GOTO Ent_Minutes Ent_Seconds: PAUSE 500 PAUSE 10 ' allows display to clear SEROUT txt_pin, txt_baud, [148, "seconds: 00 "] SEROUT txt_pin, txt_baud, [157] ' places blinking cursor in tens digit GOSUB Get_Data LOOKDOWN key_pad, [13,2,1,0,6,5,4,10,9,8,14,12], s_tens IF s_tens > 9 THEN Ent_Minutes SEROUT txt_pin, txt_baud, [157, DEC1 s_tens] SEROUT txt_pin, txt_baud, [158] ' places blinking cursor in units digit GOSUB Get_Data LOOKDOWN key_pad, [13,2,1,0,6,5,4,10,9,8,14,12], s_units IF s_units > 9 THEN Ent_Seconds SEROUT txt_pin, txt_baud, [158, DEC1 s_units] seconds = 10 * s_tens + s_units PAUSE 50 ' waits for the user to release the button in order to receive a set time interrupt IF seconds < 60 THEN Disp_Hm IF seconds >= 60 THEN GOSUB Inv_Data GOTO Ent_Seconds Disp_Hm: SEROUT txt_pin, txt_baud, [12,22] ' clears display and gets rid of PAUSE 10 ' blinking cursor SEROUT txt_pin, txt_baud, [" KAPS: : : ",13] ' this displays the entered digits so there are no blanks while reading the clock source SEROUT txt_pin, 84, [135, DEC2 hours, 138, DEC2 minutes, 141, DEC2 seconds ] Main: DO ' main routine BUTTON 13,1,255,0,btn_2,0,Main ' waits for clock input on pin 13/currently disabled for troublshooting ' calculate hours, minutes, seconds IF seconds = 60 THEN seconds = 0: minutes = minutes + 1 IF minutes = 60 THEN minutes = 0: hours = hours + 1 IF hours = 24 THEN hours = 0 ' display digits on LCD on line 2. The values 148, 153, 158 ' place the curseor at character 0, 5, and 10 for the time values. SEROUT txt_pin, 84, [135, DEC2 hours, 138, DEC2 minutes, 141, DEC2 seconds ] IF IN0 = 1 AND IN2 = 1 AND IN3 = 1 AND IN4 = 1 THEN Ent_Hours ' reads the inputs 0-4 to see if keypress and if number is 14 (*) for ' resetting time ' maybe here apply and IF/THEN command to check if Input(firedrill) is high. IF so, BRS to output PULSEOUT, ELSE RETURN ' this is where you can have the following for the fire drill ' IF IN# = 0 (input Pin 5) THEN GOSUB Sel_Sched ' ELSE PULSEOUT 7, 125 GOSUB Sel_Sched ' this will go to the subroutine when seconds are zero. the subroutine PAUSE 600 seconds = seconds + 1 ' will then branch to the selected schedule via the button command ' the GOSUB command is needed if not used the return does not go back 'PAUSE 1000 ' right now used for troubleshooting 'IF IN12 = 1 THEN seconds = seconds + 1 ' increment 1/10 second counter LOOP ' repeat main routine Get_Data: BUTTON 0,1,255,0,btn_1,0,Get_Data ' check pin 1 for data 1 = press, 2 = no press PAUSE 100 ' allows time for the 74C922 to get data A = IN1 ' put binary value into variable B = IN2 C = IN3 D = IN4 A = 1 * A ' change BCD to digit B = 2 * B C = 4 * C D = 8 * D key_pad = A + B + C + D ' convert to decimal PAUSE 50 ' allows time for data to get to RAM RETURN Inv_Data: SEROUT txt_pin, txt_baud, [12,22] ' clears display to zero and turns the curos off PAUSE 10 SEROUT txt_pin, txt_baud, [133, "Error!", 13] SEROUT txt_pin, txt_baud, [" Invalid Data "] PAUSE 1500 SEROUT txt_pin, txt_baud, [12] ' clears display PAUSE 10 ' allows display to clear SEROUT txt_pin, txt_baud, [" Set Time "] RETURN Sel_Sched: IF IN8 = 1 THEN GOSUB Reg_Bell IF IN9 = 1 THEN GOSUB Assy_Bell IF IN10 = 1 THEN GOSUB Finl_Bell IF IN11 = 1 THEN GOSUB Aux_Bell RETURN ' returns back to the Main loop Reg_Bell: SEROUT txt_pin, txt_baud, [148, " Regular Sched. "] FOR per_step = 0 TO 18 LOOKUP per_step, [7 ,8 ,8 ,8 ,9 ,9 ,10,10,10,11,11,12,12,1 ,1 ,1 ,2 ,2 ,3 ], per_hour ' assigns to hours LOOKUP per_step, [55,0 ,45,50,35,55,0 ,45,50,35,40,25,55,0 ,45,50,35,40,25], per_minute ' assigns to minutes IF seconds = 0 AND per_hour = hours AND per_minute = minutes THEN PULSOUT 6, 125 NEXT RETURN ' returns back to the main program Assy_Bell: SEROUT txt_pin, txt_baud, [148, " Assembly Sched."] FOR per_step = 0 TO 18 LOOKUP per_step, [7 ,8 ,8 ,8 ,9 ,10,10,10,10,11,11,12,12,1 ,1 ,1 ,2 ,2 ,3 ], per_hour LOOKUP per_step, [55,0 ,45,49,34,6 ,10,55,59,44,48,33,58,2 ,47,51,36,40,25], per_minute IF seconds = 0 AND per_hour = hours AND per_minute = minutes THEN PULSOUT 6, 125 NEXT RETURN Finl_Bell: SEROUT txt_pin, txt_baud, [148, " Finals Sched. "] FOR per_step = 0 TO 6 LOOKUP per_step, [7 ,8,9 ,10,11,11,1], per_hour LOOKUP per_step, [55,0,30,0 ,25,35,0], per_minute IF seconds = 0 AND per_hour = hours AND per_minute = minutes THEN PULSOUT 6, 125 NEXT RETURN Aux_Bell: SEROUT txt_pin, txt_baud, [148, " AUX. Sched. "] FOR per_step = 0 TO 18 LOOKUP per_step, [7 ,8 ,8 ,8 ,9 ,9 ,10,10,10,11,11,12,12,1 ,1 ,1 ,2 ,2 ,3 ], per_hour LOOKUP per_step, [55,0 ,45,50,35,55,0 ,45,50,35,40,25,55,0 ,45,50,35,40,25], per_minute IF seconds = 0 AND per_hour = hours AND per_minute = minutes THEN PULSOUT 6, 125 NEXT RETURN