Research on the communication principle and circuit design between single chip microcomputer and PC

Publisher:暗里著迷Latest update time:2018-03-01 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Abstract: With the widespread application of single-chip microcomputer systems and the popularization of computer network technology, the communication function of single-chip microcomputers is becoming more and more important. Single-chip microcomputer communication refers to the information exchange between single-chip microcomputers and computers or between single-chip microcomputers. Usually, the communication between single-chip microcomputers and computers is more commonly used. Based on temperature detection data, this paper studies the communication principle and circuit design between single-chip microcomputers and PCs.


introduction


This paper studies a temperature detection data transceiver module based on serial port. Using DS18B20 temperature sensor to design temperature monitoring module, accurate to 0.1℃, using LCD to display current temperature, and then send instructions to single chip microcomputer through serial port debugging assistant. When single chip microcomputer receives hexadecimal instruction 01, it transmits the current temperature value back to PC for display at intervals of 1s, and PC displays Turn on temp; when single chip microcomputer receives hexadecimal instruction 02, it stops transmitting temperature value back, and PC displays Turn off temp; when single chip microcomputer receives other instructions, PC displays Error.


1 Overall Design


The functions of this system are coordinated by two major parts: hardware and software. The hardware part mainly completes the display of information; the software mainly completes the signal processing and control functions, etc.

      

The hardware of this system adopts modular design, with AT89C52 single-chip microcomputer as the core, LCD  display circuit, serial port communication circuit and DS18B20 temperature detection circuit to form a control system. The hardware of this system mainly includes the following modules:

      

AT89C52 main control module, LCD display module, serial port communication module, DS18B20 temperature detection module, etc. Among them, AT89C52 mainly completes the control of peripheral hardware and some calculation functions, the LCD display module completes the display function of characters and numbers, the serial port communication module mainly completes the communication function between the microcontroller and the PC, and the DS18B20 temperature detection module mainly completes the ambient temperature detection function. The system composition block diagram is shown in Figure 1.1.

Figure 1.1 System hardware block diagram

Figure 1.1 System hardware block diagram


The application software adopts modular design method. The system software is mainly composed of main program, serial port receiving and sending data interrupt subroutine, LCD display subroutine and other modules. The system software structure diagram is shown in Figure 1.2.

Fig. 1.2 System software design block diagram.

Fig. 1.2 System software design block diagram.


2 System Working Principle


When the serial port of the MCS-51 microcontroller sends/receives data, it is done through two serial buffers SBUF. These two buffers use the same address (98H), but are physically independent. The receiving buffer can only be read but not written, and the sending buffer can only be written but not read.


1. Sending process


When the data is written into the SBUF register, the microcontroller automatically starts to send data from the start bit. When it reaches the start of the stop bit, the internal hardware sets TI to 1 and requests an interrupt from the CPU. Then, the corresponding processing can be done in the interrupt service program, or you can choose not to enter the interrupt.


2. Receiving process


Whether the serial port receives or not is subject to the state of the receive enable bit REN. When REN is set to "1" by software, the receiver is allowed to receive. The serial port receiver monitors the RXD line at 16 times the selected baud rate. When the detector continuously samples the low level it is determined that the start bit appears at the RXD end, and then the receiving controller starts working. The RXD line is sampled in the 7th, 8th, and 9th pulse states of each bit transmission time to determine whether the received value is "0" or "1". After receiving the stop bit, the control circuit sets the interrupt flag R1 to "1".


3. Temperature detection


The temperature detection uses the latest single-wire digital temperature sensor DS18B20 from DALLAS. DS18B20 is a new type of "one-wire device" with a smaller size, more suitable for a variety of occasions, wider applicable voltage and more economical. The digital temperature sensor DS18B20 from DALLAS Semiconductor is the world's first temperature sensor that supports the "one-wire bus" interface.


