Serial communication example of 51 single chip microcomputer

Publisher:TranquilVibesLatest update time:2021-02-03 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Among them, fOSC is the clock frequency of the microcontroller; the baud rate refers to the number of bits sent (or received) per second by the serial port.


SM2: Multi-machine communication control bit. This is only used for multi-machine communication in mode 2 and mode 3. The transmitter SM2 = 1 (program control setting is required). The serial port of the receiver works in mode 2 or 3. When SM2 = 1, only when the 9th data bit (RB8) is 1, the first 8 bits of data received are sent to SBUF, and RI is set to issue an interrupt request to trigger a serial reception interrupt, otherwise the received data will be abandoned. When SM2 = 0, regardless of whether the data bit is 0 or 1, the data will be sent to SBUF, and RI will be set to issue an interrupt request. When working in mode 0, SM2 must be 0.


REN: Serial receive enable bit: When REN = 0, reception is prohibited; when REN = 1, reception is allowed.


TB8: In modes 2 and 3, TB8 is the 9th bit of data to be sent by the transmitter. In multi-machine communication, it represents the address or data to be transmitted. When TB8=0, it is data, and when TB8=1, it is address.


RB8: In methods 2 and 3, RB8 is the 9th bit of data received by the receiver, which happens to come from TB8 of the transmitter, thereby identifying the characteristics of the received data.


TI: Serial port sends interrupt request flag. When the CPU has sent a serial data, the SBUF register is empty, and the hardware sets TI to 1 to request an interrupt. After the CPU responds to the interrupt, the software clears TI.


RI: Serial port receive interrupt request flag. When the serial port receives a frame of serial data, the SBUF register is full, and the hardware sets RI to 1 to request an interrupt. After the CPU responds to the interrupt, the software clears RI.


Power control register PCON (see Table 3).

Table 3 PCON register


The meanings of each bit in the table (from left to right, from high to low) are as follows.


SMOD: Baud rate doubling bit. SMOD=1, when the serial port works in mode 1, 2, 3, the baud rate is doubled. SMOD=0, the baud rate remains unchanged.


GF1, GF0: general flag bits.


PD (PCON.1): Power-down mode bit. When PD=1, it enters power-down mode.


IDL (PCON.0): Standby mode bit. When IDL=1, enter the standby mode.


Other registers related to the serial port include the timer-related registers and interrupt registers described in the previous article. The timer register is used to set the baud rate. The ES bit in the interrupt enable register IE is also used as the serial I/O interrupt enable bit. When ES = 1, serial I/O interrupts are enabled; when ES = 0, serial I/O interrupts are disabled. The PS bit in the interrupt priority register IP is used as the serial I/O interrupt priority control bit. When PS = 1, it is set to high priority; when PS = 0, it is set to low priority.


Baud rate calculation: After understanding the serial port related registers, we can draw some conclusions about its communication baud rate:


① The baud rates of Mode 0 and Mode 2 are fixed.


In mode 0, the baud rate is 1/12 of the clock frequency, that is, fOSC/12, and is fixed.


In mode 2, the baud rate depends on the SMOD value in PCON, that is, the baud rate is:



When SMOD=0, the baud rate is fosc/64; when SMOD=1, the baud rate is fosc/32.


② The baud rate of mode 1 and mode 3 is variable and is determined by the overflow rate of timer 1.



When timer T1 is used as a baud rate generator, the working mode 2 of automatic reloading of the timing initial value is usually selected (Note: do not confuse the working mode of the timer with the working mode of the serial port). Its counting structure is 8 bits. Assuming that the initial count value is Count and the machine cycle of the microcontroller is T, the timing time is (256?Count)×T. Therefore, the number of overflows within 1s (i.e., the overflow rate) can be shown by formula (1):

Therefore, the baud rate calculation formula is shown in formula (2):


In practical applications, the baud rate is usually determined first, and then the initial value of T1 is calculated based on the baud rate. Therefore, equation (2) can be written as:


2. Circuit Detail


The circuit shown in Figure 1 is described in detail below.


Figure 1 Serial communication experiment circuit diagram


The minimum system part (clock circuit, reset circuit, etc.) has been discussed in the first lecture, so it will not be described here. Let's focus on the RS-232 interface circuit for communicating with the computer. As you can see, in the circuit diagram, there are two receiving and sending indicator status lights, TXD and RXD. In addition, a chip called MAX3232 is used. What is it used to achieve? First of all, we must know that the serial port on the computer is a serial interface with the RS-232 standard, and the RS-232 standard defines its electrical characteristics: the high-level "1" signal voltage range is -15V~-3V, and the low-level "0" is 0.


