AVR Tutorials
The Best AVR Microcontroller Tutorials on the Web

ATMega16 AVR Microcontroller Seven Segment Single Digit Digital Dice

The ATMega16 Single Digit Digital Dice

In this ATMega16 AVR project we will be designing and implementing a single digit digital dice with the aid of a ATMega16 microcontroller and seven segment display.

Before going through this digital dice AVR project it is recommended to complete the tutorial on Interfacing a Seven Segment Display with the AVR Microcontroller.

After completing this project you can try the ATMega8515 AVR Microcontroller Seven Segment Double Digit Digital Dice AVR project which is a bit more advanced.

Note: Although this digital dice was implemented with the ATMega16 AVR microcontroller other microcontrollers including the ATMega8515, ATMega32, ATTiny2313, etc. could have been used.

ATMega16 Seven Segment Digital Dice Operation
Our digital dice will operate similar to that of a normal dice. When the button is pressed in the circuit below a roll will be simulated by displaying a sequence of number from 1- 6 then a number randomly obtained in the sequence would then be displayed on the seven segment display. This operation would repeat whenever the button is pressed. See the video of the digital dice operation at the end of this page.

Hardware Description for the ATMega16 Digital Dice
The figure below gives the schematic diagram for the digital dice. The seven segment display use in this design is the LDS-C30RI. For connection information see the LDS-C305RI DataSheet

ATMega16 AVR Microcontroller Digital Dice Circuit

Important Note: In order for our digital dice to work correctly the internal oscillator of the ATMega16 microcontroller must be enabled and set to 4MHz.

Software for the Digital Dice
Below is the C implementation of the entire code for the AtMega16 seven segment single digit digital dice. The code was implemented and built using AVR Studio 5. Be reminded that the internal clock of the ATMega16 microcontroller should be enabled and programmed to operate at 4MHz for the C code to operate correctly. A video of the digital dice in operation is presented at the end of this page.

The image/table below shows which LEDs in the seven segment to turn ON to display the digits 1 - 6. This information was used in implementing the digit_to_7segval() and roll_dice() functions of our digital dice.

Please see the Interfacing a Seven Segment Display with the AVR Microcontroller tutorial for more information on the seven segment display.

The AVR C Program for the ATMega16 Seven Segment Digital Dice written in AVR Studio 5

 * DigitalDice_SingleDigit.c
 * Written in AVR Studio 5
 * Compiler: AVR GNU C Compiler (GCC)
 * Created: 5/27/2011 10:50:48 PM
 * Author: AVR Tutorials
 * Website: www.AVR-Tutorials.com
 * Description: AVR C program for ATMega16 Seven Segment 
 * Single Digit Digital Dice Project
#define DicePort		PORTA
#define DicePortDDR		DDRA
#define SwitchPortDDR		DDRD
#define SwitchPort		PORTD
#define SwitchPin		PIND
#define RollSwitch		7
#define F_CPU			4000000UL
#include <avr/io.h>
#include <stdlib.h>
#include <util/delay.h>
#include <avr/interrupt.h>
unsigned char seed = 0;
/*Declaration of functions. The implementation of these */
/*function are done below the main() function*/
unsigned char digit_to_7segval(unsigned char digit);
void roll_dice(unsigned char times);
void init();
int main(void)
	unsigned char digit = 0;
	init();	// Configure the Microcontroller
    /*Do forever*/
		if(!(SwitchPin & (1<<RollSwitch)))
			/*Roll the dice 2 times*/
			/*Generate an integer based random number between */
			/*1 and 6 inclusive and store it in digit*/
			seed = (seed*17)+21;
			digit = seed % 6;
			/*Get the Seven Segment value of the random number*/
			/*and output it to the pins of port B*/	
			DicePort = digit_to_7segval(digit); 
/*Configuration for the microcontroller*/
void init()
	/*Configure DicePort as an output port*/
	DicePortDDR = 0xFF;
	/*Configure SwitchPort as an input port*/
	SwitchPortDDR = 0x00;
	/*Enable SwitchPort pull-up resistors*/
	SwitchPort = 0xFF;
	TCCR0 = 0x01;   // Enable Timer0 with no prescalar
	TIMSK = 0x01;   // Enable Timer0 interrupt
	sei();			// Enable Global Interrupt
/*This function takes an character value and return the */
/*and return the value to be outputted to display that */
/*character on a seven segment display*/
unsigned char digit_to_7segval(unsigned char digit)
	unsigned char segval = '0';
	if(digit == 1)
		segval = 0x06;		// 7-Seg Value for Digit 1
	else if (digit == 2)
		segval = 0x5B;		// 7-Seg Value for Digit 2
	else if (digit == 3)
		segval = 0x4F;		// 7-Seg Value for Digit 3
	else if (digit == 4)
		segval = 0x66;		// 7-Seg Value for Digit 4
	else if (digit == 5)
		segval = 0x6D;		// 7-Seg Value for Digit 5
	else if (digit == 6)
		segval = 0x7D;		// 7-Seg Value for Digit 6
	return segval;
/*This function simulates the rolling of the dice*/
void roll_dice(unsigned char times)
	unsigned char i;
	for(i=0; i<times; i++)
		DicePort = 0x06;	// Display #1 on dice
		DicePort = 0x5B;	// Display #2 on dice
		DicePort = 0x4F;	// Display #3 on dice
		DicePort = 0x66;	// Display #4 on dice
		DicePort = 0x6D;	// Display #5 on dice
		DicePort = 0x7D;	// Display #6 on dice
/*Interrupt Service Routine for timer overflow*/
	seed++; // Increment the value in the variable seed

Video of the ATMega16 Digital Dice in Operation

AVR Tutorials hopes this AVR project on the ATMega16 digital dice was benificial to you and looks forward to your continued visits for all your AVR microcontroller projects needs.