symbol s = b5 symbol ts = b6 symbol m = b7 symbol tm = b8 symbol h = b9 symbol th = b10 symbol new = b3 symbol old = b4 setfreq m8 set: s = 0 'initialise all counters ts = 0 m = 0 tm = 0 h = 0 th = 0 dirsc = %00111110 'set all c pins to output except pins 0, 6 & 7 pins = 0 'set all outputs to 0 - will turn off alarm if sounding b11 = 100 'set delay parameter settimer off timer = 0 loop1: 'sets digits gosub fin 'find which digit to change on b2 gosub sec,tsec,mi,tmin,hr,thr 'go to correct subroutine for setting digit if pin6 = 1 then go 'check if mode switch has changed goto loop1 'subroutine to check which digit to set fin: readadc 0,b1 'read position of function switch if b1 > 229 then 'b2 codes for 6 positions b2 = 5 '0 = seconds, 1 = tens seconds, else if b1 > 178 then '2 = minutes, 3 = tens minutes b2 = 4 '4 = hours, 5 = tens hours else if b1 > 127 then b2 = 3 else if b1 > 76 then b2 = 2 else if b1 > 25 then b2 = 1 else b2 = 0 endif return 'subroutine to SET SECONDS sec: pins = 1 'turn on seconds digit b0 = ~s b0 = 2*b0 portc = b0 'load port c with seconds value low 0 gosub fin 'check if digit to set has changed if pin6 = 1 or b2 <> 0 then return 'check if mode switch has changed endif bs: if pin7 = 0 then sec 'check button press s = s + 1 'count second s = s//10 'remove overflows high 0 b0 = ~s b0 = 2*b0 portc = b0 'load port c with seconds value gosub delay goto bs 'subroutine to set TENS SECONDS tsec: pins = 2 'turn on tens seconds digit b0 = ~ts b0 = 2*b0 portc = b0 'load port c with tens seconds value low 1 gosub fin 'check if digit to set has changed if pin6 = 1 or b2 <> 1 then return 'check if mode switch has changed endif bts: if pin7 = 0 then tsec 'check button press ts = ts + 1 'count tens second ts = ts//6 'remove overflows high 1 b0 = ~ts b0 = 2*b0 portc = b0 'load port c with tens seconds value gosub delay goto bts 'subroutine to set MINUTES mi: pins = 4 'turn on minutes digit b0 = ~m b0 = 2*b0 portc = b0 'load port c with minutes value low 2 gosub fin 'check if digit to set has changed if pin6 = 1 or b2 <> 2 then return 'check if mode switch has changed endif bmi: if pin7 = 0 then mi 'check button press m = m + 1 'count minutes m = m//10 'remove overflows high 2 b0 = ~m b0 = 2*b0 portc = b0 gosub delay goto bmi 'subroutine to set TENS MINUTES tmin: pins = 8 'turn on tens minutes digit b0 = ~tm b0 = 2*b0 portc = b0 'load port c with tens minutes value low 3 gosub fin 'check if digit to set has changed if pin6 = 1 or b2 <> 3 then return 'check if mode switch has changed endif btm: if pin7 = 0 then tmin 'check button press tm = tm + 1 'count tens minutes tm = tm//6 'remove overflows high 3 b0 = ~tm b0 = 2*b0 portc = b0 'load port c with tens minutes value gosub delay goto btm 'subroutine to set HOURS hr: pins = 16 'turn on hours digit b0 = ~h b0 = 2*b0 portc = b0 'load port c with hours value low 4 gosub fin 'check if digit to set has changed if pin6 = 1 or b2 <> 4 then return 'check if mode switch has changed endif bhr: if pin7 = 0 then hr 'check button press h = h + 1 'count hours h = h//10 'remove overflows high 4 b0 = ~h b0 = 2*b0 portc = b0 'load port c with hours value gosub delay goto bhr 'subroutine to set TENS HOURS thr: pins = 32 'turn on tens hours digit b0 = ~th b0 = 2*b0 portc = b0 'load port c with tens hours value low 5 gosub fin 'check if digit to set has changed if pin6 = 1 or b2 <> 5 then return 'check if mode switch has changed endif bthr: if pin7 = 0 then thr 'check button press th = th + 1 'count tens hours th = th//10 'remove overflows high 5 b0 = ~th b0 = 2*b0 portc = b0 'load port c with tens hours value gosub delay goto bthr go: settimer count 65486 'timer set to count 50 ac mains pulses high 7 'turn on power new = timer 'set old & new timer values old = new loop2: 'multiplexing display low 5 'turn off tens hours if pin6 = 0 then set 'check if run cancelled new = timer 'read timer if new = old then 'no new time so display b0 = ~s b0 = 2*b0 portc = b0 'load seconds on bcd pins high 0 'turn on seconds digit pause 1 'wait b0 = ~ts b0 = 2*b0 low 0 'turn off seconds portc = b0 'load tens seconds on bcd pins high 1 'turn on tens seconds digit pause 1 'wait b0 = ~m b0 = 2*b0 low 1 'turn off tens seconds portc = b0 'load minutes on bcd pins high 2 'turn on minutes digit pause 1 'wait b0 = ~tm b0 = 2*b0 low 2 'turn off minutes portc = b0 'load tens minutes on bcd pins high 3 'turn on tens minutes digit pause 1 'wait b0 = ~h b0 = 2*b0 low 3 'turn off tens minutes portc = b0 'load hours on bcd pins high 4 'turn on hours digit pause 1 'wait b0 = ~th b0 = 2*b0 low 4 'turn off hours portc = b0 'load tens hours on bcd pins high 5 'turn on tens hours digit pause 1 'wait goto loop2 'finished display so check time endif old = new 'update timer if s <> 0 then 'decrement display dec s else if ts <> 0 then dec ts s = 9 else if m <> 0 then dec m ts = 5 s = 9 else if tm <> 0 then dec tm m = 9 ts = 5 s = 9 else if h <> 0 then dec h tm = 5 m = 9 ts = 5 s = 9 else if th <> 0 then dec th h = 9 tm = 5 m = 9 ts = 5 s = 9 endif b0 = s + ts + m + tm + h + th 'check end of time condition if b0 = 0 then goto turnoff goto loop2 turnoff: pins = %01000000 'turn power off and buzzer on portc = 30 'set zero for display - must use complement b11 = 20 'set delay parameter b2 = 0 'displays seconds in loop3 and delay loop3: 'flashing zero high b2 if pin6 = 0 then set 'check if mode change gosub delay pause 500 goto loop3 delay: 'balances brightness when single digit is being displayed for b0 = 1 to b11 low b2 pause 6 high b2 pause 1 next b0 low b2 pause 6 return