The main function of the following program is to send a letter to the 51 single-chip microcomputer through the serial port debugging assistant. After the single-chip microcomputer receives it, it displays the letter on the digital tube and displays the letter in reverse in the interface of the serial port debugging assistant.
A series of special function registers such as serial port buffer have been defined in the header file of 51 single chip microcomputer. You can find the following definition by opening the header file:
sfr SCON = 0x98;
sfr SBUF = 0x99;
etc.
First, create a new header file and write the variable and function declarations to be used in the program:
#include#include #define uchar unsigned char sbit dula=P2^6; sbit wela=P2^7; //Letters sent from the computer to the microcontroller uchar letter; //The table array stores the codes corresponding to the digital tube display 0~F //Only A~F are used here uchar code table[]={ 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71}; void Inital(); void Display(uchar mydata); void Putchar(uchar mydata); void ShortDelay();
The main purpose of including the header file
The main function is very simple. It first initializes and then enters a loop that is always true, waiting for the interrupt to occur and processing it.
void main() { Inital(); // Initialization while(1) { } }
The initialization function is used to enable interrupts, set timers, serial communication working mode, and variable initialization:
void Inital() { // Clear the variable letter=0; wela=0; dula=0; //Set the baud rate to 9600 SCON=0x50; TMOD=0x20; TH1=0xfd; TL1=0xfd; TR1=1; //Open serial port interrupt EA=1; ES=1; }
The interrupt service function is called by the CPU when an interrupt occurs, and Display is called inside it. The Putchar function is used to display letters on the digital tube and echo the input letters on the computer.
//Interrupt service function void ser() interrupt 4 { //If you have finished reading the data if(RI==1) { RI=0; //software clear letter=SBUF; //Read data from the MCU Receive buffer Putchar(letter); //Echo letter on the computer Display(letter); //Display the corresponding letter on the digital tube } }
The Display function is only used in the previous article about digital tubes. Here is the code directly:
void Display(uchar mydata) { uchar BigLetter; BigLetter=toupper(mydata); //First convert all the letters read into uppercase so that they can be processed uniformly //Digital selector tube wela=1; P0=0xfb; wela=0; P0=0xff; //Segment selection //BigLetter-'A'+10 index is the subscript of the letter in the table dula=1; P0=table[BigLetter-'A'+10]; dula=0; ShortDelay(); }
void ShortDelay() { uchar a=100; while(a--); }
Putchar function to send information to the computer
void Putchar(uchar mydata) { SBUF=mydata; //Write data into the transfer buffer of the microcontroller while(!TI); //Is the sending finished? TI=0; //software clears after sending }
In this way, you can use the serial port debugging assistant to send letters to the microcontroller, display the letters on the digital tube, and display the content of the letters just sent on the software interface.
Previous article:51 MCU and PC serial port asynchronous communication
Next article:51 MCU simulated serial port code
- Popular Resources
- Popular amplifiers
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
- P22-009_Butterfly E3106 Cord Board Solution
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- 【TI recommended course】#What is I2C design tool? #
- STM32 low power mode
- [Xianji HPM6750 Review] Testing CAN Transceiver
- MSP430F5529 clock and FLL configuration
- What is the difference between TMS320C6416 and TMS320C6416T?
- [McQueen Trial] The second post is delayed - Adjusting the basic input and output peripherals on the car
- Thank you for having you + all the people and things that helped me
- Excellent electronic design and product design materials 1
- [GD32L233C-START Review] Part 1: Any surprises after unboxing?
- FAQ_About BlueNRG-2 port retention in low power mode