Electronic DiceDonald Pratt
|
||||||||||||||||||||||||||||||||
![]() |
Figure 1 |
A number of optional features of the SimmStick were not used for this project. The only standard parts used were:
To this, I added
The schematic for the electronic dice is shown in figure 2. LEDs 1 to 7 are arranged in the pattern shown in figure 3. LEDs 8 to 14 are arranged similarly. Shift register U3 provides up to 8 tri-state outputs from 4 output pins of the PIC. The 74HC595 provides a Q'H pin which can feed the serial input pin of another 74HC595. This allows any number of chips to be chained to provide as many outputs as required. For this project, 2 shift registers were used, providing up to 16 outputs. As appendix A demonstrates, as few as 2 pins could be used to drive the shift registers from the PIC.
![]() |
|||||||||||||||||||||||
Figure 2
|
|||||||||||||||||||||||
|
|
||||||||||||||||||||||
Source code is provided in ss_dice.asm. On startup, after initializing variables, the CPU enters an endless loop where it repeatedly counts down from 36 to 1. This count provides a pseudo random number that is used to determine what pair of dice to display. While it isn't really a random number, the count cycles rapidly enough that it is impossible to predict what number will be the next to be displayed when the roll button is pressed.
When a player pushes the roll button, the interrupt handler sets the trigger
flag. To debounce the button, the interrupt handler spins in a loop 50 times. This
provides enough time for the button to settle before the value is read. At the end of the
loop, if the button is in a pushed state (PORTB<7> is low), the flag is set.
Interrupts are reenabled, and processing returns to the main loop.
When the main loop sees the flag, it begins the display process. To give the effect of rolling dice, the display is updated 30 times, each time with a different pair of dice. After 30 patterns have been displayed, the final pattern is left on the display. After a couple seconds, the display is cleared to conserve power.
The display routine calls 3 helper routines - diepat1, diepat2, and
disp_ch. Diepat1 and diepat2 determine what pair of dice
to display for each counter value using lookup tables. Once a die pattern has been
selected, disp_ch sends that pattern to the LEDs.
Disp_ch demonstrates how to interface to the 74HC595 shift registers in
order to drive all 14 LEDs from only 4 pins on the PIC. A loop is entered, and the bits of
dspch are rotated one by one through the carry flag. PORTA<0> is the
serial output to the shift register. After setting PORTA<0> appropriately,
PORTA<1> is toggled to clock the data into the shift register. After all 8 bits have
been sent in this fashion, PORTA<2> is toggled to move the data from the 74HC595's
shift register to the storage register. After both die patterns have been sent to the
display, PORTA<3> is brought low to enable the outputs of the 74HC595.
The SimmStick platform is ideal for quickly prototyping a project such as this one. By providing the circuits that are common to many PIC microcontroller projects, the designer can focus on the parts of their project that are unique. While the PIC 16F84 is somewhat limited in the area of I/O pins, this project demonstrated how that could be overcome through the use of shift registers to provide 8, 16, or more binary outputs from as few as 2 PIC I/O pins. In the process, a pair of electronic dice were implemented that can be used for a variety of games.
It is possible to reduce the number of output pins that are needed from 4 to 2. The modified circuit is shown in figure A1. Source code to drive this circuit is provided in ss_dice2.asm. The changes from the previous circuit are:
![]() |
Figure A1 |
When SCK and RCK are tied together, the storage register will always be one bit behind
the shift register. To compensate for this a new routine, send0, was added
that simply sends a 0 bit to the shift register. This routine clears PORTA<0> and
then toggles PORTA<1>. Clearing PORTA<0> is really unnecessary, since it
doesn't get shifted into the storage register.
By tying G (output enable) to ground, the outputs of the shift registers are always
enabled. If the outputs were feeding into other logic circuits, this could cause problems.
For this application, it isn't an issue. The main drawback comes in clearing the display.
The 4 wire interface could clear the display by simply disabling the shift register
outputs. The 2 wire interface requires that we send 16 off bits to the shift register, as
seen in the clr_dsp routine.