送给楼主,刚写的,我也是初学,可能有些不太完善的地方,但是你说的功能都实现了,数码管为共阴,位选使用了138- #include"reg52.h"
- #include"stdlib.h" // //包含随机数函数的头文件
- #define uint unsigned int
- #define uchar unsigned char
- #define max 50 //max
- #define min 1 //min
- uint a,c,d,e,f=0;
- uchar code smgduan[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
- DisplayData[8];
- bit gg;
- sbit LSA=P2^2;
- sbit LSB=P2^3;
- sbit LSC=P2^4;
- sbit k1=P3^0; //按键
- sbit k2=P3^1;
- void inittimer()
- {
- TMOD|=0x11;
- TH0=(65535-1000)/256;
- TL0=(65535-1000)%256;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void delay(uint x)
- {
- while(x--);
- }
- void DigDisplay() //138位选
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- switch(i)
- {
- case(0):
- LSA=0;LSB=0;LSC=0; break;
- case(1):
- LSA=1;LSB=0;LSC=0; break;
- case(2):
- LSA=0;LSB=1;LSC=0; break;
- case(3):
- LSA=1;LSB=1;LSC=0; break;
- case(4):
- LSA=0;LSB=0;LSC=1; break;
- case(5):
- LSA=1;LSB=0;LSC=1; break;
- case(6):
- LSA=0;LSB=1;LSC=1; break;
- case(7):
- LSA=1;LSB=1;LSC=1; break;
- }
- P0=DisplayData[7-i];
- if(c>=200){c=0;};
- P0=0x00;
- }
- }
- void datapros() //段选
- {
- DisplayData[0] = smgduan[a/10];
- DisplayData[1] = smgduan[a%10];
- DisplayData[2] = 0x40;
- DisplayData[3] = smgduan[d/10];
- DisplayData[4] = smgduan[d%10];
- DisplayData[5] = 0x40;
- DisplayData[6] = smgduan[e/10];
- DisplayData[7] = smgduan[e%10];
- }
- void main()
- {
- double b;
- inittimer();
- haha:for(b=0;b<50000000;b++)
- {
- srand(b);
- a = rand()%49+1;
- srand(c);
- d = rand()%49+1;
- srand(b-c);
- e = rand()%49+1;
- datapros();
- if(k1==0)
- {
- delay(1000);if(k1==0)
- {
- TR0=0;;while(k2){DigDisplay();};TR0=1;
- }
- }
- delay(5500); //更改括号内数字来更改刷新速度
- }
- goto haha;
- }
- void init()interrupt 1
- {
- TH0=(65535-1000)/256;
- TL0=(65535-1000)%256;
- DigDisplay();
- c++;
- }
复制代码
|