' ============================================================================== ' ' File...... Ex10 - Clock.BS2 ' Purpose... Simple software clock ' Author.... Parallax ' E-mail.... support@parallax.com ' Started... ' Updated... 01 MAY 2002 ' ' {$STAMP BS2} ' {$PBASIC 2.5} ' ' ============================================================================== ' -----[ Program Description ]-------------------------------------------------- ' This program monitors a 1 Hz input signal and uses it as the timebase for ' a software clock. ' -----[ I/O Definitions ]------------------------------------------------------ Segs VAR OutL ' segments DigSel VAR OutC ' digit select Tic PIN 15 ' 1 Hz Pulse Generator input ' -----[ Constants ]------------------------------------------------------------ DecPoint CON %10000000 ' decimal point bit Blank CON %00000000 ' all segments off Dig0 CON %1111 ' digit select control Dig1 CON %1110 Dig2 CON %1101 Dig3 CON %1011 Dig4 CON %0111 IsLow CON 0 ' Tic input is low IsHigh CON 1 ' Tic input is high ' -----[ Variables ]------------------------------------------------------------ secs VAR Word ' seconds time VAR Word ' formatted time digit VAR Nib ' current display digit ' -----[ EEPROM Data ]---------------------------------------------------------- ' .abcdefg ' -------- DecDig DATA %01111110 ' 0 DATA %00110000 ' 1 DATA %01101101 ' 2 DATA %01111001 ' 3 DATA %00110011 ' 4 DATA %01011011 ' 5 DATA %01011111 ' 6 DATA %01110000 ' 7 DATA %01111111 ' 8 DATA %01111011 ' 9 ' -----[ Initialization ]------------------------------------------------------- Initialize: DirL = %11111111 ' make segments outputs DirC = %1111 ' make digit selects outputs DigSel = Dig0 ' all digits off ' -----[ Program Code ]--------------------------------------------------------- Main: DO GOSUB Show_Time ' show current digit ' other code ~450 ms IF (Tic = IsHigh) THEN secs = (secs + 1) // 3600 ' update seconds counter DO GOSUB Show_Time ' show current digit ' other code ~450 ms LOOP UNTIL (Tic = IsLow) ENDIF LOOP END ' -----[ Subroutines ]---------------------------------------------------------- Show_Time: time = (secs / 60) * 100 ' get minutes, put in hundreds time = time + (secs // 60) ' get seconds, put in 10s & 1s Segs = Blank ' clear display ' enable digit LOOKUP digit, [Dig1, Dig2, Dig3, Dig4], digSel READ (DecDig + (time DIG digit)), Segs ' put segment pattern in digit IF (digit = 2) THEN Segs = Segs + DecPoint ' illuminate decimal point ENDIF digit = (digit + 1) // 4 ' get next digit RETURN