51 single-chip basketball scorer

foreword

A small program written by hand, after verification, the main function is to control the scores of AB and AB teams through buttons, suitable for beginners to learn.

1. Software Design Ideas

First define the design requirements. The basketball scorer needs to display the scores of the two teams A and B on the scoreboard. The blogger designed it on the 51 single-chip microcomputer development board of Puzhong Technology. Subtraction and Team B score addition and subtraction. Also need to use the resources of the digital tube for display. By selecting the chip selection signal of the nixie tube, the score of team A is displayed on the left four nixie tubes, and the score of team B is displayed on the right four nixie tubes. Fixed A characters and B characters are assigned values ​​through specific keys.

Two, the code

After clarifying the design idea, the next step is to directly upload the code

At the beginning, the variables corresponding to several buttons are defined, and there are also three chip selection signals of the digital tube. Two u8 variables data1 and data2 are defined, representing the scores of Team A and Team B respectively (it is believed that it is difficult for basketball to score more than 255 points). The smgduan function represents the digital tube signals corresponding to the characters displaying 0-F. Here you need to judge according to your own digital tube model. Common cathode and common anode are different display methods.

#include "reg52.h" //This file defines some special function registers of the microcontroller

typedef unsigned int u16;	  //Declare and define the data type u16
typedef unsigned char u8;   //Declare and define the data type u8

sbit LSA=P2^2;              //Control the LSA of digital tube chip selection
sbit LSB=P2^3;              //Control the LSB of the chip selection of the digital tube
sbit LSC=P2^4;              //Control the LSB of the chip selection of the digital tube
sbit k1=P3^0;	              //Define P30 port as k1
sbit k2=P3^1;               //Define P31 port as k2
sbit k3=P3^2;               //Define P32 port as k3
sbit k4=P3^3;               //Define P33 port as k4
u8 data1=0;                 //Define the count value of port B
u8 data2=0;                 //Define the count value of port B
u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
					0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//Display the value of 0~F

The delay function and key processing function are also very simple, and they are all modified on the basis of routines. In the routine of button processing, by judging whether the level corresponding to the button is high or low, the scores displayed by A and B are added or subtracted. delay(1000) here is to eliminate the vibration, because the mechanical keys will have a momentary vibration before they can stabilize the state.

/*******************************************************************************
* Function name: delay
* Function function: delay function, when i=1, the delay is about 10us
*******************************************************************************/
void delay(u16 i)
{
	while(i--);	
}
/*******************************************************************************
* Function name: keypros
* Function function: delay function, when i=1, the delay is about 10us
*******************************************************************************/
void keypros()
{
	if(k1==0)		  //Detect whether the button K1 is pressed
	{	
		delay(1000);   //Eliminate jitter Generally about 10ms
		if(k1==0)	 //Determine whether the button is pressed again
		{
			data1+=1;//Increment the counter value of B by 1
		}
		while(!k1);	 //Check if the key is released
	}	
	else if(k2==0)		  //Detect whether the button K2 is pressed
	{	
		delay(1000);   //Eliminate jitter Generally about 10ms
		if(k2==0)	 //Determine whether the button is pressed again
		{
			data1-=1;//The counter value of B is decremented by 1
		}
		while(!k2);	 //Check if the key is released
	}
	else if(k3==0)		  //Detect whether the button K3 is pressed
	{	
		delay(1000);   //Eliminate jitter Generally about 10ms
		if(k3==0)	 //Determine whether the button is pressed again
		{
			data2+=1;//Increment the count value of A by 1
		}
		while(!k3);	 //Check if the key is released
	}
	else if(k4==0)		  //Detect whether the button K4 is pressed
	{	
		delay(1000);   //Eliminate jitter Generally about 10ms
		if(k4==0)	 //Determine whether the button is pressed again
		{
			data2-=1;//The count value of A is decremented by 1
		}
		while(!k4);	 //Check if the key is released
	}			
}

You need to learn the meaning of the nixie tube scanning, then you can freely set the displayed content. Because the nixie tube will have a developing effect, that is, even if the light is turned off, there will be a short-lived residual visual effect in the human eye. Therefore, through rapid switching in a short period of time, there will be a phenomenon of simultaneous light emission in human eyes. The judgment condition i of the switch statement is to feel the value through the for loop. From 0 to 7, it corresponds to different digital tube display units. LSA, LSB, and LSC are actually the display ports of their channels, which have been designed by the chip inside the digital tube. Yes, you only need to choose different interfaces to get the display of different digital tubes. Since the display input unit of the digital tube corresponds to the P0 port on the single-chip microcomputer, different display conditions can be obtained by assigning different values ​​to the IO port on the P0. Then in the previous smgduan array, the corresponding code value has been set, and it can be called directly. For example, smgduan[0] corresponds to displaying 0, and so on.

/*******************************************************************************
* Function name: DigDisplay
* Function function: digital tube dynamic scanning function, cycle scanning 8 digital tube display
*******************************************************************************/
void DigDisplay()
{
	u8 i;
	for(i=0;i<8;i++)
	{
		switch(i)	 //Bit selection, select the illuminated digital tube,
		{
			case(0):
				LSA=0;LSB=0;LSC=0;P0=smgduan[data1%100%10];break;//Send the segment code, the first nixie tube displays the counting unit value of port B
			case(1):
				LSA=1;LSB=0;LSC=0;P0=smgduan[data1/10%10];break;//Send the segment code, and the second nixie tube will display the tens digit value of port B
			case(2):
				LSA=0;LSB=1;LSC=0;P0=smgduan[data1/100];break;//Send the segment code, the third nixie tube displays the counting unit value of port B
			case(3):
				LSA=1;LSB=1;LSC=0;P0=smgduan[11];break;//Send segment code, display B letter
			case(4):
				LSA=0;LSB=0;LSC=1;P0=smgduan[data2%100%10];break;//Send the segment code, and the fifth digital tube will display the counting unit value of port A
			case(5):            
				LSA=1;LSB=0;LSC=1;P0=smgduan[data2/10%10];break;//Send the segment code, the sixth nixie tube displays the counting tens digit value of port A
			case(6):            
				LSA=0;LSB=1;LSC=1;P0=smgduan[data2/100];break;//Send the segment code, the seventh nixie tube will display the counting hundreds digit value of port A
			case(7):            
				LSA=1;LSB=1;LSC=1;P0=smgduan[10];break;//Send segment code, display A letter
		}		
		delay(100); //scan at intervals	
		P0=0x00;//Blanking
	}
}

Finally, it can be called in the main function

/*******************************************************************************
* Function name: main
* Function function: main function
* Input: None
* output: none
*******************************************************************************/
void main()
{	
	while(1)
	{	
		DigDisplay();  //Nixie tube display function
    keypros();	   //Button digital display function	
	}		
}

Summarize

A journey of a thousand miles begins with a single step. Writing code by hand is the most practical way to improve your strength. You must understand what you are doing, how to solve this problem, and focus on the project itself with sufficient concentration. Come on, it is also a spur to myself.

Tags: stm32 Single-Chip Microcomputer 8051 microcontroller

Posted by Erkilite on Tue, 24 Jan 2023 01:08:30 +1030