单片机如何定时器设置不重复随机数
时间:10-02
整理:3721RD
点击:
请问我这个程序为什么不能不重复取随机数?怎么设置不让0出现
#include<reg52.h>
#include<stdlib.h>
#define uchar unsigned char
#define uint unsigned int
sbit en=P2^2;
sbit rs=P2^0;
sbit rw=P2^1;
sbit key=P1^0;
uchar table[52]=
{
1, 2, 3, 4, 5, 6, 7, 8, 9,
};
uchar table1[]=
{
0x17,0x15,0x15,0x15,0x15,0x15,0x17,0x00, //10
};
unsigned char a,b,c,d,j,k,f,l,m,sz[13];
void delay(uint a)
{
uchar b;
while(a--)
{
for(b=250;b>0;b--);
}
}
void lcd_wcom(uchar com)
{
rs=0;
rw=0;
P0=com;
delay(100);
en=1;
delay(100);
en=0;
}
void lcd_wdat(uchar dat)
{
rs=1;
rw=0;
P0=dat;
delay(20);
en=1;
delay(20);
en=0;
}
void lcd_wdat1(uchar h)
{
rs=1;
rw=0;
P2=h;
delay(20);
en=1;
delay(20);
en=0;
}
void lcd_init()
{
lcd_wcom(0x38);
lcd_wcom(0x0c);
lcd_wcom(0x06);
lcd_wcom(0x01);
}
void zd_init()
{
TMOD = 0X01;
TH0 = 0X00;
TL0 = 0X00;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void main()
{
lcd_init();
zd_init();
while(1)
{
m=0;
l=0;
if(key==0)
{
delay(5);
if(key==0)
{
k=TH0%14;//利用定时器中断来取随机数
if(k<10)
{
lcd_wcom(0x80+0x04);
lcd_wdat(0x30+k);//随机数加ACS码
delay(10);
sz[0]=k;
m++;
while(key==0);
}
if(k==13)
{
lcd_wcom(0x80+0x04);
lcd_wdat(0x40+k-2);
sz[0]=k;
m++;
while(key==0);
}
if(k==11)
{
lcd_wcom(0x80+0x04);
lcd_wdat(0x40+k);
sz[0]=k;
m++;
while(key==0);
}
if(k==12)
{
lcd_wcom(0x80+0x04);
lcd_wdat(0x51);
sz[0]=k;
m++;
while(key==0);
}
if(k==10)
{
lcd_wcom(0x40);
for(c=0;c<8;c++)
{
lcd_wdat(table[c]);
}
lcd_wcom(0x80+0x04);
for(d=0;d<1;d++)
{
lcd_wdat(d);
}
sz[0]=k;
m++;
while(key==0);
}
for(l=0;l<k;l++)
{
if(sz[l]==k);
continue;
}
sz[m]=k;
m++;
}
}
}
}
void init() interrupt 1
{
TH0 = 0X00;
TL0 = 0X00;
}
#include<reg52.h>
#include<stdlib.h>
#define uchar unsigned char
#define uint unsigned int
sbit en=P2^2;
sbit rs=P2^0;
sbit rw=P2^1;
sbit key=P1^0;
uchar table[52]=
{
1, 2, 3, 4, 5, 6, 7, 8, 9,
};
uchar table1[]=
{
0x17,0x15,0x15,0x15,0x15,0x15,0x17,0x00, //10
};
unsigned char a,b,c,d,j,k,f,l,m,sz[13];
void delay(uint a)
{
uchar b;
while(a--)
{
for(b=250;b>0;b--);
}
}
void lcd_wcom(uchar com)
{
rs=0;
rw=0;
P0=com;
delay(100);
en=1;
delay(100);
en=0;
}
void lcd_wdat(uchar dat)
{
rs=1;
rw=0;
P0=dat;
delay(20);
en=1;
delay(20);
en=0;
}
void lcd_wdat1(uchar h)
{
rs=1;
rw=0;
P2=h;
delay(20);
en=1;
delay(20);
en=0;
}
void lcd_init()
{
lcd_wcom(0x38);
lcd_wcom(0x0c);
lcd_wcom(0x06);
lcd_wcom(0x01);
}
void zd_init()
{
TMOD = 0X01;
TH0 = 0X00;
TL0 = 0X00;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void main()
{
lcd_init();
zd_init();
while(1)
{
m=0;
l=0;
if(key==0)
{
delay(5);
if(key==0)
{
k=TH0%14;//利用定时器中断来取随机数
if(k<10)
{
lcd_wcom(0x80+0x04);
lcd_wdat(0x30+k);//随机数加ACS码
delay(10);
sz[0]=k;
m++;
while(key==0);
}
if(k==13)
{
lcd_wcom(0x80+0x04);
lcd_wdat(0x40+k-2);
sz[0]=k;
m++;
while(key==0);
}
if(k==11)
{
lcd_wcom(0x80+0x04);
lcd_wdat(0x40+k);
sz[0]=k;
m++;
while(key==0);
}
if(k==12)
{
lcd_wcom(0x80+0x04);
lcd_wdat(0x51);
sz[0]=k;
m++;
while(key==0);
}
if(k==10)
{
lcd_wcom(0x40);
for(c=0;c<8;c++)
{
lcd_wdat(table[c]);
}
lcd_wcom(0x80+0x04);
for(d=0;d<1;d++)
{
lcd_wdat(d);
}
sz[0]=k;
m++;
while(key==0);
}
for(l=0;l<k;l++)
{
if(sz[l]==k);
continue;
}
sz[m]=k;
m++;
}
}
}
}
void init() interrupt 1
{
TH0 = 0X00;
TL0 = 0X00;
}
加入if判断,只要这个数出现过就跳过他