基于51单片机随机数产生.docx

上传人:b****3 文档编号:5711872 上传时间:2023-05-09 格式:DOCX 页数:14 大小:44.11KB
下载 相关 举报
基于51单片机随机数产生.docx_第1页
第1页 / 共14页
基于51单片机随机数产生.docx_第2页
第2页 / 共14页
基于51单片机随机数产生.docx_第3页
第3页 / 共14页
基于51单片机随机数产生.docx_第4页
第4页 / 共14页
基于51单片机随机数产生.docx_第5页
第5页 / 共14页
基于51单片机随机数产生.docx_第6页
第6页 / 共14页
基于51单片机随机数产生.docx_第7页
第7页 / 共14页
基于51单片机随机数产生.docx_第8页
第8页 / 共14页
基于51单片机随机数产生.docx_第9页
第9页 / 共14页
基于51单片机随机数产生.docx_第10页
第10页 / 共14页
基于51单片机随机数产生.docx_第11页
第11页 / 共14页
基于51单片机随机数产生.docx_第12页
第12页 / 共14页
基于51单片机随机数产生.docx_第13页
第13页 / 共14页
基于51单片机随机数产生.docx_第14页
第14页 / 共14页
亲,该文档总共14页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

基于51单片机随机数产生.docx

《基于51单片机随机数产生.docx》由会员分享,可在线阅读,更多相关《基于51单片机随机数产生.docx(14页珍藏版)》请在冰点文库上搜索。

基于51单片机随机数产生.docx

基于51单片机随机数产生

基于51单片机随机数产生

一:

系统主要功能与设计方案

功能:

可以根据需要产生给定范围内的任何数字(0--999),显示于数码管上。

设计方案:

利用51单片机内部的定时器与中断结合,来模仿随机数的产生;单片机编程上电后,定时器便启动开始计数,计数范围可以预先设置,其设置的上限值被储存在24c02中;当中断0口产生低电平,进入中断函数读取定时器所跑的数值,经处理送入数码管显示。

二:

电路的硬件

电路搭建如下图,这里我们采用74ls595来驱动数码管,另外,为了防止电流过大这里串联一个75欧电阻用于限流;当操控"启动"按键后,三个数码管开始跑数字,操控“选择”锁定当前显示的数字,达到随机数产生的目的;考虑到,不同的人对随机数产生的范围要求不同,这里,通过操控“储存,加1,加10,”预设的上限值存储在24c02中。

三:

软件设计

#include

#include

#defineucharunsignedchar

#defineuintunsignedint

#definedelay_nop_()/*定义空指令*/

sbitds0=P2^0;//74ls595移位寄存器

sbitst0=P2^1;

sbitsh0=P2^2;

sbitds1=P2^3;

sbitst1=P2^4;

sbitsh1=P2^5;

sbitds2=P2^6;

sbitst2=P2^7;

sbitsh2=P0^7;

sbitp30=P3^0;//启动

sbitp31=P3^1;//加1

sbitp32=P3^2;//选择

sbitp35=P3^5;//存储

sbitp36=P3^6;//加10

sbitscl=P0^0;//I2C时钟

sbitdat=P0^1;//I2C数据

bitack;/*应答标志位*/

uchartemp,a,j,aa,flag,flag1,flag2,flag3;

ucharb,c,d;

ucharbb,cc,dd;

ucharx,y,z,f,xx;

ucharcodetab[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};//共阴

voiddelay1(uintZ)

{

uintx,y;

for(x=Z;x>0;x--)

for(y=110;y>0;y--);

}

voidwrite0(unsignedchara)

{

unsignedchari;

st0=0;

for(i=0;i<8;i++)

{

if(a&0x01)

ds0=1;

else

ds0=0;

sh0=1;

sh0=0;

a>>=1;

}

st0=1;

}

voidwrite1(unsignedchara)

{

unsignedchari;

st1=0;

for(i=0;i<8;i++)

{

if(a&0x01)

ds1=1;

else

ds1=0;

sh1=1;

sh1=0;

a>>=1;

}

st1=1;

}

