|
;********WAVE-E6000/T************************************ ;*MCU: AT89C51 * ;*MCU-crystal: 6M * ;*Version: 00 * ;*Last Updata: * ;*Author: www.picavr.com * ;*Description: 用固定種子(x=99)隨機產(chǎn)生[0,255]區(qū)間的數(shù)* ; 在P0、P2口顯示 * ;******************************************************** #include <reg51.h> unsigned char y,h,l; unsigned int i,j; unsigned long x=99; unsigned char led[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8, 0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; void delay(i) {while(i--) {j=7650;while(j--);} } unsigned int rand(x) unsigned long x; { x=(25173*x+13849)%65536; return x; } void main(void) { P0=0xf9;P2=0xf9;delay(10);P0=0xff;P2=0xff; /* 文件名標示 */ while(1) { x=rand(x); /* 產(chǎn)生[0 - 65535]的隨機數(shù) */ y=x*255/65535; /* 轉(zhuǎn)換為[0 - 99]的隨機數(shù) */ h=y>>4;l=y&0x0f; /* 分解高四位和第四位 */ P0=led[h];P2=led[l]; delay(10); P0=0xff;P2=0xff;delay(2); } }
|