/*****************************************************************
Topic: Basketball game timing and scoring system
Hardware: STC89C52RC, 1602LCM LCD screen, six buttons
Software: Keil C
*****************************************************************/
#include //header file
#define uchar unsigned char //Macro definition
#define uint unsigned int
sbit S1=P3^2; //Interrupt 0 (game countdown starts/pauses)
sbit S2=P3^3; //Interrupt 1 (24s countdown restarts)
sbit key="P2"^4; //Connect S3, S4, S5, S6 (matrix keys)
sbit key_S3=P2^3; //Connect S3
sbit key_S4=P2^2; //Connect S4
sbit key_S5=P2^1; //Connect S5
sbit key_S6=P2^0; //Connect S6
sbit FM="P1"^5; //Buzzer interface
sbit EN="P1"^0;
sbit RS="P1"^1;
char sec,min,num,TIme,sec_24s;
float hpoint,rpoint;
uchar code table1[]={"H.T 000:000 R.T "};
//HT (home team) represents the home team, RT (road team) represents the away team, and the score is in the middle
uchar code table2[]={"12:00 SEC-1 24"};
//SEC-X represents the Xth quarter of the game. The left side is the countdown of a single quarter and the right side is the 24-second countdown.
/***1ms delay subroutine***/
void delay(int z)
{
int x;
flying y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/***LCD write command***/
void write_com(uchar com)
{
RS=0;
P0=with;
delay(5);
EN=1;
delay(5);
EN=0;
}
/***LCD write data***/
void write_dat(uchar dat)
{
RS=1;
P0=that;
delay(5);
EN=1;
delay(5);
EN=0;
}
/***Initialization program***/
void init(void)
{
flying i;
hpoint=0; //HT score initialization
rpoint=0; //RT score initialization
TIme=0;
TMOD=0x10; //Timer 1 initialization
TL1=0x00;
TH1=0x4c;
EA=1; //Enable total interrupt
ET1=1; //Open timer 1
TR1=0; //Timer 1 does not work
EX0=1; //Enable interrupt 0
EX1=1; //Enable interrupt 1
IT0=1; //Interrupt 0 is edge triggered
IT1=1; //Interrupt 1 is edge triggered
EN=0;
sec=0;
min=12;
num=1;
sec_24s=24;
write_com(0x38); //LCD setting initialization
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80); //LCD display initialization
for(i=0;i<16;i++)
{
write_dat(table1[i]);
}
write_com(0x80+0x40);
for(i=0;i<16;i++)
{
write_dat(table2[i]);
}
}
/***LCD score update***/
void point_lcd(float add, float dat)
{
write_com(0x80+add);
write_dat(0x30+dat/100);
write_dat(0x30+(dat%100)/10);
write_dat(0x30+dat%10);
}
/***Key detection***/
void keyscan(void)
{
key=0;
if(key_S3==0) //S3 presses HT score plus one
{
hpoint++;
point_lcd(0x04,hpoint); //Score display update
if(key_S3==0) //Let go detection
{
while(key_S3==0);
delay(20);
}
}
else if(key_S4==0) //S4 presses HT score minus one
{
hpoint--;
point_lcd(0x04,hpoint);
if(key_S4==0)
{
while(key_S4==0);
delay(20);
}
}
else if(key_S5==0) //S5 presses RT score plus one
{
rpoint++;
point_lcd(0x08,rpoint);
if(key_S5==0)
{
while(key_S5==0);
delay(20);
}
}
else if(key_S6==0) //S6 presses RT score minus one
{
rpoint--;
point_lcd(0x08,rpoint);
if(key_S6==0)
{
while(key_S6==0);
delay(20);
}
}
}
/***Game countdown/24s countdown (synchronized)***/
void counter_down(void)
{
flying i;
if(time>=20) //decrease the countdown by one every 1s
{
sec--;
sec_24s--;
write_com(0x80+0x4e); //24s countdown display
write_dat(0x30+sec_24s/10);
write_dat(0x30+sec_24s%10);
if(sec_24s==0) //After 24s, a 3s continuous alarm will be issued
{
FM="0";
delay(3000);
FM="1";
sec_24s=24;
}
if((sec==0)&&(min==0)) //Check whether a game is over
{
TR1=0; //Timer 1 pauses
write_com(0x80+0x44);
write_dat(0x30);
num++;
sec_24s=24; //24s timer reset
write_com(0x80+0x4e); //24s countdown display
write_dat(0x30+sec_24s/10);
write_dat(0x30+sec_24s%10);
if(num<5) //The buzzer will sound an 8s intermittent alarm at the end of each section
{
for(i=80;i>0;i--)
{
FM="0";
delay(500);
FM="1";
delay(500);
}
}
if(num==5) //At the end of the game, the buzzer will sound a continuous alarm sound for 10 seconds.
{
FM="0";
delay(10000);
num="1";
}
FM="1"; //buzzer off
write_com(0x80+0x4b); //Update "SEC-?"
write_dat(0x30+num);
sec="0"; //Countdown reset
min="12";
}
if(sec==-1)
{
sec="59";
min--;
}
write_com(0x80+0x40); //Update countdown display
write_dat(0x30+min/10);
write_dat(0x30+min%10);
write_com(0x80+0x43);
write_dat(0x30+sec/10);
write_dat(0x30+sec%10);
TIme=0;
}
}
/***Main program***/
void main()
{
heat();
while(1)
{
keyscan(); //Score key detection
}
}
/***S1 key interrupt 0***/
void exter0() interrupt 0 //Game time starts/pauses
{
TR1=~TR1; //Timer 1 works/pauses
if(TR1==1) //When the countdown is working, S1 presses the timer to stop working immediately
{
PT1=0;
}
else //When the countdown does not work, the countdown will work immediately after pressing S1
{
PT1=1;
}
if(S1==0) //Let go detection
{
while(S1==0)
{
counter_down();
}
delay(20);
}
}
/***S2 key interrupt 1***/
void exter1() interrupt 2 //24s countdown restarts
{
sec_24s=24;
write_com(0x80+0x4e); //24s countdown display
write_dat(0x30+sec_24s/10);
write_dat(0x30+sec_24s%10);
if(S2==0) //Let go detection
{
while(S2==0)
{
counter_down();
}
delay(20);
}
}
/***Timer 1 interrupt***/
void TImer1() interrupt 3 //Timer 1 interrupts 20 times for 1s
{
time++;
TL1=0x00;
TH1=0x4c;
counter_down(); //Countdown
}
Previous article:How does 51 microcontroller drive a DC motor in C language?
Next article:Design of digital current and voltage meter based on STC89C52 microcontroller
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- 【Review SGP40】-SGP40 test
- Does anyone know where to buy Hirose GT43 series connectors?
- Steps to create a register-based STM32 project in Keil5
- What is a Level 2 or Level 3 charger?
- Thank you for being there, thank you gift activity summary
- IMX6ULL development board apt-get software download tool
- 2019 National TI Sponsored Board Video Tutorials
- DIY Wooden Computer Case
- Switching Power Supply Reference Book, "Switching Power Converters: Principles, Simulation, and Design of Switching Power Supplies"
- Today's 10:00 AM broadcast | MPS inductor solutions help better switching power supply design