使用51单片机实现的篮球计分器设计
本项目采用芯片:AT89C52
本项目采用的编码程序:keil 5
需要用到的模块:定时器,LCD1602,独立按键(本人采用矩阵键盘,实际选择看功能多少),蜂鸣器
所实现的功能:
-
每一小节12分钟自动倒计时
-
A、B两队各自计分,由按键控制(本项目采用得几分按键按几下,也可根据需要将不同按键设计成1,2,3分)
-
每队都有24秒进攻时间显示,在进球得分后将24秒重新计时
-
比赛可设置暂停键以及重启键
-
所有过程均有蜂鸣器提示音
效果图:
没有搞仿真,感觉太麻烦了,不如一块开发板,嘿嘿
主程序:
#include <REGX52.H>
#include "timer0.h"
#include "lcd1602.h"
#include "matrixkey.h"
#include "buzzer.h"
#include "delay.h"
unsigned int min=12,sec=0,score1,score2,ball0=24;
unsigned char keynum;
void main()
{
LCD_Init();
LCD_ShowString(1,2,"A B");
while(1)
{
LCD_ShowNum(1,4,score1,3);
LCD_ShowNum(1,11,score2,3);
LCD_ShowString(2,9,":");
LCD_ShowNum(2,7,min,2);
LCD_ShowNum(2,10,sec,2);
LCD_ShowNum(1,8,ball0,2);
keynum=matrixkey();
if(keynum) //按键提示音
{
buzzer_time(100);
}
if(keynum==1) //比赛开始
{
timer0_Init();
}
if(keynum==2) //一队加分
{
score1++;
ball0=24;
}
if(keynum==3) //二队加分
{
score2++;
ball0=24;
}
if(keynum==4) //暂停
{
EA=0;
}
if(keynum==5) //复位
{
break;
}
}
}
void timer0_routine() interrupt 1
{
static unsigned int t0count;
TL0 = 0x18;//设置定时初始值
TH0 = 0xFC;//设置定时初始值
t0count++;
if(t0count>=1000)
{
t0count=0;
if(sec<=0)
{
sec=60;
if(min<=0)
min=12;
min–;
}
sec–;
if(ball0<=0)
{
ball0=25;
}
ball0–;
}
}
可直接复制
所需要模块程序
-
定时器:
#include <REGX52.H>
void timer0_Init()
{
TMOD &= 0xF0;//设置定时器模式
TMOD |= 0x01;//设置定时器模式
TL0 = 0x18;//设置定时初始值
TH0 = 0xFC;//设置定时初始值
TF0 = 0;//清除TF0标志
TR0 = 1;//定时器0开始计时
//TMOD=0x01;
//TH0=64535/256;
//TL0=64535%256;
ET0=1;
EA=1;
PT0=0;
}
-
蜂鸣器:
#include <REGX52.H>
#include <INTRINS.H>
sbit buzzer=P2^5;
//蜂鸣器私有延时函数
void buzzer_Delay500us()//@12.000MHz
{
unsigned char i;
_nop_();
i = 247;
while (–i);
}
void buzzer_time(unsigned int x)
{
unsigned int i;
for(i=0;i<x*2;i++)
{
buzzer=!buzzer;
buzzer_Delay500us();
}
}
-
矩阵键盘:
#include <REGX52.H>
#include "Delay.h"
unsigned char matrixkey()
{
unsigned char keynumber=0;
P1=0xff;
P1_3=0;
if(P1_7==0) {Delay(20) ;while(P1_7==0) ;Delay(20) ;keynumber=1;}
if(P1_6==0) {Delay(20) ;while(P1_6==0);Delay(20) ;keynumber=5;}
if(P1_5==0) {Delay(20);while(P1_5==0);Delay(20);keynumber=9;}
if(P1_4==0) {Delay(20) ;while(P1_4==0) ;Delay(20) ;keynumber=13;}
P1=0xff;
P1_2=0;
if(P1_7==0) {Delay(20) ;while(P1_7==0) ;Delay(20) ;keynumber=2;}
if(P1_6==0) {Delay(20) ;while(P1_6==0);Delay(20) ;keynumber=6;}
if(P1_5==0) {Delay(20);while(P1_5==0);Delay(20);keynumber=10;}
if(P1_4==0) {Delay(20) ;while(P1_4==0) ;Delay(20) ;keynumber=14;}
P1=0xff;
P1_1=0;
if(P1_7==0) {Delay(20) ;while(P1_7==0) ;Delay(20) ;keynumber=3;}
if(P1_6==0) {Delay(20) ;while(P1_6==0);Delay(20) ;keynumber=7;}
if(P1_5==0) {Delay(20);while(P1_5==0);Delay(20);keynumber=11;}
if(P1_4==0) {Delay(20) ;while(P1_4==0) ;Delay(20) ;keynumber=15;}
P1=0xff;
P1_0=0;
if(P1_7==0) {Delay(20) ;while(P1_7==0) ;Delay(20) ;keynumber=4;}
if(P1_6==0) {Delay(20) ;while(P1_6==0);Delay(20) ;keynumber=8;}
if(P1_5==0) {Delay(20);while(P1_5==0);Delay(20);keynumber=12;}
if(P1_4==0) {Delay(20) ;while(P1_4==0) ;Delay(20) ;keynumber=16;}
return keynumber;
}
-
独立按键:
#include <REGX52.H>
#include "Delay.h"
unsigned char key()
{
unsigned char keynum=0;
if(P3_1==0){Delay(20);while(P3_1==0);Delay(20);keynum=1;}
if(P3_0==0){Delay(20);while(P3_0==0);Delay(20);keynum=2;}
if(P3_2==0){Delay(20);while(P3_2==0);Delay(20);keynum=3;}
if(P3_3==0){Delay(20);while(P3_3==0);Delay(20);keynum=4;}
return keynum;
}
-
LCD1602:
#include <REGX52.H>
//引脚配置:
sbit LCD_RS=P2^6;
sbit LCD_RW=P2^5;
sbit LCD_EN=P2^7;
#define LCD_DataPort P0
//函数定义:
/**
* @brief LCD1602延时函数,12MHz调用可延时1ms
* @param 无
* @retval 无
*/
void LCD_Delay()
{
unsigned char i, j;
i = 2;
j = 239;
do
{
while (–j);
} while (–i);
}
/**
* @brief LCD1602写命令
* @param Command 要写入的命令
* @retval 无
*/
void LCD_WriteCommand(unsigned char Command)
{
LCD_RS=0;
LCD_RW=0;
LCD_DataPort=Command;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
/**
* @brief LCD1602写数据
* @param Data 要写入的数据
* @retval 无
*/
void LCD_WriteData(unsigned char Data)
{
LCD_RS=1;
LCD_RW=0;
LCD_DataPort=Data;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
/**
* @brief LCD1602设置光标位置
* @param Line 行位置,范围:1~2
* @param Column 列位置,范围:1~16
* @retval 无
*/
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
if(Line==1)
{
LCD_WriteCommand(0x80|(Column-1));
}
else if(Line==2)
{
LCD_WriteCommand(0x80|(Column-1+0x40));
}
}
/**
* @brief LCD1602初始化函数
* @param 无
* @retval 无
*/
void LCD_Init()
{
LCD_WriteCommand(0x38);//八位数据接口,两行显示,5*7点阵
LCD_WriteCommand(0x0c);//显示开,光标关,闪烁关
LCD_WriteCommand(0x06);//数据读写操作后,光标自动加一,画面不动
LCD_WriteCommand(0x01);//光标复位,清屏
}
/**
* @brief 在LCD1602指定位置上显示一个字符
* @param Line 行位置,范围:1~2
* @param Column 列位置,范围:1~16
* @param Char 要显示的字符
* @retval 无
*/
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)
{
LCD_SetCursor(Line,Column);
LCD_WriteData(Char);
}
/**
* @brief 在LCD1602指定位置开始显示所给字符串
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param String 要显示的字符串
* @retval 无
*/
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=0;String[i]!='\0';i++)
{
LCD_WriteData(String[i]);
}
}
/**
* @brief 返回值=X的Y次方
*/
int LCD_Pow(int X,int Y)
{
unsigned char i;
int Result=1;
for(i=0;i<Y;i++)
{
Result*=X;
}
return Result;
}
/**
* @brief 在LCD1602指定位置开始显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~65535
* @param Length 要显示数字的长度,范围:1~5
* @retval 无
*/
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i–)
{
LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');
}
}
/**
* @brief 在LCD1602指定位置开始以有符号十进制显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:-32768~32767
* @param Length 要显示数字的长度,范围:1~5
* @retval 无
*/
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
{
unsigned char i;
unsigned int Number1;
LCD_SetCursor(Line,Column);
if(Number>=0)
{
LCD_WriteData('+');
Number1=Number;
}
else
{
LCD_WriteData('-');
Number1=-Number;
}
for(i=Length;i>0;i–)
{
LCD_WriteData(Number1/LCD_Pow(10,i-1)%10+'0');
}
}
/**
* @brief 在LCD1602指定位置开始以十六进制显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~0xFFFF
* @param Length 要显示数字的长度,范围:1~4
* @retval 无
*/
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i,SingleNumber;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i–)
{
SingleNumber=Number/LCD_Pow(16,i-1)%16;
if(SingleNumber<10)
{
LCD_WriteData(SingleNumber+'0');
}
else
{
LCD_WriteData(SingleNumber-10+'A');
}
}
}
/**
* @brief 在LCD1602指定位置开始以二进制显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~1111 1111 1111 1111
* @param Length 要显示数字的长度,范围:1~16
* @retval 无
*/
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i–)
{
LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0');
}
}
-
延时函数:
void Delay(unsigned int x)
{
unsigned char i, j;
while(x–)
{
i = 2;
j = 239;
do
{
while (–j);
} while (–i);
}
}
大体程序就是这样,新人第一次发帖也没有什么经验,希望能够对大家有帮助,如果有不足,也希望大家可以多多指正,感激不尽。
🙏🙏🙏🙏🙏