AVR Tutorials
The Best AVR Microcontroller Tutorials on the Web

ATMega8515 AVR Microcontroller Seven Segment Double Digit Digital Dice

The ATMega8515 Double Digit Digital Dice


In this ATMega8515 AVR project we will be designing and implementing a double digit digital dice with the aid of a ATMega8515 microcontroller and seven segment displays.


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. You can also look at the ATMega16 AVR Microcontroller Seven Segment Single Digit Digital Dice AVR project.


Note: Although this AVR project was designed around the ATMega8515 the project could have utilized another microcontroller such as an ATMega32, ATMega16, ATTiny2313 etc.


ATMega8515 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 numbers randomly obtained in the sequence would then be displayed on the seven segment displays. 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 ATMega8515 Digital Dice
The figure below gives the schematic diagram for the digital dice. One feature to note in that both seven segment displays are driven by the same port (PortC). The microcontroller through controls from PortE indicate which seven segment display each digit is displayed on. Note that these Seven Segment Displays are common cathode display.

ATMega8515 Digital Dice

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

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

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

Below is the AVR C code for the ATMega8515 Based Double Digit Digital Dice. This code was written in AVR Studio 5.

/*
 * DigitalDice_DoubleDigit.asm
 * Written in AVR Studio 5
 * Compiler: AVR GNU C Compiler (GCC)
 *
 * Created: 10/24/2011 4:34:14 PM
 * Author: AVR Tutorials
 * Website: www.AVR-Tutorials.com
 *
 * Description: AVR C program for ATMega8515 Seven Segment double digit 
 * digital dice
 */ 
 
#define DicePort	PORTC
#define DicePortDDR	DDRC
 
#define CntrlPortDDR	DDRE
#define CntrlPort	PORTE
#define CntrlPin	PINE
 
#define Dice1CntrlPin	0
#define Dice2CntrlPin	1
#define RollCntrlPin	2
 
 
#define F_CPU		4000000UL
 
#include <avr/io.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();
ISR(TIMER0_OVF_vect);
 
 
int main(void)
{	
	unsigned char digit = 6;
	unsigned char digit2 = 6;
 
	init();	// Configure the Microcontroller
 
    /*Do forever*/
	while(1)
	{
 
		if(!(CntrlPin & (1<<RollCntrlPin)))
		{
			/*Roll the dice 2 times*/
			CntrlPort = ~(1<<Dice1CntrlPin | 1<<Dice2CntrlPin);
			roll_dice(2);
 
			/*Generate two integer based random number between */
			/*1 and 6 inclusive and store it in digit & digit2 */
			seed = (seed*17)+21;
			digit = seed % 6;
			digit++;
 
			seed = (seed*17);
			digit2 = seed % 6;
			digit2++; 
		}
 
		/*Get the Seven Segment values of the random numbers*/
		/*and output it to the pins of port B*/
		CntrlPort = ~(1<<Dice1CntrlPin);
		DicePort = digit_to_7segval(digit);
		_delay_ms(10);
		CntrlPort = ~(1<<Dice2CntrlPin);
		DicePort = digit_to_7segval(digit2);
		_delay_ms(10);	
	}		
 
}
 
/*Configuration for the microcontroller*/
void init()
{
	/*Configure DicePort as an output port*/
	DicePortDDR = 0xFF;
 
	/*Configure CntrlPort as an input port*/
	CntrlPortDDR = ~(1<<RollCntrlPin);
	/*Enable RollCntrlPin pull-up resistors*/
	CntrlPort = 1<<RollCntrlPin;
 
	TCCR0 = 1<<CS00;   // Enable Timer0 with no prescalar
	TIMSK = 1<<TOIE0;   // 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 dice*/
void roll_dice(unsigned char times)
{
	unsigned char i;
 
	for(i=0; i<times; i++)
	{
		DicePort = 0x06;		// Display #1 on dice 
		_delay_ms(200);
		DicePort = 0x5B;		// Display #2 on dice 
		_delay_ms(200);
		DicePort = 0x4F;		// Display #3 on dice 
		_delay_ms(200);
		DicePort = 0x66;		// Display #4 on dice 
		_delay_ms(200);
		DicePort = 0x6D;		// Display #5 on dice 
		_delay_ms(200);
		DicePort = 0x7D;		// Display #6 on dice 
		_delay_ms(200);
 
	}		
}
 
/*Interrupt Service Routine for timer overflow*/
ISR(TIMER0_OVF_vect)
{
	seed++; // Increment the value in the variable seed
}		


Video of the ATMega16 Digital Dice in Operation



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