How can I use a computer to send data to a single-chip computer and how can the single-chip computer receive data?
Jp3 is connected to P0 port. Send a hexadecimal data from the computer to the microcontroller, and then view the digital display.
# Include <reg51.h>/** send a hexadecimal data to the microcontroller to display the digital tube */void initser (); void main () {<span> </span> initser ();
While (1) {// query the RI value. If it is 1, the data is received and the software should be cleared. Ri = 0if (Ri = 1) {rI = 0; // obtain the data sent from the computer from the buffer and assign it to p0p0 = sbuf ;}}// initialization function void initser () {// enable the timer T1 to work in Mode 2 tmod = 0x20; // assign the value of Th1 = 0xfd; TL1 = 0xfd; // the serial port is 1 sm0 = 0; SM1 = 1; // allow serial port accept Ren = 1; // start the timer tr1 = 1; p0 = 0xc0 ;}
Steps for serial communication query:
1 --- determine how the timer T1 works
2 --- assign values to the two registers of the timer
3 --- start timer T1
4 --- determine how the serial port works
5 --- if the microcontroller needs to obtain data from the computer, it should make Ren = 1
6 --- query the RI value. If it is 1, the data is received. Note that the software should set Ri to 0 immediately.
MCU Lesson 11th: Serial Communication query method --- receiving data from a computer