The temperature measurement range is -55℃~+125℃, and the conversion accuracy can be programmed to 9~12 bits, and the resolvable temperatures are 0.5℃, 0.25℃, 0.125℃ and 0.0625℃. At 9-bit resolution, the temperature can be converted to digital within 93.75ms at most; at 12-bit resolution, the temperature value can be converted to digital within 750ms at most.


3 Temperature Sensor

3.1 Temperature sensor characteristics


DALLAS' latest single-wire digital temperature sensor DS18B20[2] is a new type of "one-wire device" with a smaller size, more suitable for a variety of occasions, wider applicable voltage and more economical. DALLAS Semiconductor's digital temperature sensor DS18B20 is the world's first temperature sensor that supports the "one-wire bus" interface. The temperature measurement range is -55℃~+125℃, and the conversion accuracy can be programmed to 9-bit~12-bit. The temperatures that can be distinguished are 0.5℃, 0.25℃, 0.125℃ and 0.0625℃ respectively. At 9-bit resolution, the temperature can be converted to digital within 93.75ms at most; at 12-bit resolution, the temperature value can be converted to digital within 750ms at most. The performance characteristics of DS18B20 are as follows:

      1. Unique single-wire interface requires only one port pin for communication;

      2. Multiple DS18B20 can be connected in parallel on only three wires to achieve multi-point networking function

      3. No external devices required;

      4. Can be powered by data line, voltage range is 3.0~5.5V;

      5. Zero standby power consumption;

      6. Temperature in 9 or 12 digits;

      7. User-definable alarm settings;

      8. The alarm search command identifies and marks devices that exceed the programmed temperature limit (temperature alarm condition);

      9. Negative voltage characteristics: when the power polarity is reversed, the thermometer will not burn out due to heat, but it will not work normally;


DS18B02 can be powered in two ways. One is power supply, in which case pin 1 of DS18B20 is grounded, pin 2 is used as a signal line, and pin 3 is connected to the power supply. The other is parasitic power supply, in which the microcontroller port is connected to a single-wire bus. To ensure sufficient current within the effective DS18B20 clock cycle, a MOSFET can be used to pull up the bus.


When DS18B20 is in memory write operation and temperature A/D conversion operation, there must be a strong pull-up on the bus, and the maximum pull-up start time is 10us. When using parasitic power supply, the VDD terminal is grounded. Since the single-wire system has only one wire, the transmission interface must be tri-state.

Figure 3.1 DS18B20 pin diagram

Figure 3.1 DS18B20 pin diagram


3.2 Working sequence diagram

1. Initialization

Figure 3.2 Initialization timing diagram

Figure 3.2 Initialization timing diagram


1) Set the data line to high level 1 first; 2) Delay (the time requirement is not very strict, but it should be as short as possible); 3) Pull the data line to low level 0; 4) Delay 750us (the time range can be 480~960us); 5) Pull the data line to high level 1; 6) Delay waiting. If the initialization is successful, a low level 0 returned by DS18B20 will be generated within 15~60ms, and its existence can be determined based on this state. However, it should be noted that it cannot wait indefinitely, otherwise the program will enter an infinite loop, so a timeout judgment must be performed; 7) If the CPU reads the low level 0 on the data line, it will also delay, and the delay time must be at least 480us from the time the high level is sent; 8) Pull the data line to high level again and end.


2. DS18B20 write data

Figure 3.3 Write data timing diagram

Figure 3.3 Write data timing diagram

      1) The data line is first set to low level 0;

      2) The delay time is determined to be 15us;

      3) Send data in order from low to high (only one bit at a time);

      4) The delay time is 45us;

      5) Pull the data line to high level 1;

      6) Repeat steps 1) to 5) until all bytes are sent;

      7) Finally pull the data line high to 1.

      3. DS18B20 read data

Figure 3.4 Read data timing diagram

Figure 3.4 Read data timing diagram

      1) Pull the data line high to 1;

      2) Delay 2us;

      3) Pull the data line low to 0;

      4) Delay 6us;

      5) Pull the data line high by 1;

      6) Delay 4us;

      7) Read the state of the data line to obtain 1 state bit and perform data processing;

      8) Delay 30us;

      9) Repeat steps 1) to 7) until one byte is read.


