Ahmad Bilal
Newbie level 2
I'm trying to generate random number in embedded C using "stdlib.h" and rand() function on Kiel C51 compiler. But every time i reset microcontroller, i get the same sequence of number not random numbers. I think its bcoz the 8051 family doesn't have a realtime timing clock. Any help regarding the problem will be appreciated....!!!
I want to generate a random number for luddoo simulation and display this random number on 7-Segment via Port 2:
I want to generate a random number for luddoo simulation and display this random number on 7-Segment via Port 2:
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 code is here: /* Main.c file generated by New Project wizard * * Created: Wed Jul 31 2013 * Processor: AT89C51 * Compiler: Keil for 8051 */ #include <reg51.h> #include <stdio.h> #include <stdlib.h> unsigned int Random_Num(); void Delay_Loop(); void main(void) { unsigned int random; P2=0; while(1) { random=(rand()%130); if(random==6){ P2=random; Delay_Loop(); } if(random==91){ P2=random; Delay_Loop(); } if(random==79){ P2=random; Delay_Loop(); } if(random==102){ P2=random; Delay_Loop(); } if(random==109){ P2=random; Delay_Loop(); } if(random==125){ P2=random; Delay_Loop(); } } } void Delay_Loop() { int d; for(d=0;d<=20;d++) { TMOD=0x01; TL0=0xFD; TH0=0x04B; TR0=1; while(TF0==0); TR0=0; TF0=0; } }