The function we want to achieve is to send a string such as "hello world" to the serial debugging assistant through the serial port of the microcontroller when the program is running. After the serial communication assistant receives the string, it will display it and the user will feedback a string of length 4 and display it on the digital tube.
Let's see the effect
The first step is to configure the serial communication circuit and set up the relevant serial ports with the help of VSPD and serial communication assistant.
Use VSPD to create two virtual serial ports that can communicate with each other, such as COM1 and COM2. After clicking the Add Port button, you can see the two newly created virtual ports in the Virtual Ports section on the left column.
Open the Serial Debug Assistant V2.2 and configure it. Change the serial port to COM2 port, set the baud rate to 9600, select NONE for the parity bit, 8 data bits, and 1 stop bit.
Configuration in the circuit diagram: First, change the crystal oscillator to 11.0592MHZ, and then change the crystal oscillator in the AT89C52 chip to 11.0592MHZ. Then configure the serial port, set the physical port to COM1, the physical baud rate to 9600, the physical data bit to 8 bits, and the physical parity bit to NONE. The following virtual baud rate, virtual data bit, and virtual parity bit are the same as above.
Next is the circuit diagram
Create two virtual ports using VSPD
You can use the device manager to check whether the serial port is created successfully.
AT89C52 configuration
Configuration of COMPIM
Some port settings
The hardware configuration is almost done. Next, let's see how to write code in Keil. Here are only some important code snippets. If you are interested, you can [download the code](http://download.csdn.net/detail/lrwwll/9720330)
/*Digital tube display function, using 74HC595 to achieve serial input and parallel output*/
void ser_inout(uchar datas)
{
uchar i; //loop control variable
STCP = 0; //P2.6 pin set to 0
for(i = 0 ; i < 8 ; i++) //for loop, transmit data from high to low
{
SHCP = 0; //P2.7 pin set to 0
if( (datas & 0x80) == 0) //If the highest bit of data is equal to 0
{
DS = 0; // pass data 0 to P2.5 pin
}
else
{
DS = 1; //If the highest bit data is not equal to 0, pass data 1 to P2.5 pin
}
datas <<= 1; //Shift the transmitted 8-bit data one bit to the left
SHCP = 1; //P2.7 pin is set to 1, generating a rising edge, and transferring the data on P2.5 pin to 74HC595
}
STCP = 1; //P2.6 is set to 1, a rising edge is generated, and eight bits of data are sent
}
/*Dynamic display function, digital tube dynamically displays data*/
void Display()
{
uint i = 0; //i is the loop control variable
while( i!=900 ) //while loop, loop 90 times, let the digital tube dynamically display data for about 1.5S
{
ser_inout(table[show[0]]); //Send the highest bit data to the digital tube
wx0 = 0; //Select the first digital tube, low level is valid
delay_ms(1); //delay 1ms
wx0 = 1; //Cancel bit selection
ser_inout(table[show[1]]); //Send the second bit of data to the digital tube
wx1 = 0; //Select the second digital tube, low level is valid
delay_ms(1); //delay 1ms
wx1 = 1; //Cancel bit selection
ser_inout(table[show[2]]); //Send the third digit to the digital tube
wx2 = 0; //Select the third digital tube, low level is valid
delay_ms(1); //delay 1ms
wx2 = 1; //Cancel bit selection
ser_inout(table[show[3]]); //Send the fourth digit to the digital tube
wx3 = 0; //Select the fourth digital tube, low level is valid
delay_ms(1); //delay 1ms
wx3 = 1; //Cancel bit selection
i++; //i+1
}
}
/*Output function, display the corresponding data through the digital tube*/
void printf(uchar dat[]) //dat stores the data to be displayed
{
uint i = 0; //loop control variable
for(i = 4;i > 0;i--) //Loop 4 times and store the data to be displayed in the show array
{
show[4-i]=dat[4-i];
}
Display(); //Call the dynamic display function to let the digital tube display data dynamically
}
/*Serial communication related initialization*/
void init()
{
TMOD=0X20; //Set timer 1 mode 2
TH1=0XFD; //Set the baud rate to 9600
TL1=0XFD; //same as above
SCON=0X50; //Set the serial port control register to serial port working mode 1 and enable interrupt to receive data
PCON&=0XEF; //Baud rate is not doubled
TR1=1; //Start timer 1
IE=0X0; //Disable any interrupt
}
/*MCU serial port sends a character*/
void send(uchar txd)
{
SBUF=txd;
while(!IF);
IF = 0;
}
/*MCU serial port sends a string*/
void send_buf(uchar dat[])
{
uint i=0;
while(dat[i]!='\0')
{
send(dat[i]);
i++;
}
}
Some explanations
SCON=0x50:
SCON is the serial port control register, 0x50 is hexadecimal, converted to binary is 01010000, corresponding to the following table, that is, the serial port control register is set to serial port working mode 1 and interrupt receiving data is allowed.
Appendix: scon register structure table
SCON SM0 SM1 SM2 REN TB8 RB8 TI RI
Bit address 9FH 9EH 8DH 9CH 9BH 9AH 99H 98H
TMOD=0x20: TMOD is the timer/counter mode control register, so TMOD=0x20 sets the timer/counter 1 to working mode 2.
TH1=0xE8,TL1=0xE8:
Set the initial value of timer/counter 1 to determine the timing duration, and the specific time is related to the crystal oscillator.
TI=1,TR=1:
From the above table, we can see that TI and RI are the lowest two bits of the SCON register. TI: Transmit interrupt flag, RI: Receive interrupt flag. TI=1 indicates that the frame transmission is completed, and RI=1 indicates that the frame reception is completed.
Previous article:Character Problems in Communication between PC Serial Debug Assistant and MCU
Next article:(C51 Learning Five) MCU and PC communicate through serial port
- 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
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- Based on ST FOC5.3 self-made IHM08 V3 board: non-sensing/sensing Hall/sensing encoder/torque mode and other reference programs/circuits...
- 【AT32WB415 Review】02 Clock Configuration + LED Operation
- Reading Notes of the Good Book "Electronic Engineer Self-Study Handbook" 01-Receiving & Title Page
- The main features of embedded C and the similarities and differences between embedded C and standard C
- I would like to ask what role does the 8-wire three-state bus transceiver play in the circuit?
- 【BearPi-HM Nano, play Hongmeng "Touch and Go"】-5-Compilation exception question①
- Impedance Matching of ADS1263
- Should I use a larger capacitor or a smaller capacitor first? --DC-DC power module output
- Based on PSOC6 development board simulation I2C solution X-NUCLEO-IKS01A3 HTS221
- 50 ways to use TI CC6678 digital signal processor (DSP)