4 Hardware Design


4.1 Clock circuit and reset circuit


1. Clock circuit


The clock circuit can generate CPU calibration timing and is the control core of the single-chip microcomputer. This design realizes the timing control of the clock circuit by connecting an external 12MHz crystal oscillator. When using the on-chip oscillator, XTAL1 and XTAL2 are the input and output of the reverse amplifier respectively. The external crystal and capacitors C3 and C5 form a parallel resonant circuit, which is connected to the feedback loop of the amplifier. When driven by an external clock, the XTAL2 pin should be left floating and driven by the signal on the XTAL1 pin. The external oscillator becomes the internal clock signal through a 2-frequency trigger, so there is no requirement for the duty cycle of the external signal, but the minimum and maximum high-level duration and low-level duration should meet the technical requirements. The circuit is shown in Figure 4.1.


Figure 4.1 Crystal oscillator circuit

Figure 4.1 Crystal oscillator circuit


2. Idle mode


In idle mode, the internal clock signal of the CPU is blocked by the gating circuit, and the CPU enters a sleep state, but the internal clock signal continues to supply the interrupt system, timer and serial port. This mode is called by software. During idle mode, the status of the on-chip RAM and all special registers is still retained. The idle mode can be terminated by any allowed interrupt or hardware reset. When the idle mode is terminated by hardware reset, the system usually resumes the execution of the program at idle. The hardware reset only requires the signal to remain valid for two machine cycles. When the idle mode is terminated by reset, in order to avoid the possibility of accidentally writing to the port pin, the next instruction that calls the idle mode instruction should not be a write to the port pin or external memory.


3. Power-off working mode


5.2 Programming

5.2.1 Main program design


The main program mainly completes functions such as hardware initialization and subroutine calling.


1. Initialization.


First, call  the LCD  initialization program to display the data "RECEIVE:" and "TEMP is: *C" on the LCD.


Then call the interrupt and serial port initialization subroutine program, and clear the serial port receiving data unit RECDATA. Set the SM0 and SM1 bits of the register SCON to define the serial port working mode, select the baud rate generator as timer T1; set the working mode of timer T1 to mode 2; set the baud rate parameter to 9600bps; enable serial interrupt and total interrupt; enable the serial port to receive data, define REN=1; start the timer/counter T1, and define TR1=1.


2. Serial port sends and receives data.


Determine whether the serial port successfully receives data flag flag_UART is  0. If flag_uart is 0, it means that the serial port has not received data, and then continue to wait for the serial port to receive data; if flag_uart is 1, it means that the serial port successfully receives or sends data, enter the serial port interrupt service subroutine, the microcontroller receives data, and clears the serial port successfully receives data flag flag_uart, calls the LCD display receiving data subroutine, displays the data received by the microcontroller from the serial port on the LCD, and sends the temperature value back to the PC for display. The main program design flow chart is shown in Figure 5.1.

Figure 5.1 Main program flow chart

Figure 5.1 Main program flow chart


5.2.2 Serial port interrupt service subroutine


Determine whether the serial port transmission flag TI is 1. If TI is 1, the data is sent from the microcontroller to the PC, and TI is cleared, and the interrupt subroutine returns; if TI is 0, indicating RI=0, the serial port reception flag RI is cleared, the data in the serial port reception buffer SBUF is written into the serial port reception data unit RECDATA, and then the data is sent to the serial port transmission buffer SBUF, transmitted to the PC, and the serial port successful reception data flag RECOKBIT is set to 1, indicating that the serial port successfully receives and transmits data, and finally the interrupt subroutine returns. The design flow chart of the serial port receiving and transmitting data interrupt service subroutine is shown in Figure 5.2.

Figure 5.2 Serial port interrupt service subroutine

Figure 5.2 Serial port interrupt service subroutine


5.2.3 Temperature reading subroutine


