It feels good to use a computer to control a microcontroller, and then control other peripherals such as relays. How to achieve it? Let's do it step by step.
To realize the microcontroller serial port communication, that is, the communication between the microcontroller and the computer, you need to understand some basic concepts.
(1) Before implementation, please make sure that the serial port is normal. I have done serial port operation before, but it burned and could not be controlled. Later I found that there was an exclamation mark on the serial port in the device manager. There was a problem with the driver. It may be that the development experiment board is not plugged in properly or the USB port is changed (I use a serial port to USB converter for laptop)
(2) Serial port initialization. In this regard, it is best to have a related book to understand timers and interrupts, so that the operation will be easy.
1. Set the working mode of the serial port: set the SCON register.
What is SCON?
The SCON register is an addressable special register of the 51 microcontroller, used for serial data communication control, its byte address is: 98H, bit address: 98H~9FH. The content is:
SM0 | SM1 | SM2 | REN | TB8 | RB8 | TI | RI |
SM0 SM1: Serial port working mode selection bit.
SM0SM1 Working Mode Function Baud Rate
00 Mode 0 8-bit synchronous shift register fosc/12
01 Mode 1 10-bit UART Variable
10 Mode 2 11-bit UART fosc/64 (or fosc/32)
11 Mode 3 11-bit UART Variable
-------------------------------------------------- ----------
SM2: Multi-machine communication control bit
REN: Receive enable control, 1 means allowing serial port reception, 0 means prohibiting serial port reception.
TB8: The 9th data bit sent.
RB8: The 9th data bit received.
TI: Transmit interrupt flag. After sending a frame of data, the hardware automatically sets it to 1. The TI bit must be cleared by software.
RI: Receive interrupt flag. After receiving a frame of data, the hardware automatically sets it to 1. The RI bit must be cleared by software.
Example: Use the serial port debugging assistant to debug the serial port and send hexadecimal numbers to make the corresponding LED light up.
The C program is as follows:
#include
void main()
{
TMOD=0x20; //Set timer: Timer1 Mode 2 8-bit reload
TH1=0xfd; //Baud rate for 9600 baud @ 11.0592MHz
TL1=0xfd;
SM0=0;
SM1=1;
REN=1; //Control RI to allow serial reception. SM0SM1 and REN are SCON=0x50;
TR1=1; //Start timer 1
while(1)
{
if(RI==1) //If RI is 1, the data sent by the serial port is received
{
RI=0; Library //Software clear. Must
P0=SBUF; //SBUF is a buffer. The receiving buffer can only be read but not written, and the sending buffer can only be written but not read.
/* The two share the same byte address and are independent of each other. */
}
}
}
The above is the serial control LED light. If you want to display which lights, you can send them through the serial port debugging software.
Example: Digital tube display. Here only one digit is shown. If there are multiple digits, you can add related program segments.
#include
void main()
{
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
SM0=0;
SM1=1;
REN=1; //Control RI
TR1=1;
while(1)
{
if(RI==1)
{
RI=0;
P0=SBUF; //P0 is the value of the digital tube.
P2=0x06; //8-bit digital tube, the 7th one, P2 port is chip select,
}
}
}
The above program can be debugged and run on AT89s52 microcontroller + Keil.
Related tools:
Serial port debugging assistant, 51 serial port communication calculator.exe, can be searched and downloaded online.
Previous article:LCD 12864 Chinese character display program and C language program with normal display order
Next article:MCS-51 core based assembly learning notes
- Popular Resources
- Popular amplifiers
- Multi-channel pressure measuring instrument based on C8051F020 single chip microcomputer
- Design of automatic player for work and rest signal based on 51 single chip microcomputer
- tlc1549 pdf datasheet (10-BIT ANALOG-TO-DIGITAL CONVERTERS)
- MTK Solution Software Multi-channel Download Platform Operation Instructions
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
- Analog Multiplexer and Switch Configuration
- Build a "core" building together: front-line engineers talk about national chips, valid all year round (11 floors have been built)
- LM339 quad op amp IC exploded a hole 0.0
- Principle of MCU reset
- Regarding the C216 issue, I want to make a combination lock, and I'm a newbie looking for advice from a master! !
- The use of SRAM in HPM6750 and the problems encountered
- Oscilloscope automobile turbocharger solenoid valve waveform and analysis
- Transformer voltage calculation
- Failure to send data in broadcast mode
- EEWORLD University - IGBT module technology, drive and application