This is only a preview of the October 2007 issue of Silicon Chip. You can view 40 of the 112 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 "Oscar: Electronic Noughts & Crosses Game":
Items relevant to "PICProbe: A Versatile Logic Probe":
Items relevant to "Rolling Code Security System; Pt.1":
Items relevant to "Simple Data-Logging Weather Station; Pt.2":
Purchase a printed copy of this issue for $10.00. |
It’s the modern-day equivalent of one of the
world’s first “computer games”
OSCAR:
2007
STYLE
B y B R I A N H E A LY
Noughts and crosses may rate quite poorly amongst young gamers
of today but in the late 1960s, a machine, more often called an
electronic brain than a computer, playing noughts and crosses
against a human opponent, was quite a sensation.
T
HE FIRST COMPUTERS WERE
built during World War II to
attempt to decode German coded
signals. From this early work sprung
“EDSAC” (for Electronic Delay Storage Automatic Calculator), the first
truly programmable computer. It was
built at Cambridge University (UK)
in 1949.
This computer, shown in the background above, was used by mathematicians for research and learning. It
26 Silicon Chip
contained 3000 valves and consumed
some 12kW of power!
History was made in 1952 by A.S.
Douglas, a young PhD student, when
he used it for another purpose: he
programmed it to play noughts and
crosses. The computer used a cathode
ray tube to display its output, which
means this was the very first video
game in the world.
In the mid-1960s, both Sydney and
Melbourne technical museums at-
tracted large crowds with a “computer’
which played noughts and crosses
against a human opponent.
In 1968, when the author was aged
24, he and a friend built a machine
using 70-odd telephone relays and a
uniselector to play the game.
A uniselector, by the way, is a
rotary, solenoid driven, 50-position
switch. They were commonly used
in automatic telephone exchanges at
the time and were even found in some
siliconchip.com.au
This is a Uniselector, a
50-way solenoid-driven
switch. Every automatic
telephone exchange
used these in their
thousands – now they
are virtually a museum
piece. The Uniselector
was used as the basis
for the original “Oscar”
built by the author back
in the 1960s.
older exchanges until quite recently,
before first solid-state devices and
then microcontrollers took over. In a
busy telephone exchange, the noise of
the uniselectors switching back and
forth following the numbers dialled
on a phone perhaps 10 kilometres or
more away was quite deafening!
Our machine was as large as a refrigerator and about twice as heavy! We
called him “OSCAR” and he worked
very well. When you pressed a button
for your turn, the machine started
whirring loudly (the sound coming
from the uniselector), a row of lights
flashed, relays clicked in and out and
finally it all stopped as it brought up
its reply. It was very impressive.
We hired it out to retailer David
Jones and a new shopping centre
called “Westfield” in Wollongong (a
large city south of Sydney) where it
attracted large crowds.
It must be very difficult for people
who did not come through that era to
understand such a reaction, so I will
try to explain.
It was akin to the crowds who
gathered on footpaths outside electrical retailers a decade or so earlier to
watch that new-fangled invention,
television, playing in the windows.
In 1968, very few people had ever
seen a computer “in the flesh”. They
may have seen one in a film where
typically it would be in an almost sacred situation, a series of large metal
cabinets, some with large tape spools
rotating and all attended by well
groomed, bespectacled technicians,
wearing white lab coats, hovering over
it like nurses over a new-born baby.
What the computer actually did was
a complete mystery and there was no
way you were ever going to be able to
get close enough to touch one.
So, if suddenly you were now able
to have contact with a computer, to
challenge it at a game that you unD1 1N4004
K
4.7k
4.7k
14
Vdd
16
1
RA2
RA1 18
A1
OSC1
100pF
A1
MCLR
IC1
PIC16F84A
RESET
S1
K
RB5 11
RB4 10
RB3 9
A1
LED1
3
OSCAR'S
FIRST
TURN
S2
S3
Vss
RA3
A1
A2
A1
A2
S5
K
LED7
S7
S6
K
S4
A2
LED8
S8
S9
K
S10
S11
2
5
2007
A1
A2
A2
LED4
K
K
K
8
RB2
RB1 7
6
RB0
RA4
A1
LED5
A2
LED9
K
A2
LED2
A1
A2
LED6
K
17
RA0
RB7 13
RB6 12
4
A1
A2
LED3
SC 'OSCAR'
+6V
10 F
100nF
4.7k
10k
A
– 2007 version
(RED)
A1
K
A2
(GRN)
0V
LEDS1–9
1N4004
A
K
Fig.1: not a Uniselector in sight (or even hidden!). The PIC chip does all the work of the mechanical monster of 40+
years ago – and this OSCAR is much easier to build.
siliconchip.com.au
October 2007 27
new game and the red button is to allow OSCAR to have the first move in
the game. It is polite and fair to give
OSCAR the first turn at least sometimes. OSCAR is very clever here, as
the square he chooses for his first move
is truly random.
Generating truly random numbers is
difficult for a PC and very difficult for
a PIC but OSCAR employs a trick here.
At the end of a game, following a
reset, OSCAR is not just sitting there
doing nothing – not at all. He is repeatedly counting rapidly from one
through to nine at high speed, until
you press a key to start the game, at
which time he stops counting. The
number that he stops on will be the
square he chooses if you give him
first turn.
How it works
This clipping,
believed to be from
the “Wollongong
Mercury” around
1968, shows the
original fridge-sized
OSCAR noughts and
crosses machine. No,
those aren’t LEDs in
the display – they
weren’t even a glint
in a mad scientist’s
eye back then . . .
derstood well, this was very exciting.
Lots of my older friends still remind
me about OSCAR.
We initially did not know even
how to start to build such a machine.
We eventually worked out that the
machine had to go through a logical
series of steps, in sequence. We never
called it a program but of course, it
was a program.
I have never forgotten the sequence,
so now that PICs have become available, I set out to put the exact same
program from the 1968 OSCAR into
a 2007 PIC16F84A.
The new OSCAR
The new OSCAR is a tiny fraction
of the size of the original. And instead
of requiring a lot of power to operate,
it will run for months on a couple of
batteries. It has nine green buttons in
a 3 x 3 array and nine bicolour LEDs,
also in a 3 x 3 array.
28 Silicon Chip
When you press a button, to take
your first turn, the corresponding
LED illuminates green. OSCAR now
“thinks” for a second or two and then
has his move, illuminating his position red. It is then your chance to have
your second move – and I think you
know the rest.
If OSCAR wins, which is pretty
common, the winning row of three
red LEDs flashes, calling attention to
the player’s demise(!) and the game
is halted. You cannot continue when
you have been defeated.
The first version of the software was
wriiten so that OSCAR never lost a
game. However, that quickly becomes
boring and so the software was later
reworked to give its opponent a chance
to win approximately one in 10 games.
If you do win, your three green LEDs
will flash to indicate success.
There are two more buttons. The
white button is to reset OSCAR for a
If you glance at the circuit you will
see that it is quite simple and does
not use any active components apart
from the PIC.
The circuit is powered from four AA
batteries that sit in a plastic battery box
available from Jaycar. The maximum
voltage for the PIC is 6V, so don’t be
tempted to install a 9V battery.
This PIC can have various oscillators but in this case, we are using a
resistor/capacitor circuit on pin 16 of
the chip. With the values chosen, the
circuit oscillates at around 700kHz.
The RC oscillator is a little cheaper
and somewhat slower than a crystal,
making delay loops easier.
You can see the 700kHz triangular
waveform on pin 16 with a ’scope
and high impedance probe. The PIC
divides this by four to become the system clock and you can see the resulting
180kHz square-wave on pin 15.
Because we are connecting so many
devices to the PIC, we need to do some
multiplexing. The PIC has only 13
input/output connections available,
but we have nine position buttons,
nine red LEDs, nine green LEDs and
a couple more buttons.
If you have a look at the circuit
you will see that for each of the nine
locations, the common cathode of the
green/red LED and one side of the
pushbutton are all connected together.
So we have a common connection for
the button, red LED and green LED,
and of course there are nine separate
common points.
The PIC holds these lines high at 5V
and then, one at a time, drops the line
siliconchip.com.au
+
4.7k
OSCAR'S
FIRST TURN
RESET
10k
10 F
1
IC1 PIC16F84A
S2
4.7k
4.7k
100nF
0V
100pF
D1
+6V
S1
17001180
LED7
LED4
LED1
LED8
LED5
LED2
LED9
LED6
LED3
1CIP RACSO
S9
S6
S3
S10
S7
S4
S11
S8
S5
Fig.2: follow this parts layout diagram and the accompanying photo to assemble the unit. In the prototype, ordinary
copper wire was used for the links but we suggest tinned copper wire to prevent oxidation.
to 0V for around one millisecond, then
puts it back up to 5V and drops the
next one to zero for one millisecond
and so on. So the PIC is scanning from
one to nine, relentlessly, regardless of
the state of the game.
Let’s now look at pins 1 and 18 of
the PIC. These pins are configured as
outputs and are normally held low by
the PIC. Pin 1 connects to all the green
LED anodes and pin 18 connects to all
the red LED anodes.
If, during the scanning, the PIC
needs to illuminate, let’s say, green
LED number six, it waits until the
scanning reaches position number
six and then, just for one millisecond,
while the cathode is held low, it raises
the anode via pin 1 of the PIC to 5V.
Only that LED will light because it is
siliconchip.com.au
the only one with power on one end
and 0V on the other end.
In this way, the PIC lights the LEDs
one at a time at high speed, so you are
unaware that they are actually flashing
rapidly. It will never light both green
and red for the one location, as that
situation never occurs.
The common sides of the pushbuttons are all connected to pin 2 of the
PIC. This pin is configured as an input
and is normally held high by a 4.7kW
resistor. However, if you press a button, this pin will be pulled low when
the scanning reaches that position.
During an actual game, as the PIC
is scanning each position, then for
the one millisecond when the common point is pulled low, pin 1 will
be switched high if the LED should
be green. Similarly, if the LED should
be red, pin 18 will be switched high.
If the position is not occupied (no
red, no green), then (and only then) the
PIC looks at pin 2 to see if a button has
been pressed. This means that if the
player presses the button for a position
already occupied, it is ignored.
Software
Let’s ignore the housekeeping software and just look at the game logic
itself.
When you press a button, the green
LED is illuminated immediately, and
then there is a deliberate delay of
1-2 seconds so that OSCAR appears
to be “thinking”. Then the PIC very
rapidly goes through four separate
procedures, looking for a response. As
October 2007 29
These two shots give a good idea of how
OSCAR fits together. The PC board screws
to the box lid via standoffs with the
switches and LEDs poking through, while
the battery is fixed to the bottom of the box.
soon as a response is indicated,
the PIC executes it and quits
any further procedures until the
next move.
The first procedure, called
“Win for Sure” is to test every
position to see if in any row of
three LEDs, there are two red
LEDs lit and the third position
blank. If it finds one, it of course
puts a red LED there, stops the
game, declares a win, etc.
The second procedure is called
“Prevent Win” and is similar to the
first. Its job is to test every row to
see if there are two green LEDs in
a row and a third position blank. If
it finds one it puts a red LED there
to prevent defeat.
The third procedure is the most
difficult. It is called “Tactics”. It
goes through quite a few algorithms
and tries to do something intelligent.
The fourth procedure, if the first
three produce nothing, is simple:
just find an empty position and go
there. There is more software for
responding when the player lets
OSCAR have first turn and also to
highlight the winning row of three red
LEDs by making them flash.
Assembly
The whole circuit is built on one PC
board which mounts inside the lid of
a jiffy box. The most difficult part of
the construction process is the precise
drilling of the lid. Photocopy or cut
out the front panel art and use it as a
template. Tape it to the jiffy box lid
and drill a small pilot hole for each
marked spot. That done, increase the
size of the drill, being careful to keep
the drill perpendicular to the lid at
all times.
Check that the LEDs will fit into the
holes easily and that the buttons have
about 1mm clearance all around. If
the switches get caught on the hole
edges and jam on, the project won’t
work!
Assemble all the components onto
the board except the LEDs. This is
important – leave the LEDs until
later.
It’s best to use a socket for the PIC in
case you need to remove it. The end of
Parts List – OSCAR Noughts & Crosses Game
1
1
1
4
9
1
1
1
4
8
OSCAR PIC1 PC board, code 08110071, 145 x 86mm
158 x 95 x 53 mm (UB1) jiffy box (Jaycar HB6011)
4 x AA battery holder with switch (Jaycar PH-9282
AA batteries
green PC-mount pushbuttons (Jaycar SP- 0724)
red PC-mount pushbutton (Jaycar SP- 0720)
white PC-mount pushbutton (Jaycar SP- 0723)
18-pin IC socket
M3 x 10mm tapped metal stand-offs (Jaycar HP-0900)
M3 x 6mm screws
Semiconductors
1 PIC16F84A preprogrammed with OSCARv2.hex (IC1)
9 red/green 3-terminal LEDs (WES Components LED5GRY)
1 1N4004 silicon diode (D1)
Capacitors
1 10mF 16V electrolytic capacitor.
1 100nF miniature polyester capacitor (code 104, 0.1 or 100n)
1 100pF ceramic capacitor (code 101 or 100p)
Resistors (0.25W, 5%)
1 10kW
(code: brown black orange gold)
3 4.7kW
(code: yellow purple red gold)
30 Silicon Chip
siliconchip.com.au
0v
OSCAR’S FIRST TURN
+6v
R
A
C
O
OSCAR PIC1
S
5677s
+6v
RESET
Figs.3 & 4: full-size artwork for the front panel (which can also be used as a drilling template) and the PC board, viewed
from the copper side. Note the four cutouts required in the corners of the PC board so it can clear the pillars in the box.
the socket with the notch in it is near
the edge of the board. Leave the PIC
itself out for the moment.
Fit all 18 wire links on the PC
board (we suggest using resistor and
capacitor lead cut-offs as these are
invariably tinned copper wire) and
the four resistors.
The small electrolytic capacitor
is polarised. Install the four 10mm
metal standoffs to the PC board. Test
fit the board to see how well you have
drilled the holes for the buttons; file
or ream the holes a little if necessary.
When you are happy with the fit of the
buttons in the holes, fit all nine LEDs
into the holes in the board, taking
great with the polarity (flat side is on
the anode 1 [red] leg) but don’t solder
them just yet.
Now mount the board on the lid
using the standoffs and fit the screws
siliconchip.com.au
to both ends, so that the board is in
its correct position. That done, push
the LEDs one at a time hard into their
corresponding holes in the lid. Make
sure each LED is fully pressed into its
hole, then solder its leads. Repeat until
all the LEDs have been soldered in.
If you have done this well, all the
LEDs will be protruding through the
lid by the same amount (around 3mm).
The battery box can be attached to
the bottom of the jiffy box with double
sided tape. Finally, fit the PIC into its
socket, install the batteries, switch on
and give it a go.
Faultfinding
If you have any trouble with any of
the functions you can check out the
board as follows.
First power off and remove the
PIC, then make a short jumper out of
a single strand of telephone wire. Put
one end in pin 14 of the socket and
the other end in pin 18.
Put one end of a 220W resistor in pin
5 and the other into pin 6. The first LED
should light red. Swap the resistor end
from pin 6 to pin 7 and the second LED
should light red. Keep going into pins
8, 9, 10, 11, 12, 13 & 17 and each LED
should light red in turn.
To check the green LEDs, change the
jumper linking pins 14 & 8 to pins 14
& 1. Repeat as above with the 220W
resistor and again, each LED should
light green in turn.
If you have a LED in backwards it
will light green when it should have
lit red and vice versa.
If these checks are OK, then it is
highly likely that you have a faulty PIC
or a PIC that has not been programmed
SC
or is programmed incorrectly.
October 2007 31
|