用单片机和lcd来编写一个篮球计分器的代码
时间: 2024-03-13 07:42:30 浏览: 79
基于单片机的篮球计分器设计毕业设计.doc
好的,以下是一个基于单片机和LCD的篮球计分器的代码,其中使用的是8051单片机和1602A型号的LCD显示屏。代码中包括了得分、计时、犯规、暂停等功能。
```
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS = P2^0;
sbit RW = P2^1;
sbit EN = P2^2;
uchar count = 0;
uchar sec = 0;
uchar min = 0;
bit flag = 0;
void delay(uint i)
{
while(i--);
}
void write_com(uchar com)
{
RS = 0;
RW = 0;
P0 = com;
EN = 1;
delay(5);
EN = 0;
}
void write_data(uchar date)
{
RS = 1;
RW = 0;
P0 = date;
EN = 1;
delay(5);
EN = 0;
}
void init()
{
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void display_time()
{
write_com(0x80);
write_data('T');
write_data('I');
write_data('M');
write_data('E');
write_data(':');
write_data(min/10+0x30);
write_data(min%10+0x30);
write_data(':');
write_data(sec/10+0x30);
write_data(sec%10+0x30);
}
void display_score(uchar score)
{
write_com(0xc0);
write_data('S');
write_data('C');
write_data('O');
write_data('R');
write_data(':');
write_data(score/10+0x30);
write_data(score%10+0x30);
}
void main()
{
init();
display_time();
display_score(0);
while(1)
{
if(flag == 0)
{
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
TMOD = 0x01;
TR0 = 1;
flag = 1;
}
if(TF0 == 1)
{
count++;
if(count == 20)
{
sec++;
if(sec == 60)
{
sec = 0;
min++;
}
count = 0;
display_time();
}
TF0 = 0;
}
if(P3^0 == 0)
{
delay(10);
if(P3^0 == 0)
{
display_score(display_score+1);
}
}
if(P3^1 == 0)
{
delay(10);
if(P3^1 == 0)
{
display_score(display_score-1);
}
}
if(P3^2 == 0)
{
delay(10);
if(P3^2 == 0)
{
TR0 = 0;
flag = 0;
display_time();
}
}
if(P3^3 == 0)
{
delay(10);
if(P3^3 == 0)
{
TR0 = 0;
flag = 0;
display_time();
display_score(0);
sec = 0;
min = 0;
}
}
if(P3^4 == 0)
{
delay(10);
if(P3^4 == 0)
{
TR0 = 0;
flag = 0;
display_time();
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
TR0 = 1;
flag = 1;
}
}
}
}
```
这段代码实现了得分、计时、犯规、暂停等功能。其中,P3^0和P3^1分别控制得分增加和减少,P3^2和P3^3控制暂停和复位,P3^4控制计时器开始。
阅读全文