单片机课程设计---篮球记分牌
程序员文章站
2022-03-07 18:23:07
单片机课程设计# include /******定义单个位操作*****/sbit resset = P1^4;//定义复位按键sbit redRLED = P0^0;//定义加分红灯sbit redYLED = P0^1;//定义减分黄灯sbit blueRLED = P0^2;sbit blueYLED = P0^3;sbit buzz = P2^4;//蜂鸣器#define uint unsigned int#define uchar u...
单片机课程设计
# include <reg51.h>
/******定义单个位操作*****/
sbit resset = P1^4; //定义复位按键
sbit redRLED = P0^0; //定义加分红灯
sbit redYLED = P0^1; //定义减分黄灯
sbit blueRLED = P0^2;
sbit blueYLED = P0^3;
sbit buzz = P2^4; //蜂鸣器
#define uint unsigned int
#define uchar unsigned char
uint i,j;
uint redCount = 0, blueCount = 0; //记录红队蓝队的分数
uchar keyFlag; //标志位
uchar ledTable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //共阴极段选值
/******函数声明*******/
void delay(uint i);
void buttonChange();
void display();
void res();
/******主函数********/
void main()
{
while (1){ //死循环
res(); //复位函数
if ((P1&0xff)!=0xff){ //判断是否有键按下
delay(100); //消抖
if ((P1&0xff)!=0xff){
keyFlag = (P1&0xff);
}
else{
buzz = 0;
}
}
buttonChange(); //
display(); //显示函数
}
}
/****复位函数****/
void res(){
if (resset == 0){
P3 = ledTable[0];
P2 = 0x00;
redCount = blueCount = 0;
delay(5);
}
}
/****延时函数*****/
void delay(uint i){
for(; i>0; i--)
for(j = 200; j > 0; j--);
}
/******按键函数*****/
void buttonChange(){
switch(keyFlag){
case 0xfe: //红队加一
{
redRLED = 0; //LED灯1 亮
buzz = 1; //蜂鸣器开
delay(30);
redCount += 1;
keyFlag = 0xff;
break;
}
case 0xfb: //蓝队加一
{
blueRLED = 0; //LED灯3 亮
buzz = 1; //蜂鸣器开
delay(30);
blueCount += 1;
keyFlag = 0xff;
break;
}
case 0xfd: //红队减一
{
redYLED = 0; //LED灯2 亮
buzz = 1; //蜂鸣器开
delay(30);
redCount -= 1;
keyFlag = 0xff;
break;
}
case 0xf7: //蓝队减一
{
blueYLED = 0; //LED灯4 亮
buzz = 1; //蜂鸣器开
delay(30);
blueCount -= 1;
keyFlag = 0xff;
break;
}
default: //按键放开
P0 = 0xff; //LED灯灭
break;
}
}
/*******显示函数*******/
void display(void){
P3 = ledTable[(redCount % 10)]; //红队个位
P2 = 0x0d;
delay(5);
P2 = 0x0f;
P3 = ledTable[(redCount / 10)]; //红队十位
P2 = 0x0e;
delay(5);
P2 = 0x0f;
P3 = ledTable[(blueCount % 10)]; //蓝队个位
P2 = 0x07;
delay(5);
P2 = 0x0f;
P3 = ledTable[(blueCount / 10)]; //蓝队十位
P2 = 0x0b;
delay(5);
P2 = 0x0f;
}
本文地址:https://blog.csdn.net/qq_43710889/article/details/108182642