Path:AVR-EN => Applications => double dice tn24
Double dice tn24 Application of
AVR-Single-Chip-Controller AT90S, ATtiny, ATmega and ATxmega
Double dice with ATtiny24 and 7-segment display
Double dice complete

Double dice with ATtiny24

  1. Properties
  2. Hardware
  3. Mounting
  4. Software

1 Properties

The double dice has the following properties:

Top of page Properties Hardware Mounting Software

2 Hardware

The hardware centers around the 14-pin ATtiny24. It's eight portpins of port A control the seven segments of the display and the decimal point LEDs. Current through the segments and the decimal point LED is limited by 47Ω resp. 68Ω resistors to 27 resp. 19 mA. The LEDs are on with an active low pin.

Portpins PB1 (left display) and PB0 (right display) control the common anode voltages of the displays by multiplexing the two PNP transistor drivers: portpin low switches the transistor on and the collector provides current to the display.

Dicing is performed with a key on the INT0 input, that has an active pull-up resistor switched on by software. The key is actively debounced by software.

The schematic is shown for an operating voltage of 5 V, the 3 V version has the above mentioned smaller current-limiting resistors.

Dice 2 schematic

Top of page Properties Hardware Mounting Software

3 Mounting

The components are placed on a 50-by-80 mm single-sided PCB.

Dice 2 pcb layout Dice 2 component placement

Two bridges have to be soldered. Short-circuit protection is provided by a plate of acrylic glass mounted underneath the PCB with four 2.0 or 2.5 mm screws and 5 mm plastic spacers, living room table surface protection with four adhesive plastic feets. This is it and makes it a nice birthday or christmas gift.

Top of page Properties Hardware Mounting Software

4 Software

4.1 Source code

The software for the double dice in
.asm format here or as or as html format here. Assemble the source code either with ATMEL®'s Studio or with my assembler gavrasm. If you do not know how to assemble, either consult the Windows how-to-assemble page or the Linux how-to-assemble page. Before assembling please check in line 9 that your device is an ATtiny24. If you built in an ATtiny44 or 84 change this line accordingly. The software works correctly for all those three devices without further changes.

4.2 Programming the device

With your favored programming device write the assembled hex code in the file "dice2_tn24_v1.hex" to the device via the 6-pin ISP plug on the PCB.

Now you should alter the fuse EESAVE in the device to on, which protects the EEPROM content (otherwise every following programming erases the EEPROM to hexadecimal 0xFF, by that setting the EEPROM counters to their maximum of 0xFFFFFF and the counters do not work properly). Do not change the clock fuses, the software works fine with the default RC clock and CLKDIV8 fuse setting.

Finally you should program the EEPROM with the content of the file "dice2_tn24_v1.eep", which clears the counters in the EEPROM. Whenever you think that the counters might overflow, which is the case after eight years of dicing every 5 seconds for eight hours per day, you can clear them by re-programming the eep file.

4.3 Analyzing the counters

To analyze the counters follow those steps:
  1. Read the EEPROM content to a file using programming software via the ISP6 plug. Name the file something.hex.
  2. Either download the executable for Windows here and unzip it or download the Lazarus source files here and compile the dice.lpi with Lazarus.
  3. Start the dice analysis software, click into the "Select EEP file" edit field and locate your something.hex.
The program should now list your counters like this:

Dice analysis The left window shows the numbers in your EEPROM in hexadecimal format for each of the 36 double dice combinations (LSB first, MSB next, HSB last).

To the right, the double dice combinations between 1-1 and 6-6 are listed in decimal form. The lower window sums up all diced single numbers 1 to 6 and adds the percentage of occurence. This should be roughly 100 / 6 or 16.67% for each single dice. The more you dice the nearer you come to that. Try it out. Good luck.

4.4 How the software works

Flow main init The software first inits the stack pointer and all output pins.

Then the key input pin on the INT0 input is configured (pull-up resistor switched on). Following that 108 bytes are read from the EEPROM to the SRAM of the device: counting of dices is done in the SRAM, and after each dicing the counter is written to EEPROM.

Then the counters TC1 and TC0 are initiated. 16-bit TC1 is the counter for dice 2, 8-bit TC0 for dice 1. TC0 is also controlling the display multiplexing.

Then the sleep mode idle and interrupts are enabled and the device goes to sleep.

Flow INT0 Activities start with an active key input: INT0 is enabled on every edge. First it checks if the register rBounce is cleared. If not, the decimal point of display 1 is switched on.

If rBounce is clear, it is checked if the key input pin is low. If that is the case (the key is pressed), the T flag in the status register is set, signalling the next dice (which is processed later on in the loop section). And the rBounce register is set to prevent any re-dicing for a pre-selectable time.

Flow loop Following any interrupt the controller wakes up and executes the instruction following the sleep instruction. If the T flag is clear, no dicing is active and the controller goes back to sleep.

If the T flag is set, it is cleared and the blank counter for the 10 second period is set. The two dices, that are increased by the TC0 and TC1 timers, are copied, converted from zero to five to the 7-segment codes between 1 and 6 and written to the display registers.

The counter in the SRAM is located (address = (6 * dice1 + dice2) * 3), increased by one and the highest changed byte of the counter is written to the EEPROM. Further writes are initiated by setting the interrupt enable bit.

Flow EEPROM write The EEPROM ready interrupt first decreases the counter rEep. If that is zero, the EEPROM interrupt enable bit is cleared and no further interrupts occur.

If there are still bytes to write, the next previous counter byte is written to the EEPROM.

Flow TC0 The TC0 interrupt occurs on compare match A and clears the counter TC0. The counter is prescaled by 64 and the CTC divides the prescaled clock input by 128, resulting in 122 Hz for the multiplexing of the display.

The interrupt service routine first determines if the current display is the first. If yes the second is displayed next, if not the first display comes next. Display change first switches off the anode driver, then writes the display's register value to the output port and then switches the other output driver on.

Next the dice counter is increased. If it is higher than five, it is cleared again.

Third comes the bounce protection: if rBounce is larger than zero, it is decreased.

Flow TC1 TC1 works with a prescaler of eight and divides this by 133 (you can use any number which is not too large). It clears on compare match A and initiates an interrupt.

It increases the second dice counter, which is cleared when it reaches six. Then it decreases the blank counter. If this reaches zero, the displays are cleared (by writing 0xFF to them) and the decimal point of the second display is switched on (by writing 0x7F to it).

Top of page Properties Hardware Mounting Software

Praise, dispraise, error reports, criticaster, scolding and unqualified spam please via the comments page to me.

©2017 by