这个单片机小制作的功能很全:有比赛剩余时间 加分减分和归零,交换场地,暂停开始按钮,代码在附件,仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <reg51.h>
- #define DUAN P0
- #define WEI P2
- #define uchar unsigned char
- #define uint unsigned int
-
- unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- sbit key0=P3^0; //归零
- sbit key1=P3^1; // 归12
- sbit key2=P3^2; //分钟加一
- sbit key3=P3^3; //暂停/开始
- sbit beep=P3^7;
- uint addnum;
- uchar miaonum,fennum;
- uchar b;
-
- void delay(unsigned int num) //时间函数
- {
- unsigned i,j;
- for(i=num;i>0;i--)
- for(j=120;j>0;j--);
- }
- void main()
- {
- TMOD=0x02;
- TL0=TH0=6;
- ET0=1;
- EA=1;
- TR0=0;
- beep=0;
- b=10;
- fennum=12;
- miaonum=0;
- while(1)
- {
- P2=0xff;
- WEI=0xef;
- DUAN=table[miaonum/10];
- delay(5);
- P2=0xff;
- WEI=0xdf;
- DUAN=table[miaonum%10];
- delay(5);
- P2=0xff;
- WEI=0Xf7;
- DUAN=0X40;
- delay(5);
- P2=0xff;
- WEI=0xfd;
- DUAN=table[fennum/10];
- delay(5);
- P2=0xff;
- WEI=0xfb;
- DUAN=table[fennum%10];
- delay(5);
-
- if(!key0) //清零
- {
- delay(10);
- if(!key0)
- {
- fennum=0;
- miaonum=0;
- }
- while(!key0);
- }
-
- if(!key1) //分钟归10
- {
- delay(10);
- if(!key1)
- {
- fennum=10;
- }
- while(!key1);
- }
-
- if(!key2) //分钟加1
- {
- delay(10);
- if(!key2)
- {
- fennum++;
- }
- while(!key2);
- }
-
-
- if(!key3) //暂停/开始
- {
- delay(10);
- if(!key3)
- {
- TR0=~TR0;
- while(!key3);
- }
- }
- }
- }
-
-
- void timer0() interrupt 1
- {
-
- addnum++;
- if(addnum==400)
- {
- if(fennum!=0)
- {
- if(miaonum==0)
-
- miaonum=59,fennum--,addnum=0;
- else
- miaonum--;
- addnum=0;
- }
-
- if(fennum==0)
- {
- if(miaonum==0)
-
- miaonum=0;
-
- else
- miaonum--;
- addnum=0;
- }
- if(fennum==0)
- {
- if(miaonum==0)
- {
-
- if(b==0)
-
- beep=0;
- else
- beep=1,b--;
-
- }
- }
-
- }
- }
-
复制代码
所有资料51hei提供下载:
简易篮球计分器.zip
(29.22 KB, 下载次数: 74)
|