Random Number Generator Using 8051
Random Number Generator Using 8051
A random number generator using 8051 that displays a random number between 0 & 99 is shown in this article. The circuit it self is very simple and may not find any applications in serious embedded projects and this article is just an illustration. The circuit is based on AT89S51 microcontroller, two seven segment LED displays, two transistors and few passive components.
Circuit diagram.
The two seven segment LED displays are multiplexed together and their data lines are connected to Port0 of the microcontroller. Transistors Q1 and Q2 drives the corresponding displays D1 and D2. The driving signals for there transistors are obtained from P1.1 and P1.2. Push button switch S1,capacitor C1 and resistor R10 forms a debouncing reset circuit. Resistor R9, capacitor C2 and pushbutton switch S2 will provide an active low harware interrupt signal at INTO (pin12) when ever S2 is pressed. Here also R9 and C2 are meant for debouncing. After power ON the display will show blank and when push button S2 is pressed the display will show a random number between 0 and 99. For another try you have to press the reset switch and then switch S2. If you need a single digit setup only, the remove display D2 and its associated components. Everything else is same.
Program.
ORG 000H SJMP MAIN ORG 003H // sets the starting address for the ISR ACALL ISR // calls the ISR subroutine when S2 is pressed
RETI // return from interrrupt MAIN:SETB IP.0 // this part sets the initial conditions SETB TCON.0 SETB IE.0 SETB IE.7 MOV P0,#00000000B MOV P1,#00000000B MOV DPTR,#LUT // moves the starting address of LUT to DPTR LABEL:MOV R6,#99D // this part generates the random number LOOP:MOV A,R6 DJNZ R6,LOOP SJMP LABEL ISR: MOV A,R6 // Subroutine ISR displays the current random number MOV B,#10D DIV AB SETB P1.2 ACALL DISPLAY MOV P0,A ACALL DELAY MOV A,B CLR P1.2 SETB P1.1 ACALL DISPLAY MOV P0,A ACALL DELAY CLR P1.1 SJMP ISR RET DELAY: MOV R3,#02H // this subroutine creates 1mS delay for switching the displays DEL1: MOV R2,#0FAH DEL2: DJNZ R2,DEL2 DJNZ R3,DEL1 RET DISPLAY: MOVC A,@A+DPTR // produces the digit drive pattern for the current digit in A RET LUT: DB DB DB DB DB DB DB DB 3FH // Look up table 06H 5BH 4FH 66H 6DH 7DH 07H
#include <stdlib.h>#include <stdio.h> /* for printf */ void tst_srand (void) { int i; int r; srand (56); for (i = 0; i < 10; i++) { printf ("I = %d, RAND = %d\n", i, rand ()); } }
#include <stdlib.h>#include <stdio.h> /* for printf */ void tst_rand (void) { int i; int r; for (i = 0; i < 10; i++) { printf ("I = %d, RAND = %d\n", i, rand ()); } }