The main function of the temperature reading subroutine is to read out 9 bytes in the RAM. During the reading, a CRC check is required. If the check is wrong, the temperature data will not be rewritten. The program flow chart is shown in Figure 5.3.

Figure 5.3 Temperature reading subroutine

Figure 5.3 Temperature reading subroutine


5.2.4 Temperature Conversion Command Subroutine


The temperature conversion command subroutine mainly sends the temperature conversion start command. When using 12-bit resolution, the conversion time is about 750ms. In this program design, a 1s display program delay method is used to wait for the completion of the conversion. The temperature conversion command subroutine flow chart is shown in the figure above and Figure 5.4.

Figure 5.4 Temperature conversion flow chart

Figure 5.4 Temperature conversion flow chart


5.2.5 Temperature calculation subroutine


The temperature and humidity calculation subroutine converts the value read from RAM into BCD code and determines whether the temperature value is positive or negative. The program flow chart is shown in Figure 5.5.

Figure 5.5 Temperature calculation flow chart

Figure 5.5 Temperature calculation flow chart

5.2.6 Display data refresh subroutine


The display data refresh subroutine mainly performs the refresh operation on the separated temperature display data. When the flag bit is 1, the symbol display bit is moved to the first bit. The program flow chart is shown in Figure 5.6.

Figure 5.6 Display data refresh subroutine


6 Conclusion


The hardware of this system adopts modular design, with AT89C52 single-chip microcomputer as the core, LCD display circuit, serial port communication circuit and DS18B20 temperature detection circuit to form a control system. The hardware of this system mainly includes the following modules:


AT89C52 main control module, LCD display module, serial port communication module, DS18B20 temperature detection module, etc. Among them, AT89C52 mainly completes the control of peripheral hardware and some calculation functions, the LCD display module completes the display function of characters and numbers, the serial port communication module mainly completes the communication function between the microcontroller and the PC, and the DS18B20 temperature detection module mainly completes the ambient temperature detection function.



In power-down mode, the on-chip oscillator stops working. The power-down instruction is the last instruction executed. The values ​​of the on-chip RAM and special registers are retained until the power-down mode is terminated. The power-down mode can only be exited by hardware reset. After reset, all special registers will be redefined, but the contents of RAM will not be changed. Reset cannot be started before VCC returns to the normal operating voltage. The reset signal should be kept long enough to ensure the oscillator starts and reaches stability.


In order to make the microcontroller work normally, it is also necessary to add a power-on reset circuit and a power-off detection circuit. Brief principle of power-on reset:

When the system does not need to be reset, the RST terminal is at a low level; when the button is pressed, the RST terminal becomes a high level.

Figure 4.2 Power-on reset circuit

Figure 4.2 Power-on reset circuit

AT89C51, crystal oscillator circuit and power-on reset circuit together constitute the minimum system of the microcontroller, as shown in Figure 4.3.

Figure 4.3 Minimum system

Figure 4.3 Minimum system

4.2 Temperature Sensor

Figure 4.4 DS18B20 connection diagram

Figure 4.4 DS18B20 connection diagram


As can be seen from Figure 4.4, the connection between DS18B20 and the microcontroller is very simple. The microcontroller only needs one I/O port to control the DS18B20. The connection method in this figure is that the microcontroller communicates with one DS18B20. If you want to control multiple DS18B20s for temperature collection, just connect all the I/O ports of the DS18B20 together.


4.3 LCD Display Module


The display circuit uses LCD1602 liquid crystal display screen, P2 is used as the 8-bit data input port of the liquid crystal, P1.0 port is used as the liquid crystal data/command selection port, and P1.1 is the liquid crystal enable port.

Figure 4.5 LCD display module

Figure 4.5 LCD display module


4.4 Serial port communication module design


The 51 microcontroller has a full-duplex serial communication port, which allows easy communication between the microcontroller and the computer.


