Random Number Generator using 8051
In this project, I will illustrate the generation of a random number using an 8051 microcontroller by detailing the circuitry and the functioning of a Random Number Generator.
Upon pressing a push button, this project generates a random number ranging from 0 to 100. It’s important to note that this is a straightforward project, and embedded projects might not find practical applications for it. The project serves primarily as an illustrative example.ran
Outline
- Circuit Principle of Random Number using 8051
- Circuit Diagram Random Number using 8051 Microcontroller
- Components Required
- Circuit Design of Random Number using 8051
- How to Write the Program?
- CODE
- Random Number Generator using 8051 – Circuit Simulation Video
- How to Operate Random Number Circuit using 8051?
- Random Number Generator using 8051 Circuit Applications
- Random Number Generator using 8051 Project Output Video
Circuit Principle of Random Number Generator using 8051
Upon pressing a button, a random number generator generates a number within a defined range. When the push button, connected to P3.0, is pressed, the LCD (liquid crystal display) will show the randomly generated number.
This process involves rapidly counting from 0 to 99. The LCD displays the current count value upon pressing the button. As the count increases rapidly when the button is pressed, the 8051 Microcontroller selects a random number.
Circuit Diagram Random Number Generator using 8051 Microcontroller
Components Required
- AT89C51 (8051 Microcontroller)
- 8051 Programmer
- Programming cable
- 5V Power Supply
- 16×2 Alphanumeric LCD
- 2 Ceramic Capacitors – 33pF
- 11.0592 MHz Crystal
- Push Button – 3
- Electrolytic Capacitor – 10μF, 16V
- 10KΩ Resistor (1/4 watt) – 2
- 330Ω Resistor (1/4 watt) – 2
- POT – 10KΩ
- 1KΩ X 8 Resistor Pack
- Connecting Wires
Circuit Design of Random Number Generator using 8051
The circuit employs the AT89C51 Microcontroller as its central component, alongside the 162 alphanumeric LCD display, push buttons, and various passive components.
In this configuration, two push buttons are integrated into the circuit, one connected to P3.0 (Random) responsible for generating random numbers, and the other linked to P3.1 (RST) for count reset functionality.
For the LCD, the data pins interface with PORT2 of the microcontroller, while the control pins RS, RW, and En connect to P2.0, GND, and P2.1, respectively. Since the LCD operates in 8-bit mode, all of its data pins are utilized.
The 8051 microcontroller’s reset circuit comprises a 10K resistor, a 10F capacitor, and a push button. The oscillator for the 8051 microcontroller relies on two 33pF capacitors and an 11.0592 MHz Quartz Crystal.
To control the LCD’s contrast or brightness, a 10K potentiometer is linked to the VEE pin of the LCD. Adjusting the potentiometer’s resistance allows for fine-tuning the display’s contrast and brightness levels.
NOTE: If you want to reduce the data lines of LCD, you can use 4 bit mode.
How to Write the Program?
Now, let’s delve into the programming aspect of generating random numbers with an 8051 microcontroller. To initiate the process, declare an integer-type count variable and configure the LCD for 8-bit operation. To ensure the program continues indefinitely, employ an infinite while loop. Within this persistent WHILE loop, obtain the integer’s value and increment it until the button is pressed, achieved through an additional while loop.
The count ascends from 0 to 99 and then reverses its course, returning to 0, subsequently repeating this cycle until it reaches the upper limit, which is set at 100 in this example. You can adjust this value within the loop to alter the upper limit of the random number generator to suit your requirements. When the button is pressed, the counting process halts, and the current count value is displayed on the LCD.
If you wish to extend the upper limit of the random number generator within this software, minor modifications can be made. Utilizing an LCD allows for greater flexibility in adjusting the upper limit, a feature that may not be as easily accomplished when employing two seven-segment displays (as it would require an increase in the number of 7-segment displays).
CODE
#include<reg51.h> | |
#define lcd P0 | |
sbit rs=P2^0; | |
sbit e=P2^1; | |
sbit sw=P3^0; | |
sbit rst=P3^1; | |
unsigned int i=0; | |
void delay (int); | |
void display (unsigned char); | |
void cmd (unsigned char); | |
void string (char *); | |
void init (void); | |
void int_lcd(unsigned int); | |
void delay (int d) | |
{ | |
unsigned char i=0; | |
for(;d>0;d–) | |
{ | |
for(i=250;i>0;i–); | |
for(i=248;i>0;i–); | |
} | |
} | |
void cmd (unsigned char c) | |
{ | |
lcd=c; | |
rs=0; | |
e=1; | |
delay(5); | |
e=0; | |
} | |
void display (unsigned char c) | |
{ | |
lcd=c; | |
rs=1; | |
e=1; | |
delay(5); | |
e=0; | |
} | |
void string (char *p) | |
{ | |
while(*p) | |
{ | |
display(*p++); | |
} | |
} | |
void init (void) | |
{ | |
cmd(0x38); | |
cmd(0x0c); | |
cmd(0x01); | |
cmd(0x80); | |
} | |
void int_lcd(unsigned int n) | |
{ | |
char a[4]={0},i=0; | |
if(n==0) | |
{ | |
display(‘0’); | |
return; | |
} | |
else | |
{ | |
while(n>0) | |
{ | |
a[i++]=(n%10)+48; | |
n/=10; | |
} | |
for(–i;i>=0;i–) | |
{ | |
display(a[i]); | |
} | |
} | |
} | |
void main() | |
{ | |
sw=1; | |
rst=1; | |
init(); | |
cmd(0x80); | |
string(” Random Number “); | |
cmd(0xc0); | |
string(” Generator “); | |
delay(2000); | |
cmd(0x01); | |
while(1) | |
{ | |
cmd(0x80); | |
string(“Press the button”); | |
while(sw!=0) | |
{ | |
i=++i; | |
if(i==100) | |
i=0; | |
} | |
cmd(0x80); | |
string(“RAND NUM: “); | |
cmd(0x8a); | |
int_lcd(i); | |
while(rst!=0); | |
//cmd(0x01); | |
} | |
} |
Random Number Generator using 8051 – Circuit Simulation Video
How to Operate Random Number Generator Circuit using 8051?
- First, in Keil Vision Software, create a programme for a random number generator and a hex file.
- With the help of the 8051 Programmer, burn this hex file to the AT89C51 microcontroller.
- Connect the wires according to the circuit schematic.
- Ensure that the circuit’s power supply is 5V DC.
- Now, turn on the circuit’s power supply.
- On the LCD, you can see the string “Random Number Generator.”
- After that, you’ll be asked to press a button to produce a random number.
- When you press the push button, the LCD displays a random number. Until you push the RST button, this will be displayed (connected to P3.1).
- After pressing the RST button, you’ll be asked to hit it again to produce.
- This process continues until you switch off the power supply.
Random Number Generator using 8051 Circuit Applications
- This project is used in the applications where we need to generate Random number
- Used in noise generators
- Project is used as an alternative for the traditional dice while playing the games like monopoly, snake ladder.