voidwrite2(unsignedchara)

{

unsignedchari;

st2=0;

for(i=0;i<8;i++)

{

if(a&0x01)

ds2=1;

else

ds2=0;

sh2=1;

sh2=0;

a>>=1;

}

st2=1;

}

voidinit()

{

scl=1;

dat=1;

a=0;

p30=1;

p31=1;

p32=1;

p35=1;

p36=1;

bb=0;

temp=0;

TMOD=0x02;

TH0=0x00;

TL0=0x00;

EA=1;

ET0=1;

TR0=1;

IT0=1;

EX0=1;

}

voidstart24()

{

dat=1;

delay;

scl=1;

delay;

delay;

delay;

delay;

delay;

dat=0;delay;

}

voidstop24()

{

dat=0;

delay;

delay;

scl=1;

delay;

delay;

dat=1;

delay;delay;delay;delay;delay;

}

voidrespons24()

{

uchari;

scl=1;delay;

while((dat==1)&&(i<255));

scl=0;

delay;

delay;

delay;

}

voidwrite24(uchara)

{

uchari,temp;

temp=a;

scl=0;

delay;

delay;

for(i=0;i<8;i++)

{

temp=temp<<1;

dat=CY;

delay;delay;delay;delay;delay;

scl=1;

delay;

delay;delay;delay;

scl=0;

delay;delay;

}

dat=1;//释放总线

delay;

delay;delay;delay;

}

ucharread24()

{

uchari,j,k;

scl=0;

delay;delay;delay;delay;

dat=1;//释放总线

delay;delay;delay;

for(i=0;i<8;i++)

{

scl=1;

delay;delay;delay;delay;

j=dat;

k=(k<<1)|j;

delay;

scl=0;

delay;

delay;delay;delay;

}

returnk;

}

voidwritein(ucharaddress,uchardate)

{

start24();

write24(0xa0);

respons24();

write24(address);

respons24();

write24(date);

respons24();

stop24();

}

ucharreadout(ucharaddress)

{

start24();

write24(0xa0);

respons24();

write24(address);

respons24();

start24();

write24(0xa1);

respons24();

temp=read24();

returntemp;

}

voidmain()

{

init();//初始化

temp=readout(0);//读取24c02内数值

delay1(20);//等待读出

cc=(temp&0xf0)/16;

dd=(temp&0x0f)%16;

temp=cc*16+dd;

while

(1)

{

if(p31==0)//加1

{

while(p31!

=1)

{

flag=0;

flag1=1;

flag2=0;

}

}

if(flag1==1)

{

temp++;

x=temp/100;

y=temp%100/10;

z=temp%10;

write0(tab[z]);

write1(tab[y]);

write2(tab[x]);

flag1=0;

}

if(p36==0)//加10

{

while(p36!

=1)

{

flag=0;flag1=0;flag2=1;

}

}

if(flag2==1)

{

temp=f*10;

x=temp/100;

y=temp%100/10;

z=temp%10;

write0(tab[z]);

write1(tab[y]);

write2(tab[x]);

flag2=0;

f++;

}

if(p30==0)//启动

{

while(p30!

=1);

flag=1;

}

if(flag==1)

{

write0(tab[j]);delay1(20);

write1(tab[10-j]);delay1(30);

write2(tab[j]);delay1(30);

j++;

if(j==9)

j=0;

}

if(p35==0)//存储用户设置的随机数产生范围到24c02

{

while(p35!

=1)

{

flag=0;flag1=0;flag2=0;flag3=1;

}

}

if(flag3==1)

{

xx=temp;

writein(0,((xx/16)<<4|(xx%16)));

delay1(2000);

flag3=0;

flag=1;

}

}

}

voidtimer0()interrupt1

{

TH0=0;

TL0=0;

aa++;

if(aa==temp)

aa=0;

}

voidint0()interrupt0

{

flag=0;

d=aa/100;

c=aa%100/10;

b=aa%10;

write0(tab[b]);

write1(tab[c]);

write2(tab[d]);

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2