Electronic Dice using 8051

 

Description

It’s an Electronic Dice Game using 8051 microcontroller (Have a Peek Here). It is an interesting and basic learning project for student and hobbies. It describes how can we interface 8051 port pins for input and output.

Diagram

Detail

It is a project that builds microcontroller programming interest in new student and hobbies. This project is a very simple one that can easily be built by new students. It uses 7 LEDs, 8051 microcontroller and an optional 5V power supply regulator part. It’s algorithm is also very simple.

1) Initially microcontroller displays 1 on LEDs display just like in real dice.

2) Then in main while loop it waits for switch attached to pin 0 of port 1 to be pressed.

3) Until switch remains pressed it generates random number from 1 to 6 and displays it on LEDs.

4) When button is released it generates and display a new random number on LEDs.

5) It repeats from step 2 to step 5 until power is applied to the system.

For further information of components and basic 8051 microcontroller circuit please visit
8051 Basic LED Flasher.

Code

// Start of code.

///////////////////////////////////////////////////////////
// Company         : Micro Digital                       //
// Address         : Office # C7 Raza Plaza              //
//                   DAV College Road                    //
//                   Rawalpindi,                         //
//                   Pakistan.                           //
// Programmed By   : Rashid Mehmood                      //
// Project Name    : Electronic Dice using 8051          //
// Crystal         : 24.000000 MHz                       //
// Microcontroller : AT89C2051-C51-C52-C55-S2051-S51-S52 //
///////////////////////////////////////////////////////////

// Header file for AT89x051 microcontrollers regiter definitions.
#include <AT89x051.h>
// Header file for random number generation functions.
#include <stdlib.h>

sbit Led1 = P1^1;
sbit Led2 = P1^2;
sbit Led3 = P1^3;
sbit Led4 = P1^4;
sbit Led5 = P1^5;
sbit Led6 = P1^6;
sbit Led7 = P1^7;

// This function halts execution for
// specified milliseconds.
void delay_ms(unsigned int del)
{
	unsigned int i,j;
	for(i = 0; i < del; i ++)
		for(j = 0; j < 1275; j ++);
}

// Rename or define P1.0 as Switch.
sbit Switch = P1^0;

// This function displays an integer value from
// 0 to 6 on LEDs.
void Display(char Value)
{
	// Switch off all LEDs.
	Led1 = Led2 = Led3 = Led4 = Led5 = Led6 = Led7 = 1;
	switch(Value)
	{
	case 1:
		Led4 = 0;
		break;
	case 2:
		Led1 = Led7 = 0;
		break;
	case 3:
		Led1 = Led4 = Led7 = 0;
		break;
	case 4:
		Led1 = Led3 = Led5 = Led7 = 0;
		break;
	case 5:
		Led1 = Led3 = Led4 = Led5 = Led7 = 0;
		break;
	case 6:
		Led1 = Led2 = Led3 = Led5 = Led6 = Led7 = 0;
		break;
	}
}

// Define macro or formulae for random number
// generation from 1 to 6.
#define GetRandomNumber() ((rand() % 6) + 1)

void main()
{
	char RandomNumber;
	Switch = 1;
	Display(1);
	srand(50);
	while(1)
	{
		// Wait until switch is pressed.
		while(Switch);
		// Until switch is released.
		while(!Switch)
		{
			// Generate random number.
			RandomNumber = GetRandomNumber();
			// Display this number on LEDs.
			Display(RandomNumber);
			// Give some delay.
			delay_ms(10);
		}
		// Generate random number last time.
		RandomNumber = GetRandomNumber();
		// Finally display this number on LEDs.
		Display(RandomNumber);
	}
}

// End of code.

This code can also be executed on AT89C2051 microcontroller a small brother of AT89C51.

 

Related Projects

8051 Basic LED Flasher

2-Way Traffic Signal Controller

8051 To Seven Segment Display Interfacing

More Projects