|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
可以实现篮球计分基本功能
- V- W# C8 {' \* ?仿真原理图如下(大家自行画一下图)4 v, E6 R9 ]$ e
7 C# P% L& p- `0 j& d: A
. k3 K2 l$ X: F: X0 e单片机源程序如下:
: h( p% G4 M$ P1 v- #include<reg52.h>
- #include<absacc.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit A8=P2^0;
- sbit A9=P2^1;
- sbit A10=P2^2;
- sbit A11=P2^3;
- sfr P00=0x80;
- uchar code wei_1[]={0X01,0X02,0X04,0X08,0X10,0X20,0X40,0X80};
- uchar code table[]={0X3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F,0x77,0x7c,0x39,0x5e,0x79,0x71,0X00,0X40};
- uchar minute=12; //分
- uchar second=0; //秒
- uchar key_status=0; //按键状态值
- uchar scoreA=0; //A队得分
- uchar scoreB=0; //B队得分
- uchar count1=20; //记录定时器时间
- uchar match=1; //比赛节数
- uchar rule_time=24; //记录24秒规则
- uchar rule_flag=0; //记录24秒规则是否启用
- /*
- 初始化IO口
- */
- void IO_init(){
- A8=0;
- A9=0;
- }
- /*
- 初始化时间
- */
- void time_init(){
- TMOD = 0x01;
- TH0 = (65536-50000)/256;
- TL0 = (65536-50000)%256;
- EA = 1;
- ET0 = 1;
- }
- /*
- 延时函数
- */
- void delay_ms(uint z){
- uint i;
- uchar j;
- for(i=0;i<z;i++){
- for(j=0;j<100;j++);
- }
- }
- /*
- 计时函数
- */
- void T0_Time() interrupt 1 using 1{
- if (count1-- == 0)
- {
- second--;
- rule_time--;
- count1 = 20;
- }
- TH0 = (65536-50000)/256;
- TL0 = (65536-50000)%256;
- }
- /*
- 时间显示函数
- */
- void time_display(uchar w2,uchar d2){
- uchar shi1,ge1;
- shi1=d2/10;
- ge1=d2%10;
- P00=wei_1[w2];
- A10=1;
- A10=0;
- P00=table[shi1];;
- A8=1;
- A8=0;
- delay_ms(5);
- P00=0;
- A10=1;
- A10=0;
- A8=1;
- A8=0;
- P00=wei_1[w2+1];
- A10=1;
- A10=0;
- P00=table[ge1];
- A8=1;
- A8=0;
- delay_ms(5);
- P00=0;
- A10=1;
- A10=0;
- A8=1;
- A8=0;
- }
- /*
- 分数显示函数
- */
- void score_dispaly(uchar w2,uchar d2){
- uchar qian2,shi2,ge2;
- qian2=d2/100;
- shi2=d2%100/10;
- ge2=d2%100%10;
- P00=wei_1[w2];
- A9=1;
- A9=0;
- P00=table[qian2];;
- A8=1;
- A8=0;
- delay_ms(10);
- P00=0;
- A9=1;
- A9=0;
- A8=1;
- A8=0;
- P00=wei_1[w2+1];
- A9=1;
- A9=0;
- P00=table[shi2];
- A8=1;
- A8=0;
- delay_ms(10);
- P00=0;
- A9=1;
- A9=0;
- A8=1;
- A8=0;
- P00=wei_1[w2+2];
- A9=1;
- A9=0;
- P00=table[ge2];
- A8=1;
- A8=0;
- delay_ms(10);
- P00=0;
- A9=1;
- A9=0;
- A8=1;
- A8=0;
- }
- /*
- 键盘输入函数
- */
- void key(){
- static uchar key_new = 0, key_l,temp;
- key_status = 0;
- P1 = 0x0f;
- if((P1 & 0x0f) != 0x0f)
- {
- delay_ms(1);
- if(((P1 & 0x0f) != 0x0f) && (key_new == 1))
- {
- key_new = 0;
- /*
- 矩阵键盘扫描
- */
- key_l = (P1 | 0xf0);
- P1 = key_l;
- switch(P1)
- {
- /*
- 开始计时/比赛继续
- */
- case 0x7e:
- TR0=1;
- rule_flag=1;
- time_display(6,rule_time);
- break;
- /*
- 暂停比赛
- */
- case 0x7d:
- TR0=0;
- break;
- /*
- 加分,同时24秒清零和总计时器停止计时
- */
- case 0xee:
- TR0=0;
- scoreA +=3;
- rule_time=24;
- rule_flag=0;
- break;
- case 0xed:
- TR0=0;
- scoreA +=2;
- rule_time=24;
- rule_flag=0;
- break;
- case 0xeb:
- TR0=0;
- scoreA +=1;
- rule_time=24;
- rule_flag=0;
- break;
- case 0xde:
- TR0=0;
- scoreB +=3;
- rule_time=24;
- rule_flag=0;
- break;
- case 0xdd:
- TR0=0;
- scoreB +=2;
- rule_time=24;
- rule_flag=0;
- break;
- case 0xdb:
- TR0=0;
- scoreB +=1;
- rule_time=24;
- rule_flag=0;
- break;
- /*
- 增加比赛节数,同时24秒复位和总计时器复位
- */
- case 0xbd:
- match++;
- minute=12;
- second=0;
- TR0=0;
- rule_time=24;
- rule_flag=0;
- break;
- /*
- 手动24秒复位
- */
- case 0xbe:
- if(TR0==1){
- rule_time=0;
- rule_flag=0;
- }else{
- rule_flag=0;
- }
- break;
- }
- }
- }
- else
- {
- key_new = 1;
- }
- }
- void main()
- {
- IO_init();
- time_init();
- while(1){
- key();
- if(second==0){
- /*
- 判断某节比赛是否结束
- */
- if(minute==0){
- /*
- 判断是否为最后一节比赛
- */
- if(match<4){
- /*
- 计时系统恢复为默认值,比赛节数加一
- */
- second=60;
- minute=11;
- match++;
- rule_time=24;
- rule_flag=0;
- }
- TR0=0;
- }
- /*
- 比赛分钟数减一
- */
- else {
- second=60;
- minute--;
- }
- }
- /*
- 24秒结束,计时器暂停和24秒复位
- */
- if(rule_time<=0){
- TR0=0;
- rule_time=24;
- }
- /*
- 显示
- */
- time_display(0,minute);
- time_display(2,second);
- time_display(4,match);
- time_display(6,rule_time);
- score_dispaly(1,scoreA);
- score_dispaly(5,scoreB);
- }
- }
5 H: t( @& R, L
: n( l; a% k+ r" Z |
|