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

8051 To 7 Segment Display Interfacing

Description

It’s an example project of 7 segment display interfacing to microcontroller 8051.

Diagram

Detail

This project describes how can we connect 7 segment display to 8051 microcontroller. Similarly it can be connected to any other microcontroller like PIC or AVR with modifications in code. Counting from 0 to 9 is displayed on it after some delay. This project also describes how to use 8051 port pins for output and LED drive.

How 7 Segment Works

7 segment display consists of 7 segments and a dot. Actually each segment and dot are LEDs (Light Emitting Diodes). Each segment including dot are total 8 LEDs.

7-Segment-88.gif
Click to Enlarge

Light Emitting Diode like an ordinary diode passes current in one direction (forward biased) and block in other (reverse biased) direction. Each LED has 2 pins one for anode and other for cathode. In common anode all anode terminals are connected together to form a common connection and remaining 8 cathodes are provided for negative side supply of individual LED. We connect common wire with positive supply and provide negative supply to appropriate LED segments to switch on and make a specific pattern on the whole display. In common cathode display all pins are opposite to common anode. All cathode terminals are connected to form a common connection and all other 8 pins are anodes. Pin diagram of commonly used seven segment display are given in above figure.

Code

///////////////////////////////////////////////////////////
// Company         : Micro Digital                       //
//                   DAV College Road                    //
//                   Rawalpindi,                         //
//                   Pakistan.                           //
// Programmed By   : Rashid Mehmood                      //
// Project Name    : 8051 To Seven Segment Display       //
// Crystal         : 24.000000 MHz                       //
// Microcontroller : AT89C2051-C51-C52-C55-S2051-S51-S52 //
///////////////////////////////////////////////////////////

// Start of code.

// Header file for AT89x051 microcontrollers.
#include <AT89x051.h>


#define SSeg P1

code const unsigned char RealDigit[10] = {
						0x40, 0x79, 0x24,
						0x30, 0x19, 0x12,
						0x02, 0x78, 0x00,
						0x10
					 };

void  delay(unsigned int del)
{
	// Wait until del is greater zero.
	while(del --);
}

// Displays value (0 to 9) on seven
// segment display.
void Display(unsigned char Value)
{
	if(Value < 10)
		SSeg = RealDigit[Value];
}

void main()
{
	unsigned char Value = 0;
	while(1)
	{
		for(Value = 0; Value < 10; Value ++)
		{
			Display(Value);
			delay(30000);	// Calibrated delay.
		}
	}
}
// End of code.

Related Projects

8051 Basic LED Flasher

Electronic Dice using 8051

2-Way Traffic Signal Controller

Cricket Score Board using 8051 and 7 Segment Display

8051 Timer0 As Second Counter On 7 Segment

8051 Digital Clock On 7 Segment (Non Multiplexed)

More Projects

555 Timer Oscillator/Led Flasher

Description

It’s a 555 IC based LED flasher that is very useful for beginners. It demonstrates basic operation of 555 timer IC as Astable Multivibrator.

Diagram

Detail

An oscillator is an important and essential part of many of the electronic circuits. It function in electronic circuit is as our heart function in our body. It pumps electronic parts pulses and makes them functional. So many electronic parts work on time basis. These perform different piece of work at different time such as timers shift registers and sequential logic ICs like CD4017. These all give different response or result on coming next pulse from oscillator. In other words oscillator instructs these ICs to perform their next function or operation. Counters increment on pulses and give different result on each pulse, shift registers shift internally loaded data on each pulse etc. So an oscillator or timer has very important role in our electronic circuits.
In above circuit diagram 555 IC with some additional parts work as an oscillator. It is also called an astable multivibrator. It outputs on/off(1 or 0 logic) on it’s pin 3. To describe it’s functionality first we see it’s internal diagram

555-Timer-Flasher-Osc-2.gif
Click to Enlarge

Internal circuit of 555 IC consists of 2 comparators, 3 resistors of same value, one transistor for discharging of capacitor C connected outside, 2 transistors for output stage and 1 flip flop. network of three resistors form a voltage divider and using this scheme 1/3 of the supply voltage are feed to 2nd op-amp at non-inverted (+ve) input for comparison reference voltage.




8051 Basic Led Flasher

Description

It’s an 8051 based basic LED flasher that is very useful for beginners. It describes parts, connections between them and programming environment for 8051 micro-controller.

Diagram

Detail

