#include
* Customize Macro
//The encoding is reverse encoding
#define CLEAR 0x7f //Define the complement of clear
#define LED_BEGIN 0x01 // Define the display of the digital tube at the beginning
#define LED_FOUL 0x38 // Display the letter "F" after a foul, digital tube encoding
#define LED_C 0x31 // The encoding of the letter "C"
#define LED_L 0x71 // The encoding of the letter "L", two are used to display "CL" after the host cancels --cancel
#define GET 1 // This is mixed as a parameter of a function, which means successful answer.
#define FOUL 0 // Mixed with the above parameters, foul --- the usage of these two will be reflected later
#define READY 0x7e
//The following is the command sent to the host computer, corresponding to cmdID
#define _STRING_READY_ 9//Adjust the answer countdown and prepare to start the answer
#define _STRING_START_ 8//The countdown ends and the answering begins
#define _STRING_CANCEL_ 7//Cancel answering
#define _CHANGE_TIME_ 6//Read seconds each time
//Send commands 1--4 to represent the serial number of the answering terminal
//Because a 12M crystal oscillator is used on the board, and an 11.0529M crystal oscillator is used during simulation. In order to facilitate compilation at different times, this is combined with the subsequent conditional compilation to make it easier for the program to modify parameters.
//#define CLOCK_FREQUENCY_12M 1
/******************************************************************
* Custom data type
*******************************************************************/
typedef unsigned char Byte; // One byte
typedef unsigned int Word; // One word, two bytes
typedef bit Bool; // Imitate Boolean variables
//typedef sbit Port; // I wanted to use a custom port type variable, which is more convenient, but I don’t know why it can’t be compiled.
/******************************************************************
* Define MAX7219 register
*******************************************************************/
#define REG_NO_OP 0x00 //Define no operation register
#define DIG_1 0x01 //Define digital tube 1 register
#define DIG_2 0x02 //Define digital tube 2 register
#define DIG_3 0x03 //Define digital tube 3 register
#define DIG_4 0x04 //Define digital tube 4 register
#define DIG_5 0x05 //Define digital tube 5 register
#define DIG_6 0x06 //Define digital tube 6 register
#define DIG_7 0x07 //Define digital tube 7 register
#define DIG_8 0x08 //Define digital tube 8 register
#define REG_DECODE 0x09 //Define decoding control register
#define REG_INTENSITY 0x0a //Define display brightness register
#define REG_SCAN_LIMIT 0x0b //Define scan limit register
#define REG_SHUTDOWN 0x0c // Define "shutdown" mode register
#define REG_DISPLAY_TEST 0x0f // Define "display test" mode register
#define INTENSITY_MIN 0x00 //Define the minimum display brightness
#define INTENSITY_MAX 0x0f //Define the maximum display brightness
/*********************************************************************
* Define hardware pin connections
**********************************************************************/
sbit DATA=P2^0; //Data port of MAX7219
sbit LOAD=P2^1; //Latch port of MAX7219
sbit CLK=P2^2; //clock port of MAX7219
sbit HOST_START=P0^0; //Host button, the button used to restart start
sbit HOST_CANCEL=P0^1; //Clear button used by the host to cancel answering questions
sbit SWITCH1_3=P1^4; // DIP switch to adjust the countdown time. The number before the underline represents the serial number of the switch, and the number after the underline represents the value of the switch.
sbit SWITCH2_2=P1^5; // Same as above
sbit SWITCH3_2=P1^6; // Same as above
sbit SWITCH4_1=P1^7; // Same as above
sbit BEEP=P0^7; //Define buzzer port
#ifdef USE_SOUND //You can choose whether to use the sound during simulation through define
sbit LS138_C=P2^4; //Define the decoder input
sbit LS138_B=P2^5; //Same as above
sbit LS138_A=P2^6; //Same as above
sbit LS138_E1=P2^7; //Define the decoder enable end
#endif
/*********************************************************************
* Define global variables
**********************************************************************/
Byte data intrCounter; // Number of timer interruptions
Byte data beginNum; // Start countdown time
Byte data counterBack; // Put the number of interruptions in it for later use
Byte data showNum; // The time the digital tube is displaying
Bool data isStart; // Whether to start answering questions
Bool data isFoul; // Whether there is a foul
Bool data isPressed; // Whether there is a key to answer the question is pressed
Byte data number_temp; // A variable used to record the last status of P1 port
Bool data needResetTimes;//Record whether the number of overflows of Timer0 needs to be reset
code unsigned char C51BOX2[3] _at_ 0x43; //Prevent the program from running away when using C51Box
/***********************************************************************
* Common cathode seven-segment digital tube display corresponding segment lookup table (numbers 0-9 correspond to code_table [0]-[9] respectively)
***********************************************************************/
Byte code code_table_zheng[10]=
{0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70,0x7f,0x7b};
Byte code code_table[10]=
{0x01,0x4f,0x12,0x06,0x4c,0x24,0x20,0x0f,0x00,0x04};
/***********************************************************************
* Function declaration
***********************************************************************/
void MAX7219_SendByte (Byte dataout);
void MAX7219_Write (Byte reg_number, Byte dataout);
void MAX7219_DisplayChar(Byte digit, Byte character);
void MAX7219_Clear (void);
void MAX7219_SetBrightness (Byte brightness);
void MAX7219_DisplayTestStart (void);
void MAX7219_DisplayTestStop (void);
void MAX7219_ShutdownStart (void);
void MAX7219_ShutdownStop (void);
void MAX7219_Init (void);
void Delay10ms(void);
Bool GetHostStartKey (void);
Bool GetHostCancelKey (void);
void GetCounter(void);
Byte GetPressed(Byte KeyState);
Byte GetPressedWireless(Byte KeyState);
void IT0_Init(void);
void Timer0_Overflow();
void PressedHandle(Byte keyPressed);
void GetOrFoulHandle(Bool state);
void CancelHandle();
void SPEAKER_count (void); //Declare the countdown sound function
void SPEAKER_start(void); //Declare the start answering sound function
void SPEAKER_get(void); //Declare the sound function
void SPEAKER_foul(void); // Declare the foul sound function
void initialSerial();
void sendNumber(int number);//The serial port sends a number, what is sent here is the CommandID
void sendString(unsigned char *string);//Send string through serial port
/***********************************************************************
* MAX7219_SendByte()
*
* Description: Transfer one byte of data to MAX7219
* Arguments : dataout = data to send
* Returns : none
*************************************************************************/
void MAX7219_SendByte (Byte dataout)
{
Change in;
for (i=8;i》0;i--)
{
Byte mask=1《《(i-1);//mask is a mask, used to take bits
CLK=0;//The bit input of MAX7219 is before the rising edge of the clock, so it must become low level before each bit is sent.
if (dataout&mask)
DATA=1;
else
DATA=0;
CLK=1;//After all eight bits are transferred, they become high level and latched
}
}
/***********************************************************************
* MAX7219_Write()
*
* Description: Write commands to MAX7219
* Arguments : reg_number = register to write to
* dataout = data to write to MAX7219
* Returns : none
Unfinished~
***************************************************************************/
void MAX7219_Write (Byte reg_number, Byte dataout)
{
LOAD=0;//It is also before the rising edge of the latch. It must become low level before sending these two bytes.
MAX7219_SendByte(reg_number); //Send register address
MAX7219_SendByte(dataout);//Send data
LOAD=1;//turn to high level, latch
}
/**************************************************************************
* MAX7219_DisplayChar()
*
* Description: Make a certain digit display a number
* Arguments : digit = digit number (0-7)
* character = character to display (0-9, A-Z)
* Returns : none
void MAX7219_DisplayChar(Byte digit, Byte character)
{
MAX7219_Write(digit, character);
}
* MAX7219_Clear()
* Description: Clear the display of all bits
* Arguments : none
* Returns : none
Previous article:Design of step angle subdivision system for stepper motor based on AT89CC51 microcontroller and TA8435 chip
Next article:51 Basic concepts of microcontroller serial communication
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- msp430 to tft
- [A-Current Signal Detection Device] Shaanxi Province First Prize_Topic A
- Log-Periodic Antenna
- [TI Recommended Course] #TI Robot System Learning Kit (TI-RSLK) Upgraded Version#
- Why does the bandpass filter output such a waveform?
- Understanding mobile phone fast charging technology in one article
- Temperature and humidity program based on msp430f5529
- Discussion on interoperability issues of IoT devices
- Module for measuring blood pressure and heart rate
- How to improve battery safety while increasing accuracy and runtime