The level range is the voltage range within which the circuit can safely and reliably identify the signal.


 The voltage level range of CMOS circuit is generally from 0 to the power supply voltage. In CMOS level, high level (3.5~5V) is logic "1" and low level (0~0.8V) is logic "0".


The level range of the RS232 interface is -15V to +15V. The RS232 level uses negative logic, that is, logic "1": -3~-15V, logic "0": +3~+15V.


The serial port of the microcontroller is TTL level, while the serial port of the computer is RS232 level. To enable communication between the two, there must be a level conversion circuit between the two. That is, the serial port of the microcontroller must be connected to an external level conversion circuit chip to convert the 1 represented by the TTL-compatible CMOS high level into the negative voltage signal of RS232, and the low level into the positive voltage signal of RS-232. The typical conversion circuit gives -9V and +9V.


In this design, the conversion interface of MAX232 chip can be used to realize logic level conversion: MAX232 is produced by MAXIM, and contains two-way driver and receiver RS-232 conversion chip. There is a voltage converter inside the MAX232 chip, which can convert the input +5V voltage into the ±10V voltage required by the RS-232 interface, which is especially suitable for single power supply systems without ±12V. Other chips with the same principle include MAX202, ADDt101 of AD Company and IC 1232 of SIL  Company .

Figure 4.6 MAX232 chip pins

Figure 4.6 MAX232 chip pins


Since protues simulation does not require level conversion, the MAX232 chip circuit is not used in simulation, but level conversion is required when making a real object. The hardware connection diagram is shown in Figure 4.7. The T1in pin of the MAX232 chip is connected to the P3.1 (TXD) pin of the AT89C51 microcontroller, and the R1out pin of the MAX232 chip is connected to the P3.0 (RXD) pin of the AT89C51 microcontroller; the T1out pin of the MAX232 chip is connected to the 2nd pin of the DB9 pin interface, and the R1in pin of the MAX232 chip is connected to the 3rd pin of the DB9 pin interface.

Figure 4.7 Level conversion hardware connection diagram

Figure 4.7 Level conversion hardware connection diagram


4.5 System Schematic Diagram


The above modular design can be used to obtain the schematic diagram of the entire system as shown in Figure 4.8:

Figure 4.8 System schematic diagram

Figure 4.8 System schematic diagram


5 Software Design


5.1 Algorithm Design

 

The steps to write a microcontroller asynchronous communication program are as follows:

      1. Set the serial port working mode

      At this time, you need to set SM0 and SM1 in the serial controller SCON. In the communication between the PC and the microcontroller, the serial port is generally selected to work in mode 1.

      2. Select the baud rate generator

      Select Timer 1 or Timer 2 as the baud rate generator.

      3. Set the timer working mode

      When selecting timer 1 as the baud rate generator, you need to set its mode register TMOD to the counting mode and select the corresponding working mode (usually select mode 2 to avoid reloading the timer initial value); when selecting timer 2 as the baud rate generator, you need to set T2CON to the baud rate generator working mode.

      4. Set the baud rate parameters

      There are two parameters that affect the baud rate. One is the SMOD bit in the special register PCON, and the other is the initial value of the corresponding timer.

      5. Enable serial interrupts

      Because we usually use interrupt receiving mode in the program, we should set EA=1 and ES=1.

      6. Allow to receive data

      Set REN in SCON to 1, which means the serial port is allowed to receive data.

      7. Enable the timer/counter to work

      At this time, turn on the timer/counter to generate a baud rate of 8. Write a serial interrupt service program.

      When data arrives at the serial port, the system will automatically execute the interrupt service program you have written.

      9. Receive/send corresponding data

      Note that T1 needs to be cleared after the sending operation is completed, and R1 needs to be cleared after the receiving operation is completed.


Keywords:MCU Reference address:Research on the communication principle and circuit design between single chip microcomputer and PC

Previous article:Analytical solution for communication between a microcontroller and a PCI device
Next article:Application of Multi-computer Parallel Communication System Based on 51 Single Chip Microcomputer

Latest Microcontroller Articles
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号