This is only a preview of the March 1994 issue of Silicon Chip. You can view 34 of the 96 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "Build A 50W Audio Amplifier Module":
Articles in this series:
Items relevant to "Level Crossing Detector For Model Railways":
Items relevant to "Switching Regulators Made Simple":
Items relevant to "Voice Activated Switch For FM Microphones":
Articles in this series:
Items relevant to "Build A Simple LED Chaser":
Items relevant to "Computer Bits":
Articles in this series:
Articles in this series:
|
COMPUTER BITS
BY DARREN YATES
A binary clock of the software kind
Binary notation is the essence of programming
whether you do it in assembler or BASIC. This
month, we present one of our past projects in
software form – a binary clock.
SILICON CHIP BINARY CLOCK
This is the on-screen display generated by the Binary Clock software. For those
unaccustomed to binary readouts, the display also shows the time in hours,
minutes & seconds.
Programming Tip
If you’re always losing your DOS prompt amongst the other information on
screen, add these lines to your AUTOEXEC.BAT file. The prompt becomes
yellow type on a red background while normal screen printing remains as
white on black for normal DOS operation. The current time and date are
also displayed.
PROMPT $e[1;33;41m Time:$t$_ Date:$d$_ $P$G $e[0;37;40m
PROMPT Time:$t$_Date:$d$_$e[1;33;41m$p$g$e[0;37;40m
Note: You must have the ANSI.SYS device driver installed in your CONFIG.
SYS file. If not, add the following line:
DEVICE = C:\DOS\ANSI.SYS
66 Silicon Chip
Talking in ones and zeros in
something that we humans do not
undertake easily yet they are the only
real language our faithful computing
companions can understand. So the
programming languages flourish (BASIC, Pascal, C, Lisp, ADA, Prolog and
Cobol), everyone trying to make their
own to suit their own application:
BASIC for beginners, Cobol for economists who know little about computer
language, and C for programmers who
know little about the English language
(just kidding).
However, the one thing they all have
in common is the translation of what
the programmer understands into
something the computer understands.
And to this end, a knowledge of binary
notation is vital.
Back in the October 1993 issue,
we presented a project which used
programmable array logic or PALs to
produce a clock which used a series of
LEDs to display the time in binary format. Then, late last year, we received
a letter from a reader, Eric Hughes of
Tasmania, who had developed a program on the Atari ST which performed
the same task.
Based on his ideas, we generated our
own version for the PC. It runs under
QBASIC or most of the QuickBASIC
compliers, at least those from versions
3.0 and up. The program listing is
published here and requires a VGA
card and monitor.
The program briefly is divided up
into two sections – the main module
and the screen display routine called
DISPLAY. The main module sets up
the output screen, placing the various
‘8 4 2 1’ sequences. It also draws and
captures the “lights” that indicate
which bits are on or off. This is done
by the GET statement.
The main operation of the program
is inside the WHILE..WEND loop in the
main module. This continuously updates the time and calls the DISPLAY
subroutine each second to update the
display.
The DISPLAY subroutine does a
number of things. First, it takes the
time from the TIME$ command and
sections it off into hours, minutes and
seconds and places these values into
appro
priately named strings. These
values are then placed into a three
level array, ‘D’. Inside the FOR V..NEXT
V loop, these values are turned into a
binary string array H$ containing 1s
and 0s.
Array H$(0) now contains a binary
string which represents the hours,
H$(1) the minutes, and H$(2) the seconds. The final FOR W..NEXT W loop
checks each character in the string
array H$ and then places either the
“off light” if its a 0 or the “lit light”
if it’s a 1. This continues indefinitely
until the Q key is pressed.
This program can be easily adapted
to suit CGA screens by switching to
screen 2 and scaling all of the coordinates from a 640 x 480 grid down to a
320 x 200 grid; similarly for EGA, by
switching to SCREEN 9 and scaling
the coordinates from 640 x 480 to
640 x 350.
You may also like to try to shrink
the code a little further. This program
is by no means the definitive version
and there are several ways in which it
could be improved, but it could form
the basis of a useful learning tool.
The WHILE D(V)<>0 loop in the
DISPLAY subroutine is where the decimal number is converted to binary.
It uses a simple form of successive
approximation, as used by many
up-market analog-to-digital converters
(ADCs). It first checks the most significant bit to see if it is set.
If so, it adds a 1 to the appropriate
H$ array. This is done by dividing the
original section of time, say minutes
for example, by 2 and then taking the
integer of that value. You then subtract
twice this integer from the original
number, which is stored in variable
F. If the remainder is 1 then that bit is
set, otherwise it is 0.
For those of you who don’t wish to
type in the whole program, we can
supply BINARY.BAS plus an executable version, BINARY.EXE, on disc for
$7 plus $3 for postage. Please specify
the disc format required. You can
phone in your order along with your
SC
credit card details.
Binary Clock Program Listing
REM Binary Clock for PCs
REM Written By DARREN YATES B.Sc. – requires VGA screen & card
DIM SHARED B(200), c(200)
DECLARE SUB DISPLAY (B, c, A$)
SCREEN 12, 1
LINE (0, 0)-(639, 479), 2, B
LOCATE 2, 28: PRINT “ SILICON CHIP Binary Clock”
LOCATE 10, 38: PRINT “HOURS”
LOCATE 22, 15: PRINT “MINUTES”
LOCATE 22, 60: PRINT “SECONDS”
BIN$ = “32 16 8 4 2 1”
LOCATE 28, 7: PRINT BIN$
LOCATE 16, 29: PRINT BIN$
LOCATE 28, 51: PRINT BIN$
SCREEN 12, 1
CIRCLE (20, 20), 15, 15
GET (4, 4)-(36, 36), B
PAINT (19, 20), 4, 15
GET (4, 4)-(36, 36), c
PUT (4, 4), c
WHILE QUIT$ <> “Q” AND QUIT$ <> “q”
FOR G = 1 TO 3
H$(G) = “”
NEXT G
A$ = TIME$
IF A$ <> OLDA$ THEN CALL DISPLAY(B, c, A$)
OLDA$ = A$
QUIT$ = INKEY$
WEND
SUB DISPLAY (B, c, A$)
DIM D(3), H$(3)
hour$ = MID$(A$, 1, 2)
minute$ = MID$(A$, 4, 2)
second$ = MID$(A$, 7, 2)
hour = VAL(hour$)
minute = VAL(minute$)
second = VAL(second$)
D(1) = hour
D(2) = minute
D(3) = second
LOCATE 7, 39: PRINT D(1)
LOCATE 20, 17: PRINT D(2)
LOCATE 20, 62: PRINT D(3)
FOR v = 1 TO 3
WHILE D(v) <> 0
f = D(v)
D(v) = INT(f / 2)
r = f - (2 * D(v))
IF r = 0 THEN H$(v) = “0” + H$(v)
IF r = 1 THEN H$(v) = “1” + H$(v)
WEND
IF LEN(H$(v)) < 6 THEN
FOR G = 1 TO 6 - LEN(H$(v))
H$(v) = “0” + H$(v)
NEXT G
END IF
NEXT v
FOR w = 1 TO LEN(H$(1))
bit$ = MID$(H$(1), (LEN(H$(1)) - (w - 1)), 1)
IF bit$ = “1” THEN PUT (425 - (w * 35), 190), c, PSET
IF bit$ = “0” THEN PUT (425 - (w * 35), 190), B, PSET
NEXT w
FOR w = 1 TO LEN(H$(2))
bit$ = MID$(H$(2), (LEN(H$(2)) - (w - 1)), 1)
IF bit$ = “1” THEN PUT (250 - (w * 35), 380), c, PSET
IF bit$ = “0” THEN PUT (250 - (w * 35), 380), B, PSET
NEXT w
FOR w = 1 TO LEN(H$(3))
bit$ = MID$(H$(3), (LEN(H$(3)) - (w - 1)), 1)
IF bit$ = “1” THEN PUT (600 - (w * 35), 380), c, PSET
IF bit$ = “0” THEN PUT (600 - (w * 35), 380), B, PSET
NEXT w
END SUB
March 1994 67
|