It provides foundation for microcontroller based application development for students, hobbies and beginners. It provides basics of 8051, minimum parts required, connection between them and programming environment for 8051 microcontroller.
Pin 20, 40 are power pins of microcontroller. Pin 40 is connected to 5V and pin 20 to ground. Pin EA (External Access) is connected to 5V. If we connect EA to 5V(logic 1) then it means code burnt inside microcontroller chip will be executed and if we connect it to ground(logic 0) then code inside an external RAM connected to microcontroller will be executed. Now we examine this circuit in parts.

1-Reset

8051-Basic-LED-Flasher-2.jpg
Click to Enlarge

When we power up our circuit then power and oscillator parts take some time to be stabilized. During this time we have to wait before starting program execution. If we does not wait and start program execution immediately then then there may be undesirable results. Suppose we have performed time calculation for some purposes then microcontroller may calculate wrong time value. Microcontroller manufacturers provide a reset pin that is used to accomplish this task with some extra components. To introduce delay we use circuit given in above figure. It is an RC oscillator that provides some delay according resistor-capacitor values used. In standard 8051 microcontroller pin 9 is used for reset. Capacitor C1 takes some time to charge up through R1 to a certain level. Before reaching this level pin 9 remains in reset state (logic 0) and program execution is not started. When this capacitor is charged to logic 1 level then program execution starts.

2-Oscillator

8051-Basic-LED-Flasher-3.jpg
Click to Enlarge

Usually microcontroller oscillator frequency selection option is given by the manufacturer to the circuit designer. Designer can select different frequencies. In ATmel’s 8051 microcontroller we can use this option as shown in above figure. This circuit uses crystal XT and 2 capacitors Cx1, Cx2 with internal oscillator of microcontroller to complete it. Different microcontrollers use different operating frequency range. Please consult datasheet of specific microcontroller for this purpose. ATmel’s AT89C51, AT89C52, AT89C2051 use maximum 24 Mhz, AT89S51, AT89S52, AT89C55 use maximum 33 MHz.

Code

// Start of code.

// Header file for AT89x051 microcontrollers.
#include <AT89x051.h>


// Delay routine.
void delay(unsigned int del)
{
	while(del --);
}

// Main routine.
void main()
{
	while(1)
	{
		P1_0 = 0;	// Turn off pin 0 of port 1.
		delay(30000);	// Give some delay.
		P1_0 = 1;	// Turn on pin 0 of port 1.
		delay(30000);	// Give some delay.
	}
}

// End of code.

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

Related Projects

Electronic Dice using 8051

2-Way Traffic Signal Controller

8051 To Seven Segment Display Interfacing

More Projects

LM78xx Regulated Power Supply

Description

It is very useful regulated power supply It is based on 78xx series ICs. We can use it for different output regulated voltages by choosing different IC numbers.

Diagram

Detail

It is 78xx IC series based regulated DC supply. We can make different voltage level supplies by choosing different 78xx IC series number. For example we can choose

1) 7805 for 5V output.
2) 7808 for 8V output.
3) 7809 for 9V output.
4) 7812 for 12V output.

Vin is high level DC voltage that is provided through D1 to 78xx series IC for regulation. Diode D1 is used for blocking reverse voltage polarity applied by mistake and saves whole circuit from reverse polarity. capacitor C1-C4 are used for filtration purposes. LED D2 is an on/off indicator and resistance R1 limits the current through it. R1 can be changed according to input voltage. Diode D3 is used here to protect 78xx IC from reverse voltage across it’s output due to C4 when power is removed and also stops overcharging of capacitors C3, C4. At output pin 3 of 78xx we can get regulated DC supply that is at low level than input supply at pin 1 of 78xx. For better circuit performance and protection of 78xx IC input supply should be greater than output by 3V to 5V range. More voltage input than output will cause the 78xx IC to be hot and can damage the IC permanently. Because there will be more voltage drop across 78xx IC and so more wattage will be dissipated. Use heat sink for better performance of IC when there is more current requirement at output.

Full Wave Bridge Rectifier Supply

Description

It is 220 volt AC to 12 volt DC full wave rectifier supply.

Diagram

Detail

It uses 4 diode to rectify AC voltage. We can understand this circuit by breaking down in parts as follows

Full-Wave-Bridge-Rectifier-Supply-4.jpg
Click to Enlarge

This is the part that converts AC voltage into pulsating DC by using D1-D4.

Full-Wave-Bridge-Rectifier-Supply-2.jpg
Click to Enlarge

In this part diode we exclude D2 and D3. Now when transformer output 1 will be positive and output 2 negative during sin wave pulse then diodes D1 and D4 conduct as these are forward biased and positive side is available at D1’s output(labeled as 2) and negative side is available at D4’s output(labeled as 4). When negative voltage is present at output 1 of transformer and positive at output 2 now these diodes will be reversed biased and so will not conduct. And there will be no output due to D1 and D4.

