

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、<p><b> 中文4700字</b></p><p> The Designing of Serial Communication Based on RS232</p><p><b> Abstract</b></p><p> This paper discussed the principle
2、of serial communication mainly, proposed the design method based on RS232, designed the hard circuit of serial communication, and realized the programming to PC Microcomputer and single chip microcomputer by using assemb
3、ly language and C++ Builder separately.</p><p> Keywords: serial communication; single chip microcomputer; program design.</p><p> I. INTRODUCTION</p><p> During working in the c
4、ontrol system, the PC microcomputer sends instructions for step-by-step motor to collect the image information. Therefore the communication between the PC microcomputer and the driving system is needed for the communicat
5、ion system. In general, a complete communication system consists of transmitter, receiver, converting the data interface and transmitting data channel. The control of the step-by-step motor is realized through the pulse
6、distribution by single chip microcomp</p><p> Serial communication is one of the oldest mechanisms for devices to communicate with each other. Starting with the IBM PC and compatible computers, almost all c
7、omputers are equipped with one or more serial ports and one parallel port. As the name implies, a serial port sends and receives data serially, one bit at a time. In contrast, a parallel port sends and receives data eigh
8、t bits at a time, using eight separate wires.</p><p> For serial communication to work, you just need a minimum of three wires—one to send, one to receive, and one signal ground. For parallel communication,
9、 you need eight wires.</p><p> Despite the comparatively slower transfer speed of serial ports over parallel ports, serial communication remains a popular connectivity option for devices because of its simp
10、licity and cost-effectiveness. It shows some of the devices that use a serial port to connect to the computer. Using a serial port, you can connect to a modem, a mouse, or a device such as a bridge/router for configurati
11、on purposes.</p><p> II. THE BRIEF DESCRIPTION OF SERIAL PORT COMMUNITION</p><p> Serial communication is the communication between sender and receiver data transfer is in the single data line
12、, to move each time a bit. The advantage is that only a pair of transmission lines to send information, so its low cost, suitable for long-distance communications; its disadvantage is that transmission speed is low.</
13、p><p> As mentioned, a serial device sends and receives data one bit at a time. Some devices can send and receive data at the same time and are known as full-duplex devices. Others that can either send or rece
14、ive at any one time are known as single-duplex.</p><p> To initiate transmission, a device first transmits a start bit, followed by the data bits. The data bits can be five, six, seven, or eight bits, depen
15、ding on what has been agreed upon. Both the sender and the receiver must be set to the same data bits for communication to take place correctly. Once the data bits are sent, a stop bit is sent. A stop bit can be one, one
16、 and a half, and two bits. The baud rate is the speed of transmission of data from one device to another. Baud rate is usually mea</p><p> To detect that the data has been sent correctly, an optional parity
17、 bit can be included together with the data bits. A parity bit can be one of the following: odd, even, mark, space, or none (mark and space parity are almost always used). Using a parity bit provides a basic mechanism to
18、 detect corruption of data that was sent and does not guarantee that the data received is free from error. Nevertheless, a parity bit is useful for improving the integrity of the data sent.</p><p> Most ser
19、ial ports adhere to the RS232C standard, which specifies a connector either with 25 pins or with 9 pins. Most serial devices use the nine-pin connector.</p><p> Serial communication with asynchronous commun
20、ication and synchronous communication are two basic means of communication. Synchronous communication for the case of high transmission speed, the hardware complexity. The asynchronous communication used in transmission
21、speed between 50 and 19200 baud. Transmission is relatively common. In asynchronous communication, data is transmitted frame by frame, each frame of serial data format by a start bit, 5 to 8 data bits, a parity bit (opti
22、onal) and a Sto</p><p> CMOS smart sensor IC has received wide application for low cost, miniaturization, intelligence and standardization. In smart sensor IC designs, the communication interface circuits a
23、lso play an important role other than sensors and signal processing circuits. There are more serial communication standard interfaces, such as RS-232, RS-485, USB Interface, IEEE-1394, which has itself good features and
24、also has focused on areas of application. RS-485 has a anti-noise capacity transmitting data, so i</p><p> RS-232C standard RS-232C is the American Electronics Industry Association (EIA) formally announ
25、ced, in the asynchronous serial communication, the most widely used standard bus. The standard for DCE and DTE serial binary communication between the maximum data transfer rate of up to 19. 2kbps, the longest transmissi
26、on cables up to 15 meters 1RS - 232C standard defines the 25-lead, two-way communication in general, just Serial input RXD, serial output TXD and ground GND1RS - 232C standard level</p><p> RS-232 is define
27、d as the single-ended standards, which has much characteristic, such as more communication distance in a low-speed serial communications, moderate price and good practicality of the system, so it is most appropriate to b
28、e a communication port. RS-232 is an essential modern computer interface, it includes ports COM1 and COM2, the port COM1 has 9-pin connector. The new generation computer has a 9-pin connector (DB9) so as to connect with
29、the RS-232 communication port. In order to a</p><p> Figure 1. The RS-232 port</p><p> III. THE HARDWARE DESIGN OF SERIAL PORT COMMUNICATION</p><p> The in-out level of single ch
30、ip computer is TTL level, while the RS-232 serial interface of a PC computer is standard serial interface, so both the electrical specifications are inconsistent. In order to communicate with the upper and lower computer
31、, it is needed that the out level of single chip computer is changed to the TTL level. The common level converter chip between TTL and RS-232 level is MCI1488 and MCI1489. The MCI1488 converts TTL level to RS-232 level,
32、it needs the supply voltage of </p><p> MAX232 is the product of the MAXIM Company; it is the road receiver and driver IC chip.</p><p> About MAX232 chip MAXIM MAX232 chip is produced by l
33、ow-power, single-supply dual RS232 transmit / receiver 1 for all EIA-232E and V. 28 / V. 24 communication interfaces 1MAX232 a chip supply voltage converter, can+ 5V input power converted into RS - 232C ± 10V output
34、 voltage level required, so using this chip serial communications interface system as long as a single +5 V power supply can. MAX232 requires four external electrolytic capacitors C1, C2, C3, and C4, is the internal
35、capacitan</p><p> MAX232 chip has a voltage converter which can transform the voltage of +5 V to the required voltage. Therefore, it adapts to the serial communication interface, moreover it has much charac
36、teristic, such as moderate price, the hardware simple, so it is used to be a voltage converter. The hardware circuit of serial communication module is shown in Figure 2.</p><p> Figure 2. The hardware circu
37、it of serial communication module</p><p> IV. THE SOFTWARE DESIGN OF SERIAL PORT COMMUNICATION</p><p> The communication program of the single chip microcomputer communicates with PC computer
38、by interrupt and PC computer is called as main controller. When the single chip computer receives the data signals sent by PC computer, then it calls interrupt service program. The flowchart of the interrupt service main
39、 subprogram is shown in Figure 3. In order to fully exploit the efficiency of single chip computer to minimize CPU ‘time occupied by communication, the control program will be we will promptl</p><p> A. The
40、 subprogram of serial port interrupts for receiving data</p><p> The subprogram of serial port interrupts for receiving data is mainly responsible for receiving data sent by PC microcomputer and storing the
41、 data into allocated memory (it does not deal with data so as to reduce the time taken up by interruptions). The data received by the single chip microcomputer includes little information, such as speed, steps and on/off
42、 instructions, and the buffer is large enough to send the computer, so the software shake hands protocol is omit so as to raises the CPU ut</p><p> B. The subprogram of sending the step-by-step impulse</
43、p><p> Using counter-timer of the single chip microcontroller timer 0, as the fixed times complete, the p1.5 port is negated so as to periodically generate the driving pulses.</p><p> The timer 0
44、 begins to count from initial value to 65535, then the interrupt sub-program of timer 0 is called, in which the p1.5 port is negated to generate a driving pulse. Meanwhile, the single chip microcomputer loads again the i
45、nitial value which is sent by the PC microcomputer by serial communication.</p><p> Figure 3. The flowchart of the interrupt service main subprogram</p><p> C. The subprograms of judging instr
46、uction</p><p> The program of judging instruction is set on the serial port interrupt program to be called. The PC microcomputer sends data to the single chip microcomputer. After the single chip microcompu
47、ter receives data, it sets the RI higher and called the serial port interrupt sub-program, in which the first data is processed. If the first data is a digital symble”48”, the serial port interrupt sub-program sets P1.4
48、higher, then the next five bytes data is used to ensure the step-by-step motor’ speed and</p><p> D. The processing subprogram of send interrupts</p><p> The processing subprogram of send inte
49、rrupts is responsible to send data to the computer, sending interrupt state is off in the general. When the communication program completely deals with data sent by PC microcomputer, the symbol “1” can be written to send
50、 buffer. As the computer receives the symbol “1”, it is said that single chip computer executes completely the introduction sent by PC microcomputer. Therefore the serial port interrupt is set as “off” state by the singl
51、e chip microcontroller</p><p> V. THE DESIGN OF COMMUNICATION PROGRAM OF PC MICROCOMPUTER AND SINGLE CHIP MICROCOMPUTER</p><p> In order to realize the system software easily grafted and unifo
52、rmity, the program of upper computer is designed using the Boland C++ Builder 5.0 as a programming tool in the control system s the Boland C++ Builder 5.0 as a programming tool for PC programming. The serial port communi
53、cation can be utilized in two ways: First, register components, C++ Builder does not in itself provide a separate serial communication component, but it can be obtained through registered Microsoft MSComm32 component<
54、/p><p> (1) Setting by communication protocol and opening the serial port, which is called initialization, then blocking the other programs to use the serial port.</p><p> (2) Configuring the ser
55、ial port.</p><p> (3) Transmitting data by the serial port to and fro, verifying data during transmission.</p><p> (4) Closing the serial port as no needing it so that other program uses.</
56、p><p> The sub-interface is simple, in which the setting on sub-interface parameters are defaults, such as baud rate that single chip communication module has been set to 2400Bps and that the baud rate is set
57、to 2400Bps in the computer settings on the ports. The PC microcomputer has two ports called as serial port COM1 and COM2 generally, so the program can automatically identify serial port so that it can send and receive da
58、ta and instructions successfully.</p><p> Serial communication program includes two aspects, one is MCS-51 microcontroller communication program, on the other hand for the PC, the communication process. In
59、the preparation processes, the development of the two protocols is very important, otherwise it will not guarantee the reliability of data communications, and thus lose the meaning of communication. The communication pro
60、tocol is agreed as follows:1) The serial communication baud rate is 1200bps.2) The frame format is: a start bit, e</p><p> VI. CONCLUSION</p><p> The hardware of serial port communication is
61、 designed based on RS232, and the software of communication module between PC microcomputer and single chip microcomputer were programmed by using C++ Build and assembly language. The control system runs well on industry
62、 field, and controls the stepping motor’ Start/Stop and Forward/Reversal rotation by controlling single microcomputer. The PC microcomputer sends out control instruction to the single microcomputer by friendly user inter
63、face.</p><p> REFERENCES</p><p> [1] Fan Yizhi, Jiang Wenxiang, Chen Liyuan. Serial communication control based C++Build and RS232. Beijng: Tsinguua University Press, 2002, pp. 2-18</p>
64、<p> [2] Gou Suai. C++Builder 5.0 Programmer’s Guide. Beijing: Beijing Hope Electronic Press, 2001, pp. 1-6</p><p> [3] Liu Yanling. The Use of Communication Between MCS-51 Single Chip Microcomputer
65、and PC Microcomputer with MAX232. Journal of Tianjin Institute of Technology, 1999, Vol, 15(2): pp. 56-58</p><p> [4] Yang Guoxia. Dealing With The Data Communication Between The Serial Port And The Main Pr
66、ogram by Message. Journal of The Hebei Academy of Sciences, 2001, Vol. 18(4): pp. 220-223</p><p> [5] LI Buyin, Jiang Shenglin, Lu Wenzhong, Zhou Dongxiang. Serial Communication Programming Design Based on
67、C Language. Journal of Transcluction Technology, 2002, Vol. (2), pp. 65-66</p><p> [6] Wang Zhihong.The serial communication of single chip microcomputer and PC. Modern Electronic Technique, 2000,119, pp. 6
68、8-70</p><p> 基于RS-232的串行通信設計</p><p><b> 摘 要</b></p><p> 論文抽象地論述了串行通信的主要原則,提出了在RS232的基礎上,設計串行通訊硬電路的方法。實現(xiàn)了分別在PC微機和單片機中使用匯編語言和C++Builder的編程。</p><p> 關鍵詞:串行
69、通信;單片機;程序設計</p><p><b> 一、緒論</b></p><p> 在控制系統(tǒng)的工作中,PC微機通過發(fā)送指令到步進電機中來收集圖像信息。因此,PC微機和驅動系統(tǒng)之間的通信是系統(tǒng)通信所必需的。一般來說,一個完整的通信系統(tǒng)由發(fā)射器,接收器,轉換數(shù)據(jù)接口和傳輸數(shù)據(jù)通道所構成。該步進電機是通過單芯片來實現(xiàn)微機控制脈沖分配的。在電機的控制系統(tǒng)中,PC微機被
70、稱為上位機,而由單片機所控制的步進電機被稱為下位機。上位機與下位機之間的通信有兩個方法,它們分別是并行通信和串行通信。并行通信可以同時傳輸大量的數(shù)據(jù),具有傳送速度快的優(yōu)點。但是當傳輸距離遠,并且傳輸數(shù)據(jù)的數(shù)量很多時,它需要的傳輸線的數(shù)目也更多。此外,電壓等級會因為輸電線路的因素和電磁干擾而改變。串行通信是一個數(shù)據(jù)按一個個序位單向傳輸?shù)耐ǖ?,其中兩個過渡線是實現(xiàn)雙向通信傳輸?shù)男枰?。正是考慮到在控制系統(tǒng)中需傳遞的數(shù)據(jù)量多,而單次傳輸?shù)臄?shù)量少
71、,因此在控制系統(tǒng)中選擇串口通信的方法,簡化了硬件結構并節(jié)約了所需的成本。</p><p> 串行通信是最古老的溝通機制之一。從IBM個人電腦和兼容式電腦的時代開始,幾乎所有的計算機都配有一個或多個串行端口和一個并行端口。顧名思義,一個串行端口發(fā)送和接收串行數(shù)據(jù),一次一位數(shù)據(jù)。相反,一個并行端口一次發(fā)送和接收8位數(shù)據(jù),使用8個單獨的電線。</p><p> 要使串行通信工作,你只需要一根
72、三根線的電纜——1根發(fā)送,1根用來接收,1根接地。對于并行通信,你需要采用8條導線。</p><p> 盡管相對較慢的傳輸速度遠低于并行端口,串行端口通信依然因為它簡單的設備和高的成本效益而成為一個受歡迎的連接選項。它表現(xiàn)為一些設備使用串口連接到計算機。使用串行端口,你可以連接到調(diào)制解調(diào)器,鼠標或其它設備,如一個橋梁/路由器進行配置。</p><p><b> 二、串口通訊的
73、簡述</b></p><p> 串行通信是指通信的發(fā)送方和接收方之間數(shù)據(jù)信息的傳輸是在單根數(shù)據(jù)線上,以每次一個二進制位移動。它的優(yōu)點是只需一對傳輸線進行傳送信息,因此其成本低,適用于遠距離通信;它的缺點是傳送速度低。</p><p> 如前所述,一個串行設備一次發(fā)送和接收一個位的數(shù)據(jù)。有些設備因為在同一時間發(fā)送和接收數(shù)據(jù),被稱為全雙工設備。其他可以在任何時間發(fā)送或接收被稱為
74、單雙工設備。</p><p> 開始傳輸時,設備先發(fā)送一個起始位,其次是數(shù)據(jù)位。該數(shù)據(jù)位可以是五、六、七或八位,基于商定而定。發(fā)送器和接收器必須設置為相同的數(shù)據(jù)通信比特或正確的比特率。數(shù)據(jù)位被發(fā)送完后,就會發(fā)送一個停止位。一個停止位可以是一位,一個半位,或兩位。波特率是數(shù)據(jù)從一個設備到另一個的傳輸速度。波特率通常以每秒的位數(shù)(bps)來計量。大多數(shù)串行設備傳輸七、八位數(shù)據(jù)。</p><p&g
75、t; 為了檢測數(shù)據(jù)已被正確發(fā)送,一個可選的校驗位可以同數(shù)據(jù)位在一起。一個校驗位可以是以下內(nèi)容:奇數(shù),偶數(shù),標記,空白或無(空的奇偶位標志幾乎總是被使用)。使用校驗位提供了一個基本的機制,以檢測已發(fā)送數(shù)據(jù)是否損壞,但不保證檢查數(shù)據(jù)本身的錯誤。然而,校驗位可用于改善完整數(shù)據(jù)的傳送。</p><p> 大多數(shù)串行端口使用RS232C標準,它指定了一個25針或9針的連接器。大多數(shù)系列設備使用9針連接器。</p&
76、gt;<p> 串行通信有異步通信和同步通信兩種基本通信方式。同步通信適用于傳送速度高的情況,其硬件復雜。而異步通信應用于傳送速度在50到19200波特之間,是比較常用的傳送方式。在異步通信中,數(shù)據(jù)是一幀一幀傳送的,每一串行幀的數(shù)據(jù)格式由一位起始位,5~8位的數(shù)據(jù)位,一位奇偶校驗位(可省略)和一位停止位四部分組成。在串行通信前,發(fā)送方和接收方要約定具體的數(shù)據(jù)格式和波特率(通信協(xié)議)。PC機采用可編程串行異步通信控制器82
77、50來實現(xiàn)異步串行通信。通過對8250的初始化編程,可以控制串行數(shù)據(jù)傳送格式和速度。在PC機中一般有兩個標準RS-232C串行接口COM1和COM2。MCS-51系列單片機片內(nèi)含有一個全雙工的串行接口,通過編程也可實現(xiàn)串行通信功能。</p><p> 智能傳感器的CMOS芯片因為其低成本,小型化,智能化和標準化的特點已得到廣泛應用。在智能傳感器IC設計中,在傳感器和信號處理電路以外,通訊接口電路也可發(fā)揮重要角色
78、。串行通信擁有很多的標準接口,如RS-232,RS-485,USB接口和IEEE-1394。它們都有著自身的特點,同時也分別適用于不同的的應用領域。RS-485的數(shù)據(jù)傳輸具有抗噪聲的特點,因此它通常用于工業(yè)生產(chǎn)。USB接口和IEEE-1394具有傳輸速度快的優(yōu)點,但是有一些電腦和操作系統(tǒng)并不支持這種接口。RS-232是在PC機中和通信行業(yè)中應用最廣泛的的串行接口。RS-232C標準是異步串行協(xié)議溝通,并已廣泛應用于個人電腦和通信產(chǎn)業(yè)。許
79、多短距離打印機等電腦外設,所有磁盤和終端與PC機通過RS232串行接口通信。</p><p> RS-232C 標準</p><p> RS-232C是美國電子工業(yè)協(xié)會(EIA)正式公布的,在異步串行通信中應用最廣的標準總線。該標準適用于DCE和DTE間的串行二進制通信,最高數(shù)據(jù)傳送速率可達19.2kbps,最長傳送電纜可達15米。RS-232C標準定義了25根引線,對于一般的雙向通信
80、,只需使用串行輸入RXD,串行輸出TXD和地線GND。RS-232C標準的電平采用負邏輯,規(guī)定+3V~+15V之間的任意電平為邏輯“0”電平,-3V~-15V之間的任意電平為邏輯“1”電平,與TTL和CMOS電平是不同的。在接口電路和計算機接口芯片中大都為TTL或CMOS電平,所以在通信時,必須進行電平轉換,以便與RS-232C標準的電平匹配。MAX232芯片可以完成電平轉換這一工作。</p><p> RS-
81、232被定義為單端標準,它具有許多的優(yōu)點,如更低速的串行通信,通信距離遠,適中的價格和良好的系統(tǒng)實用性,所以它是最適用的一個通信端口。RS-232是一個基本現(xiàn)代化的計算機接口,它包括COM1和COM2端口,該端口COM1有九針連接器。新一代的計算機有一個九針連接器(DB9),這樣就可以通過這個九針連接器來連接RS-232串行端口。為了實現(xiàn)可靠的,實時的傳輸,該系統(tǒng)采用三線連接方式,即RS-232端口的GND引腳,RXD引腳和TXD的引腳
82、與外部端口連接,如圖1所示。</p><p> 圖1.RS-232的端口</p><p> 三、串行通信端口的硬件設計</p><p> 單片機應用的是TTL電平,而PC機的RS-232串行接口使用的是標準的串行接口標準,所以兩者的電氣性能規(guī)格是不一致的。為了溝通上位機和下位機,就需要將單片機輸出的TTL電平進行轉換。常見的TTL電平和RS-232電平之間的電
83、平轉換芯片是MCI1488和MCI1489。TTL電平可由MCI1488轉換為RS-232電平,它需要±12V電源電壓,而MCI1489可以將RS-232轉換成標準級的TTL電平,它需要電源電壓為+5V。由于適用三個電源電壓,電路將不可避免地變得復雜,所以該電路采用標準的RS-232芯片MAX232。</p><p> MAX232芯片簡介</p><p> MAX232是M
84、axim公司的產(chǎn)品,它是路徑接收和驅動IC芯片。MAX232芯片是Maxim公司生產(chǎn)的低功耗、單電源雙RS232發(fā)送/接收器,適用于各種EIA-232E和V.28/V.24的通信接口。MAX232芯片內(nèi)部有一個電源電壓變換器,可以把輸入的+5V電源變換成RS-232C輸出電平所需±10V電壓,所以采用此芯片接口的串行通信系統(tǒng)只要單一的+5V電源就可以。</p><p> MAX232外圍需要4個電解電
85、容C1、C2、C3、C4,是內(nèi)部電源轉換所需電容。其取值均為1μF/25V,宜選用鉭電容并且應盡量靠近芯片,C5為0.1μF的去耦電容。</p><p> MAX232的引腳T1IN、T2IN、R1OUT、R2OUT為接TTL/CMOS電平的引腳,引腳T1OUT、T2OUT、R1IN、R2IN為接RS-232C電平的引腳。因此TTL/CMOS 電平的T1IN、T2IN 引腳應接MCS-51的串行發(fā)送引腳TXD;
86、R1OUT、R2OUT應接MCS-51的串行接收引腳RXD,與之對應的RS-232C電平的T1OUT、T2OUT應接PC機的接收端RD;R1IN、R2IN應接PC機的發(fā)送端TD。MAX232芯片有一個電壓轉換器,它可以改變+5V的電壓至所需的電壓。因此,它適應了串行通信接口。不僅如此,它有許多優(yōu)點,比如價格適中,硬件簡單等等。因此,用它來作為電壓轉換器適用。串行通信模塊的硬件電路如圖2所示。</p><p>
87、圖2 串行通信模塊的硬件電路</p><p> 四、串口通信的軟件設計</p><p> 單片機的通信程序與PC機之間通過中斷通信,PC機被稱為主控制器。當單片機接收通過PC電腦發(fā)送的數(shù)據(jù)信號時,它就會調(diào)用中斷服務程序。中斷服務子程序流程圖如圖3所示。為了充分利用單片機,以減少CPU的占用時間,提高溝通效率,控制程序將是我們會及時響應和控制的對象,通訊程序將得到優(yōu)化。子程序的串口接收數(shù)
88、據(jù),逐個發(fā)送脈沖,通過上升沿判斷指令子程序和中斷子程序發(fā)送的數(shù)據(jù)。</p><p> A串口中斷子程序用于接收數(shù)據(jù)</p><p> 用于接收數(shù)據(jù)的串行端口的中斷子程序,主要是將PC微機接收和發(fā)送的數(shù)據(jù)存儲到分配的內(nèi)存中(不進行數(shù)據(jù)處理,以減少中斷的時間)。由單片機接收到的數(shù)據(jù)包括一些信息,如速度,步驟和開/關指示,由于緩沖區(qū)的大小足夠把這些數(shù)據(jù)發(fā)送到電腦中,所以省略了軟件握手協(xié)議,以
89、提高CPU利用率。當串口中斷程序接收到指定的數(shù)據(jù)時,便退出串行口中斷。</p><p> B發(fā)送步進脈沖子程序</p><p> 使用反單晶片微控制器定時器0,作為固定的時間計時,能使P1.5端口電平被反轉,從而產(chǎn)生周期性的驅動脈沖。定時器從0開始計數(shù)初始值65535,然后中斷設定的定時器0被調(diào)用,在其中P1.5端口電平被反轉產(chǎn)生驅動脈沖。同時,單片機重新載入由PC機通過串行通信微機發(fā)
90、送的初始值。</p><p> 圖3 主要中斷服務子程序流程圖</p><p><b> C判斷指令的子程序</b></p><p> PC微機發(fā)送數(shù)據(jù)到單片機。經(jīng)過單片機接收數(shù)據(jù),并調(diào)用串口中斷子程序,它第一次發(fā)送的數(shù)據(jù)被處理。如果第一個數(shù)據(jù)是一個數(shù)字標記“48”,串行口中斷分程序設置P1.4腳為高電平,那么接下來的五個字節(jié)的數(shù)據(jù)用于確
91、保步進電機的速度和步驟。如果第一個數(shù)據(jù)是一個數(shù)字標記“49”,這意味著步進電機反轉,則單片機接收到發(fā)送來的速度和步驟信息。否則,單片機判定接收到的數(shù)據(jù)是不是“停止”指令,如果是這樣,它發(fā)出“停止”指令驅動系統(tǒng),如果沒有,它便退出中斷。D發(fā)送中斷處理子程序 發(fā)送中斷處理子程序負責將數(shù)據(jù)發(fā)送到計算機,中斷狀態(tài)一般為不允許中斷。當通信方案完全由PC微機發(fā)送的數(shù)據(jù)交換,代碼為“1”可寫入發(fā)送緩沖區(qū)。隨著計算機接收代碼“1”,這表明,單
92、片機上執(zhí)行完全由PC微機發(fā)送的指令。因此,串口中斷設置為“關閉”狀態(tài)的單片機,將它設定為“開”狀態(tài)的數(shù)據(jù)后再發(fā)送數(shù)據(jù)。這時,單片機返回到主程序和等待接收指令。</p><p> 五、PC和單片機通信程序的設計</p><p> 為了方便地實現(xiàn)系統(tǒng)軟件移植和統(tǒng)一,上位機程序的設計使用作為控制系統(tǒng)編程工具C++Builder 5.0 ,C++Builder 5.0是一個PC的編程工具。串行
93、口通信可以利用兩種方式:一是注冊組件,生成的C++本身并不提供單獨的串行通訊組件,但是它可通過已注冊的Microsoft MSComm32成分,生成相對簡單的VB,VC代碼。第二種方法是調(diào)用Windows API(應用程序接口)函數(shù),此函數(shù)是由操作系統(tǒng)提供,以提供大量的功能。該設計的步驟如下:</p><p> ?。?)設置通信協(xié)議,打開串行端口,這是所謂的初始化,然后阻止其他程序使用串行端口。</p>
94、;<p><b> (2)配置串口。</b></p><p> ?。?)傳遞串口轉來的數(shù)據(jù),驗證數(shù)據(jù)的傳輸過程。</p><p> ?。?)關閉串口,供其他程序使用。</p><p> 子接口很簡單,其中的子接口的參數(shù)設置,如波特率通信模塊的芯片已被設置為2400Bps,在計算機上的端口的默認設置2400Bps。微機具有兩個串
95、口,一般稱為COM1和COM2,以便程序可以自動識別串行端口,利于端口成功的發(fā)送和接收數(shù)據(jù)和指令。</p><p> 串行通信程序包括兩部分,一部分是MCS-51單片機的通信程序,另一部分是PC機的通信程序。在編寫程序之前,制定其雙方通信協(xié)議是十分重要的,否則將無法保證通信數(shù)據(jù)的可靠性,從而失去通信的意義?,F(xiàn)約定其通信協(xié)議如下:</p><p> ?。?)串行通信波特率為1200bps。
96、</p><p> ?。?)幀格式為:一位起始位,八位數(shù)據(jù)位,一位可編程的第九位(此位為發(fā)送和接收的地址/數(shù)據(jù)的標志位),一位停止位。</p><p> ?。?)設定單片機的地址碼為F1H。在傳送數(shù)據(jù)前先聯(lián)絡地址碼,如地址碼正確則傳送數(shù)據(jù),否則繼續(xù)聯(lián)絡地址碼。</p><p> ?。?)無奇偶校驗位。數(shù)據(jù)的通信采用累加和校驗的方法,即每傳送一組數(shù)據(jù)(個數(shù)自定,設為1
97、00個),校驗一次累加和是否正確,正確則返回00H,否則返回FFH。</p><p> (5)通信可以有中斷傳送方式和查詢方式。在這里介紹查詢方式通信。</p><p> (6)聯(lián)絡方式為PC機主動聯(lián)絡MCS-51單片機。</p><p> ?。?)PC機采用COM2通信。</p><p><b> 六、結論</b>
98、;</p><p> 串口通信的硬件設計基于RS-232,PC機和單片機之間進行通信的軟件模塊使用C++和匯編語言編程。該控制系統(tǒng)在產(chǎn)業(yè)領域運行良好,并通過控制單個步進電機啟動/停止和正轉/反轉旋轉。PC微機通過友好的用戶界面向單片機發(fā)送控制指令。</p><p><b> 參考文獻</b></p><p> [1]樊一支,姜文祥,陳梨園
99、.串行通信控制的C++生成和RS232.北京:清華大學出版社,2002年,第2-18</p><p> [2]茍?zhí)K艾.C++Builder5.0的的程序員指南.北京:北京希望電子出版社,2001年,第1-6</p><p> [3]劉艷玲.使用MAX232進行的MCS-51單片機與PC機之間的的通信.天津科技,1999年,第研究所15(2):第56-58</p><
100、p> [4]楊國霞.采用消息方式處理串口與主程序間的通訊.雜志:促進科技,2001年,河北省科學院卷.18(4):220-223頁</p><p> [5]黎步銀,江盛林,盧重,周東祥.基于C語言的串行通信程序設計.作者:Transcluction技術,2002,卷.(2)第65-66</p><p> [6]王志忠.單片機和PC機之間的串行通信.現(xiàn)代電子技術,2000,119
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 眾賞文庫僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 外文翻譯中英對照--基于rs-232的串行通信
- 外文翻譯--rs-232串行接口
- 畢業(yè)設計--基于單片機的rs-232c串行通信接口設計
- 單片機課程設計--利用rs-232實現(xiàn)單片機與pc間的串行通信
- 畢業(yè)論文外文翻譯-串行通信
- 電信類外文翻譯(英文+譯文)----串行通信
- 串行異步通信程序設計
- 畢業(yè)設計---串行通信接口設計
- 串行通信微機原理課程設計--串行通信實現(xiàn)文件傳輸
- 串行通信接口
- 雙機串行通信課程設計
- 雙機串行通信設計畢業(yè)設計
- 雙擊串行通信
- 串行通信75470
- sci串行通信
- 雙機串行通信的設計與實現(xiàn)
- 基于usb的串行通信軟硬件設計
- 串行通信75305
- usb轉rs―232串行適配器的設計
- 實驗二rs-232串口通信實驗
評論
0/150
提交評論