'this program uses the output pins to control both data to 4511 and multiplexing 'pins 4 - 7 contain data for 4511 whilst pins 0 - 3 turn on correct display 'portc pin0 is counter input 'portc pin1 is the alarm on/off control input 'portc pin2 is data input 'portc pin4 is the relay control output 'portc pin5 is the buzzer control output 'portc pin6 is alarm set indicator = DP on units hours 'portc pin7 is the AM PM indicator = DP on units minutes 'adc0 is mode switch 'adc1 is function switch 'b11 records mode switch 2 = hrs; 1 = min; 0 = run 'b12 records function switch 2 = set time; 1 = set alarm on time; 0 = set alarm off time 'b13 contains hours during set hours process 'b14 contains mins during set minutes process 'debug symbol m = b9 symbol h = b10 symbol new = b3 symbol old = b4 symbol onmin = b5 symbol onhr = b6 symbol offmin = b7 symbol offhr = b8 setfreq m8 dirsc = %11110000 settimer count 62536 'set timer to count minutes new = timer old = new set: if old <> new then gosub increment 'keep checking time gosub check 'check what to do b11 = b0 'permanently keep values so that changes can be detected b12 = b1 if b11 = 0 then 'this is exit set condition goto main elseif b11 = 2 then 'check whether to set hrs or mins gosub hr else gosub minutes endif on b12 gosub aloff2, alon2,time2 'restore the updated hour or minute goto set 'subroutine hr increments hours hr: if timer <> new then gosub increment 'keep checking time on b12 gosub aloff1, alon1, time1 'get correct hour to update in b13 loop1: for b15 = 0 to 20 b20 = b13 dig 0 'get and display 1st digit b20 = 16*b20 'put data in upper nibble, invert & set bit 2 b20 = ~b20 - 11 pins = b20 gosub check 'check if selection has changed if b1 <> b12 or b0 <> b11 then return endif if timer <> new then gosub increment 'keep checking time b20 = b13 dig 1 'get and display second digit b20 = 16*b20 b20 = ~b20 - 7 'put data in upper nibble, invert & set bit 3 pins = b20 gosub check 'check if selection has changed if b1 <> b12 or b0 <> b11 then return endif next b15 if pin2 = 0 then loop1 'no button is pressed so go back b13 = b13 + 1 'the button is pressed so increment hrs if b13 = 24 then b13 = 0 endif goto loop1 minutes: if timer <> new then gosub increment 'keep checking time on b12 gosub aloff1, alon1, time1 'get correct minute to update in b14 loop2: for b15 = 0 to 20 b20 = b14 dig 0 'get and display 1st digit b20 = 16*b20 b20 = ~b20 - 14 'put data in upper nibble, invert & set bit 0 pins = b20 gosub check 'check if selection has changed if b1 <> b12 or b0 <> b11 then return endif if timer <> new then gosub increment 'keep checking time b20 = b14 dig 1 'get and display second digit b20 = 16*b20 b20 = ~b20 - 13 'put data in upper nibble, invert & set bit 1 pins = b20 gosub check 'check if selection has changed if b1 <> b12 or b0 <> b11 then return endif next b15 if pin2 = 0 then loop2 'no button is pressed so go back b14 = b14 + 1 'the button is pressed so increment hrs if b14 = 60 then b14 = 0 endif goto loop2 return 'main routine is normal clock operation main: if pin1 = 1 then 'check if alarm is set high portc 6 'if so set DP on unit hours else low portc 6 'if not turn off indicator low portc 5 'and turn off buzzer endif new = timer if new = old then b0 = m dig 0 'get first digit of minutes b0 = 16*b0 b0 = ~b0 - 14 'put data in upper nibble, invert & set bit 0 pins = b0 'display if onhr = h and onmin = m and pin1 = 1 then high portc 4 'turn on relay endif b0 = m dig 1 'get 2nd digit of minutes b0 = 16*b0 b0 = ~b0 - 13 'put data in upper nibble, invert & set bit 1 pins = b0 'display if h > 12 then b2 = h - 12 'set for 12 hr clock else b2 = h endif b0 = b2 dig 0 'get 1st digit of hours b0 = 16*b0 b0 = ~b0 - 11 'put data in upper nibble, invert & set bit 2 pins = b0 'display if offhr = h and offmin = m and pin1 = 1 then low portc 4 'turn off relay high portc 5 'turn on buzzer endif b0 = b2 dig 1 'get 2nd digit of hours b0 = 16*b0 b0 = ~b0 - 7 'put data in upper nibble, invert & set bit 3 pins = b0 'display else gosub increment endif readadc 0,b0 if b0 > 64 then set goto main increment: new = timer old = new m = m+1 'increment minutes if m=60 then m=0 h = h + 1 endif if h = 24 then h = 0 end if if h > 11 then 'check am or pm high portc 7 'turn on or off am/pm indicator else low portc 7 endif return 'subroutine check determines switch positions check: readadc 0,b0 'check if hr or min to set or to resume clock if b0 < 64 then 'this is the run condition b0 = 0 elseif b0 > 190 then 'this is set hrs condition b0 = 2 else b0 = 1 'this is the set mins condition endif readadc 1,b1 ' now check which to set - time;alarm on time; alarm off time if b1 < 64 then b1 = 0 'setting alarm off time elseif b1 > 190 then b1 = 2 'setting time else b1 = 1 'setting alarm on time endif return time1: settimer off b13 = h b14 = m return time2: h = b13 m = b14 if h > 11 then 'check am/pm and set indicator high portc 7 else low portc 7 endif settimer count 62536 'set timer to count minutes new = timer old = new return alon1: b13 = onhr b14 = onmin return alon2: onhr = b13 onmin = b14 return aloff1: b13 = offhr b14 = offmin return aloff2: offhr = b13 offmin = b14 return