Source program C code: basketball game application system

Publisher:荣耀使者Latest update time:2023-06-05 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  /*****************************************************************

  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

  }


Reference address:Source program C code: basketball game application system

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

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号