51 MCU Tutorial from Scratch—— 23 MCU Serial Port Communication Programming

Publisher:EnchantedMelodyLatest update time:2012-02-16 Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
MCU serial port communication program design examples and notes

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.

Click to browse the next page

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.

Click to browse the next page

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.

Keywords:MCU Reference address:51 MCU Tutorial from Scratch—— 23 MCU Serial Port Communication Programming

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

Research on LED display drive circuit in single chip computer system
In recent years, the single-chip microcomputer system has gradually penetrated into the practical engineering applications of various industries with its advantages of small size, powerful functions, flexible expansion and easy use. The LED display circuit is like the eyes of the single-chip microcomputer system, wh
[Microcontroller]
Research on LED display drive circuit in single chip computer system
51 MCU——I2C bus EEPROM
We can save the data in EEPROM so that the data will not be lost when the power is off. Commonly used serial bus protocols: Currently, the commonly used serial buses for data transmission between microcomputers and peripherals are mainly I2C bus, SPI bus and SCI bus. I2C bus: Communicates in a synchronous serial 2
[Microcontroller]
51 MCU——I2C bus EEPROM
The difference between 89c51 MCU and 89s51 MCU, and the summary of new functions of 89s51 MCU
AT89S51 is a low-power, high-performance CMOS 8-bit microcontroller. It contains 8k Bytes ISP (In-system programmable) Flash read-only program memory that can be repeatedly erased and written 1000 times. The device is manufactured using ATMEL's high-density, non-volatile storage technology and is compatible with the
[Microcontroller]
STM32 MCU (5) Timer interrupt experiment
/****************************************************** ****************************  *     * Software function: timer interrupt experiment  *   *************************************************** *******************************/   #include "stm32f10x.h"   #include "delay.h"         void RCC_Configuration(vo
[Microcontroller]
Teach you how to DIY music spectrum display using 51 single chip microcomputer
Whenever I see the rows of small lights on the speakers and amplifiers at home jumping like waves with the music being played, or when I open the music player software Qianqian Jingting on my computer and see the dynamic spectrum dancing gracefully with the rhythm of the music, I can't help but think about it. If I ca
[Power Management]
Teach you how to DIY music spectrum display using 51 single chip microcomputer
MCU Application System Hardware Circuit Design Reference
  The hardware circuit design of a single-chip microcomputer application system includes two parts: one is system expansion, that is, when the functional units inside the single-chip microcomputer, such as ROM, RAM, I/O, timer/counter, interrupt system, etc., cannot meet the requirements of the application system, they
[Microcontroller]
C51 MCU - Instruction System
[Microcontroller]
C51 MCU - Instruction System
Getting Started with PIC Microcontrollers
The first step is to download a development environment. There are two types of PIC development environments: MPLAB IDE and MPLAB X IDE. X requires JAVA to be installed, so it is recommended to install the old version first. The following is the link: http://www.microchip.com/stellen ... 9&part=SW007002. After open
[Microcontroller]
Getting Started with PIC Microcontrollers
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号