Electronic Dice

Donald Pratt
Ramona, CA, USA

Entry SSUS0004

Abstract

This goal of this project was to design and implement an electronic pair of dice. When a player presses the roll button, a pseudo-random number is generated to determine the results of the roll. The dice that were selected are then displayed on the LEDs. This project demonstrates:

  • Using a SimmStick board
  • Debouncing an input
  • Using shift registers as I/O expanders

Description

The SimmStick platform provides an easy to use microcontroller system. The basics of power supply, crystal, and I/O connections are already provided in one small package, allowing the designer to focus on application-specific issues. For this project, I chose the DT111 board. This board provides the same basic features as the DT101 PIC-based board, and includes an additional 1" (9 by 34 holes) prototyping area. This allowed me to fit the entire project on a single 2" by 3.5" board. The completed project is shown in figure 1.

Picture.jpg (23483 bytes)

Figure 1

A number of optional features of the SimmStick were not used for this project. The only standard parts used were:

  • PIC 16F84 microcontroller
  • 4MHz ceramic oscillator
  • 78L05 voltage regulator
  • resistors R1-R3 (required  for proper operation of the PIC)
  • capacitors C1 and C2 (required by the voltage regulator)

To this, I added

  • 2 74HC595 serial-in/parallel-out shift registers
  • 14 LEDs
  • 2 330 ohm x7 resistor networks
  • 1 temporary push-button switch

Circuit Design

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.

Digital dice schematic

Figure 2

 

R1-R3 10 K ohm, 1/4 watt resistor
RN1-RN2 330 ohm 7 element resistor network
C1-C2 0.1 uF ceramic capacitor
X1 4MHz ceramic resonator
D1-D14 Mini super-bright LED
U1 PIC 16F84 microcontroller
U2 78L05 +5V voltage regulator
U3-U4 74HC595 shift register with output latches
S1 NO temporary push button switch
B1 9V battery
Layout of the 7 LEDs in each die

Figure 3

Program Operation

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.

Conclusion

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.

Appendix A - 2 wire shift register interface

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:

  • Pins 11 and 12 (SCK and RCK) of the shift registers are tied together
  • Pin 13 (G) of the shift registers are tied to ground
2 wire interface schematic

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.