Full-Wave-Bridge-Rectifier-Supply-3.jpg
Click to Enlarge

Now we exclude D1, D4 and consider D2, D3 in the circuit. In this way when transformer output 1 will be positive and output 2 negative during sin wave pulse then diodes D2 and D3 will not conduct as these are reversed biased and there will be no output due to these diodes. When negative voltage is present at transformer’s output 1 and positive at output 2 then both diodes D2, D3 will conduct(as these are forward biased) and positive voltage will be available at D2’s output(labeled as 2) and negative voltage will be available at D3’s output(labeled as 4).

Conclusion is that

1) when transformer outputs 1 and 2 go +ve and -ve respectively then diodes pair D1, D2 conducts.
2) when transformer outputs 1 and 2 go -ve and +ve respectively then diodes pair D2, D3 conducts.
3) So one pair conducts during half period (positive) of sin wave and other pair during remaining half period (negative) of sine wave.
4) In both upper cases(1, 2) +ve voltage is present at output labeled 2 and -ve voltage side at output labeled as 4.

Full-Wave-Bridge-Rectifier-Supply-4.jpg
Click to Enlarge

Now again by combining these 2 parts we can see that these 4 diodes not only provide positive side of sin wave as it is at output(labeled as 2, 4) but also present negative side of sin wave as positive at same output(labeled as 2, 4). So in this way AC signal is converted into DC. But this is not smooth DC but pulsating DC as shown in figure. We will further use capacitors to make these pulsating DC as smooth DC.

Full-Wave-Bridge-Rectifier-Supply-1.jpg
Click to Enlarge

If we add capacitor C1 at diode output this half wave pulsating DC is stored in this capacitor. At every half cycle this capacitor is charged up and if this energy is not used by load (or in other words there is no path to discharge capacitor) then this capacitor is charged up to almost it’s rated voltage level. Now pulsating effect is removed by the use of this capacitor and smooth DC is obtained at some level. Small capacitor C2 to removes fast pulses of noise. Finally we add an LED D2 and resistor R1 as power on/off indicator. This network also work as to limit charging of capacitor. It provides a path for the discharge of capacitor. For this purpose we usually use a single resistor that is also called blade resistor. It limits capacitor charging to certain level so that load can be saved from over voltage. R1 also limits current through LED D2. We will also upload interesting LED projects for beginners, students and hobbyist.

More Projects

Half Wave Rectifier Supply

Description

It is 220 volt AC to 12 volt DC half wave rectifier supply.

Diagram

Detail

It uses single diode to rectify AC voltage. We can divide this circuit into parts as follows

Half-Wave-Rectifier-Supply-1.jpg
Click to Enlarge

In this part diode D1 passes positive signal part and blocks negative part of the signal. In this way half wave pulsating DC is available at output of diode D1.

Half-Wave-Rectifier-Supply-2.jpg
Click to Enlarge

If we add capacitor C1 at diode output this half wave pulsating DC is stored in this capacitor. At every half cycle this capacitor is charged up and if this energy is not used by load (or in other words there is no path to discharge capacitor) then this capacitor is charged up to almost it’s rated voltage level. Now pulsating effect is removed by the use of this capacitor and smooth DC is obtained at some level.

Half-Wave-Rectifier-Supply-3.jpg
Click to Enlarge

Now we add small capacitor C2 to remove fast pulses of noise.

Half-Wave-Rectifier-Supply-4.jpg
Click to Enlarge

Now finally we add an LED D2 and resistor R1 as power on/off indicator. This network also work as to limit charging of capacitor. It provides a path for the discharge of capacitor. For this purpose we usually use a single resistor that is also called blade resistor. It limits capacitor charging to certain level so that load can be saved from over voltage. R1 also limits current through LED D2. We will also upload interesting LED projects for beginners, students and hobbyist.

More Projects

220V AC to 12V AC Power Supply

Description

It is 220 volt AC to 12 volt AC step down supply.

Diagram

Detail

It is simplest power supply. It step downs 220 volt AC mains supply available in offices and homes to 12 volt AC. It utilizes a step down transformer to step down voltage that can be used for different purposes. We can use different watt transformer for different wattage output requirement. If we want 12 volt and 1 ampere supply then we will use 12 watt 12 volt step down transformer and if we want 12 volt and 2 ampere output we will use 24 watt and 12 volt step down transformer.

We can calculate wattage of transformer by using formula

P = IV

where
P is power in watts
I is current in amperes
V is voltage in volts

References

Here is an important and useful app for transformer wire gauge calculation and turn per volt calculation.