1. Serial port mode 0 application programming The serial port mode 0 of the 8051 microcontroller is a shift register mode. By connecting an external serial-input and parallel-output shift register, a parallel port can be expanded.
Example: Use the 8051 microcontroller serial port to connect CD4094 to expand the 8-bit parallel output port. As shown in the figure, each bit of the 8-bit parallel port is connected to a light-emitting diode, and the light-emitting diode is required to be in the state of a running light. The data transmission of serial port mode 0 can be interrupted or inquired. No matter which method is used, it must rely on the TI or RI flag. When sending serially, the interrupt request can be caused by setting TI (after sending a frame of data), and the next frame of data can be sent in the interrupt service program, or by querying the status of TI. As long as TI is 0, the query will continue, and TI will end the query when it is 1, and the next frame of data will be sent. When receiving serially, RI will cause an interrupt or query RI to determine when to receive the next frame of data. No matter which method is used, the control register SCON must be initialized before starting communication. In mode 0, 00H can be sent to SCON.
-----------------MCU serial port communication program design example-------------------------
ORG 2000H
START: MOV SCON,#00H; Set the serial port working mode to 0
MOV A,#80H; The highest position light will light up first
CLR P1.0 ; turn off parallel output (to avoid the "dark red" phenomenon of each LED during transmission)
OUT0: MOV SBUF,A; Start serial output
OUT1: JNB TI, OUT1; Output completed?
CLR TI; Finished, clear the TI flag to prepare for the next send
SETB P1.0 ; Open parallel port output
ACALL DELAY; delay for a period of time
RR A ; Circular right shift
CLR P1.0 ; turn off parallel output
JMP OUT0 ; loop
Note: The DELAY delay subroutine can use the delay subroutine we used when talking about the P1 port running light, so it will not be given here.
2. Serial port asynchronous communication
org 0000H
AJMP START
ORG 30H
START:
mov SP,#5fh ;
mov TMOD,#20h ;T1: working mode 2
mov PCON,#80h ;SMOD=1
mov TH1,#0FDH ; Initialize baud rate (see table)
mov SCON,#50h ;Standard UART settings
MOV R0,#0AAH; the number to be sent
SETB REN ; Allow to receive
SETB TR1 ;T1 starts working
WAIT:
MOV A,R0
CPL A
MOV R0,A
MOV SBUF,A
LCALL DELAY
JBC TI, WAIT1; If TI is equal to 1, clear TI and switch to WAIT1
AJMP WAIT
WAIT1: JBC RI, READ; if RI is equal to 1, clear RI and switch to READ
AJMP WAIT1
READ:
MOV A,SBUF; send the obtained data to port P1
MOV P1,A
LJMP WAIT
DELAY: ; Delay subroutine
MOV R7,#0ffH
DJNZ R7,$
RET
END
Compile the program, write it into the chip, insert it into the experimental board, and use the through-read cable to connect the experimental board to the serial port of the host to conduct the experiment. The function of the above program is very simple, that is, to send the numbers 55H and AAH to the host in turn at regular intervals, and send the numbers sent by the host to the P1 port. You can use the serial port wizard on the PC to do experiments. The serial port wizard is available for download on my homepage. After running the serial port wizard, press the "Set Parameters" button on the main interface to enter the "Set Parameters" dialog box and set the parameters according to the following. Note that my machine uses serial port 2. If you are not using serial port 2, please change the serial port settings yourself.
After setting, press OK to return to the main interface. Note that there is a drop-down list on the right, and "According to Hexadecimal" should be selected. Then press "Start Sending" and "Start Receiving". According to this setting, two lights should be on and six lights should be off on the experimental board. You can change the sending characters in the setting parameters such as 55, 00, FF, etc., observe the lights on and off, and analyze the reasons. You can also change the "According to Hexadecimal" in the drop-down list on the main interface to "According to Decimal" or "According to ASCII Characters" to observe the phenomenon and analyze it carefully. This is also very helpful for everyone to understand hexadecimal, decimal, and ASCII characters. The program itself is very simple and has comments, so I won't explain it in detail here.
3. Interrupted version of the above program
org 0000H
AJMP START
org 0023h
AJMP SERIAL ;
ORG 30H
START:
mov SP,#5fh ;
mov TMOD,#20h ;T1: working mode 2
mov PCON,#80h ;SMOD=1
mov TH1,#0FDH ; Initialize baud rate (see table)
mov SCON,#50h ;Standard UART settings
MOV R0,#0AAH; the number to be sent
SETB REN ; Allow to receive
SETB TR1 ;T1 starts working
SETB EA ; open the total interrupt
SETB ES ; Open serial port interrupt
SJMP$
SERIAL:
MOV A,SBUF
MOV P1,A
CLR RI
RETI
END
This program does not include a sending program, you can add it yourself.
Previous article:51 MCU Tutorial from Scratch—— 22 MCU Serial Port Introduction
Next article:51 MCU Tutorial from Scratch - 24 LED Digital Tube Static Display Interface and Programming
Recommended ReadingLatest update time:2024-11-16 17:43
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
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
- Free application: Qinheng RISC-V core BLE 5.3 wireless MCU CH582
- Electronic design from scratch
- Is this a filter circuit?
- Rohde & Schwarz JD flagship store is officially launched!
- Design of Universal Asynchronous Receiver and Transmitter Using VHDL
- [AT-START-F425 Review] Unboxing Review
- How difficult is it for Chang'e 5 to "skip in space" and re-enter the atmosphere?
- SHDSL Wired Analog Front-end Design
- MT6580 for beginners
- What do you think about RF company Qorvo providing programmable power for smart home appliances?