The signal voltage ranges from +3V to +15V. Some readers may ask why it has such electrical characteristics? This is because high and low levels are represented by opposite voltages, with a voltage difference of at least 6V, which greatly improves the reliability of data transmission. Since the pin level of the microcontroller is TTL, the first thing to solve when the microcontroller communicates with the RS-232 standard serial port is the level conversion problem. Generally speaking, some professional integrated circuit chips can be selected, such as the MAX3232 in the figure. The MAX3232 chip integrates a voltage multiplier circuit, and the level conversion can be completed with a single power supply. It also has a wide operating voltage range and can work normally between 3V and 5.5V. Its typical application is shown in the figure. The capacitor connected to its periphery has an impact on the transmission rate. In the test kit, 0.1μF is used.


It is worth mentioning that the MAX3232 chip has two pairs of level conversion circuits. Only one circuit is used in the figure, so the other circuit is wasted. In some cases, the two circuits can be connected in parallel to obtain stronger driving anti-interference ability. In addition, it is necessary to understand the pin structure of the DB-9 type RS-232 connected to the computer in the figure (see Figure 2).


Figure 2 DB-9 connector interface diagram


The pin definitions are as follows (see Table 4).



Table 4 DB-9 interface pin definition


3. Programming


The design example program of this lecture is as follows:


#include "AT89X52.h" (1)


void Init_Com(void) ( 2)


{


TMOD = 0x20; ( 3)


PCON = 0x00; ( 4)


SCON = 0x50; ( 5)


TH1 = 0xE8; ( 6)


TL1 = 0xE8; ( 7)


TR1 = 1; ( 8)


}


void main(void) ( 9)


{


unsigned char dat; ( 10)


Init_Com(); ( 11)


while(1) ( 12)


Detailed description of the program:


(1) Header file inclusion.


(2) Declare the serial port initialization procedure.


(3) Set timer 1 to operate in mode 2 and automatically load the initial value (see Lecture 2 for details).


(4) When the SMOD bit is cleared to 0, the baud rate is not doubled.


(5) The serial port operates in mode 1 and allows reception.


(6) Initialize the high 8 bits of timer 1. The baud rate is 1200b/s (7) Initialize the low 8 bits of timer 1.


(8) Start the timer.


(9) Main function.


(10) Define a character variable.


(11) Initialize the serial port.


(12) Infinite loop.


(13) If data is received.


(14) Assign the received data to the previously defined variables.


(15) Output the received value to port P0.


(16) Clear the receive flag to prepare for receiving again.


(17) Send the received data out again.


(18) Check whether the sending is completed.


(19) Clear the send flag bit to 0.


4. Debugging points and experimental phenomena


After the hardware is connected, download the hex file generated by the program to the MCU through cold start, open the serial port debugging assistant software, set the baud rate to 1200, reset the MCU, and then send data to the MCU through the serial port debugging assistant (see Figure 3). You can observe that the sent data is displayed in the receiving window. In addition, the serial communication indicator on the circuit board will also flash, and the LED light connected to the P0 port will flash the received data.


Figure 3 Serial port software debugging interface


In addition, when using the serial port debugging assistant software, it should be noted that if the MCU development board adopts serial port download and the same serial port is used as the serial port debugging assistant, the program cannot be downloaded to the MCU while the serial port software is opened. If you need to download, please click "Close serial port" first. When doing the sending experiment, please note that if the hexadecimal is selected, the hexadecimal value of the number or letter is sent. For example, if "0" is sent, the actual received value should be 0x00. If it is not selected, the ASCII code value is sent by default. At this time, "0" is sent, and the actual received value should be 0x30. This can be indicated by observing the corresponding LED on the P0 port of the board.


V. Conclusion


This lecture introduces the principle of MCU serial communication and gives examples. Through this lecture, readers can understand and master the principle and application process of 51 MCU serial communication. MCU can be connected to computer, MCU can be interconnected or multiple MCUs can communicate with each other, etc., which is widely used in actual engineering applications. From the perspective of learning, proficient use of serial port to display relevant information in MCU system on computer can be very intuitive and convenient for debugging and development.

Reference address:Serial communication example of 51 single chip microcomputer

Previous article:Design of automatic fire detection and alarm system based on smoke detection
Next article:51 MCU common IO port simulation IIC (I2C) interface communication program code

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号