Communication between VB and MCU

Source: Internet
Author: User

The 51 series single-chip microcomputer supports multi-host communication with the host computer, that is, a host computer can control multiple lower computers (composed of single-chip microcomputer) to work collaboratively in time. The implementation of this function mainly depends on the existence of SM2 in 51 single-chip microcomputer, which receives data from the host computer address or the flag. 51 single-chip microcomputer has three communication modes controlled by the scon register. The scon register structure is as follows:
Scon D7 D6 D5 D4 D3 D2 D1 D0
Sm0 SM1 SM2 Ren tb8 rb8 Ti ri

(1). sm0 and SM1: control bit of the serial port operating mode.
How sm0 and SM1 work
00 mode 0
01 method 1
10 Mode 2
11 Mode 3
(2). SM2: multi-host communication control bit.
Multi-machine communication works in Mode 2 and Mode 3, while SM2 is mainly used in Mode 2 and Mode 3. receiving status. When the serial port is in the format of 2 or 3 and SM2 = 1, only when the received 9th-bit data (rb8) is 1, the first eight digits of the received data are sent to the sbuf, and the device Ri sends an application for interruption. Otherwise, the received data is discarded. when SM2 is set to 0, data is rarely sent to sbuf regardless of whether the first data is 0 or 1, and an application for interruption is sent.
When the mode is 0, SM2 must be 0.
(3). Ren: allowed receiving bits.
Ren is used to control the allow and deny of data reception. When Ren is set to 1, Ren is allowed to receive data. When Ren is set to 0, Ren is forbidden to receive data.
(4). tb8: send and receive data bits 8.
In Methods 2 and 3, tb8 is sent, that is, the 9th-Bit Data bit. In multi-host communication, the data must also be transmitted, and it indicates that the transmitted address is still data. If tb8 = 0 is data, the address is when tb8 = 1.
(5). rb8: receives data bits 8.
In Methods 2 and 3, rb8 stores the received 9th-bit data to identify the received data features.
6). Ti: indicates the sending interruption flag.
Addressable flag. When the method is 0, the hardware positions the 8th-bit data after it is sent. In other methods, the hardware positions the data before the sending or stopping bits. Therefore, Ti = 1 indicates that the frame sending is complete, ti can be "0" by software ".
(7). Ri: The identifier of the reception interruption.

Note:
1. Implementation Principle
The single-chip microcomputer can communicate with multiple machines only in 2 or 3 modes. When SM2 is set to 1, only when 9th-bit data (rb8) is received, the first eight digits of received data are sent to sbuf, and the device Ri sends an application for interruption. Otherwise, the received data is discarded. When SM2 is set to 0, no matter whether the first data is 0 or 1, it is rare that the data is sent to sbuf and an application for interruption is sent. In this way, multi-host communication can be realized. Why? If all the lower computers set SM2 to 1 at the beginning, they can only receive data marked as addresses. The host computer first sends the address data, and then all the single-chip computers receive the data and send it to their respective sbuf. Then we can judge the data in the single-chip program, check whether it is the address of the Local Machine. If yes, the SM2 will be cleared and wait for the host computer to send data (this is not the address data, the difference between them is that 9th bits is 1 or 0, if the value is 1, we define the address data as 0, And we define the data as). Other Single-Chip Microcomputer do not reset SM2 because the received address data is different from the preset data, then they will not be able to receive the data sent from the MCU in the next cycle (not the address data) and will continue to wait for the arrival of his address data.

2. How does VB send 9th-bit data?
This is a hard process. For me, PC and single-chip microcomputer have been engaged in communication, but multi-computer communication is still the first time that the big girl is on the sedan. So I found a lot of information on the Internet and tested it many times on the machine. I finally found the answer. Now I want to record it:
VB code and Annotation
// Related code in the PWM-4DA, the actual operation is effective
Public Function send (byref MSComm as MSComm, byval moduleaddress as byte, byval channeladdress as byte, byval channeldata as byte)
Dim buffer (0) as byte 'stores sent data

MSComm. commp ORT = 3' set COM1 as the serial port
MSComm. nulldiscard = false
MSComm. portopen = true' open the serial port

MSComm. settings = "9600, M, 8, 9th" '11-bit serial communication mode, sending Address mode (mark, indicating that the mark is the address data, will automatically send position 1)
Buffer (0) = moduleaddress // sends the channel address first. Note that the ninth bit of the data is 1 (address bit ID, used for MCU identification)
MSComm. Output = buffer // output data. If the buffer array length is N, the program will send n data consecutively in the same way.
Delay (5) 'has a latency of at least 2 milliseconds. We recommend that you transmit data in 5 milliseconds and judge the microcontroller. This delay ensures the quality of communication.

MSComm. settings = "9600, S," // (space, that is, blank, the ninth bit is 0, and now the sent data is not the address data, but the useful data that is actually needed)
Buffer (0) = channeladdress // channel data address
MSComm. Output = Buffer
Delay (5) 'latency: at least 2 milliseconds. Recommended: 5 milliseconds

Buffer (0) = channeldata // channel data Note: I have sent two non-address data here
MSComm. Output = Buffer
Delay (5) 'latency: at least 2 milliseconds. Recommended: 5 milliseconds

MSComm. portopen = false
Send = "OK"
End Function

Public sub delay (MSEC as long)
On Error resume next
Dim tstart as single
Tstart = Timer
While (timer-tstart) <(MSEC/1000)
Doevents
Wend
End sub

Summary of the ninth BIT data sent by VB: control of different sending modes, which is manifested in MSComm in the code. settings = "9600, M, 9600" or ", S ,"

3. How to send the ninth data on the microcontroller end (this part is not used in the project PWM-4DA, here is just some theoretical knowledge, not to personally Practice)

Assuming that the host computer is a 51-series single-chip microcomputer, then we need to solve how to send the ninth-bit data on the single-chip computer end. We noticed that there is a tb8-bit in the scon control register, when tb8 is set to 1, the address data is sent. When tb8 is set to 0, the data is sent. it is sent following in sbuf =? Later. You can try it if you have time. This is quite useful for using a single-chip microcomputer as the master.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.