【正文】
f good features and also has focused on areas of application. RS485 has a antinoise capacity transmitting data, so it is monly used in industrial production. USB interface and IEEE1394 have fast speed, but some puters and operating systems do not support such interfaces. RS232 is most widely used as a serial interface in a PC puter and munications industries. RS232C standard is a protocol of asynchronous serial munication and has been widely used in PC and munication industries. Many short distance municational peripherals of PC such as printers, disks and terminals all municate with PC via RS232 serial interface. RS232C standard RS232C is the American Electronics Industry Association (EIA) formally announced, in the asynchronous serial munication, the most widely used standard bus. The standard for DCE and DTE serial binary munication between the maximum data transfer rate of up to 19. 2kbps, the longest transmission cables up to 15 meters 1RS 232C standard defines the 25lead, twoway munication in general, just Serial input RXD, serial output TXD and ground GND1RS 232C standard levels using negative logic, the provisions of + 3V ~ + 15V at any level between a logic 0level, 3V ~ 15V of Any level between a logic 1 level, with TTL and CMOS levels are different. In the interface circuit and puter interface chip in the mostly level TTL or CMOS, so the munication, the need for level conversion, so with the RS 232C standard level matching 1MAX232 chip level conversion can be done in this work. 4 RS232 is defined as the singleended standards, which has much characteristic, such as more munication distance in a lowspeed serial munications, moderate price and good practicality of the system, so it is most appropriate to be a munication port. RS232 is an essential modern puter interface, it includes ports COM1 and COM2, the port COM1 has 9pin connector. The new generation puter has a 9pin connector (DB9) so as to connect with the RS232 munication port. In order to achieve reliable and real time transmission, the system adopt a threewire connection method, that is to say that the GND pin, RXD pin and TXD pin of RS232 port is connected with the external port, it is shown in Figure 1. Figure 1. The RS232 port III. THE HARDWARE DESIGN OF SERIAL PORT COMMUNICATION The inout level of single chip puter is TTL level, while the RS232 serial interface of a PC puter is standard serial interface, so both the electrical specifications are inconsistent. In order to municate with the upper and lower puter, it is needed that the out level of single chip puter is changed to the TTL level. The mon level converter chip between TTL and RS232 level is MCI1488 and MCI1489. The MCI1488 converts TTL level to RS232 level, it needs the supply voltage of 177。 R1OUT, R2OUT should take MCS 51 in the serial receive pins RXD1 corresponding RS232C level T1OUT, T2OUT should be connected to PCreceiver RD。 關(guān)鍵詞: 串行通信;單片機(jī);程序設(shè)計(jì) 一、緒論 在控制系統(tǒng)的工作中, PC 微機(jī)通過(guò)發(fā)送指令到步進(jìn)電機(jī)中來(lái)收集圖像信息。在電機(jī)的控制系統(tǒng)中, PC 微機(jī)被稱為上位機(jī),而由單片機(jī)所控制的步進(jìn)電機(jī)被稱為下位機(jī)。此外,電壓等級(jí)會(huì)因?yàn)檩旊娋€路的因素和電磁干擾而改變。從 IBM 個(gè)人電腦和兼容式電腦的時(shí)代開始,幾乎所有的計(jì)算機(jī)都配有 一個(gè)或多個(gè)串行端口和一個(gè)并行端口。對(duì)于并行通信,你需要采用 8條導(dǎo)線。 二、串口 通訊的簡(jiǎn)述 串行通信是指通信的發(fā)送方和接收方之間數(shù)據(jù)信息的傳輸是在單根數(shù)據(jù)線上 ,以每次一個(gè)二進(jìn)制位移動(dòng)。有些設(shè)備因?yàn)樵谕粫r(shí)間發(fā)送和接收數(shù)據(jù),被稱為全雙工設(shè)備。發(fā)送器和接收器必須設(shè)置為相同的數(shù)據(jù)通信比特或正確的比特率。波特率通常以每秒的位數(shù)( bps)來(lái)計(jì)量。使用校驗(yàn)位提供了一個(gè)基本的機(jī)制,以檢測(cè)已發(fā)送數(shù)據(jù)是否損壞,但不保證檢查數(shù)據(jù)本身的錯(cuò)誤。 串行通信有異步通信和同步通信兩種基本通信方式。在串行通信前 ,發(fā)送方和接收方要約定具體的數(shù)據(jù)格式和波特率 (通信協(xié)議 )。 MCS51系列單片機(jī)片內(nèi)含有一個(gè)全雙工的串行接口 ,通過(guò)編程也可實(shí)現(xiàn)串行通信功能。它們都有著自身的特點(diǎn),同時(shí)也分別適用于不同的的應(yīng)用領(lǐng)域。 RS232C 標(biāo)準(zhǔn)是異步串行協(xié)議溝通,并已廣泛應(yīng)用于個(gè)人電腦和通信產(chǎn)業(yè)。 RS232C 標(biāo)準(zhǔn)定義了 25根引線 ,對(duì)于一般的雙向通信 ,只需使用串行輸入 RXD,串行輸出 TXD和地線 GND。 RS232被定義為單端標(biāo)準(zhǔn),它具有許多的優(yōu)點(diǎn),如更低速的串行通信,通信距離遠(yuǎn),適中的價(jià)格和良好的系統(tǒng)實(shí)用性,所以它是最適用的一個(gè)通信端口。 圖 的端口 三、串行通信端口的硬件設(shè)計(jì) 單片機(jī)應(yīng)用的是 TTL 電平,而 PC 機(jī)的 RS232串行接口使用的是標(biāo)準(zhǔn)的串行接口標(biāo)準(zhǔn),所以兩者的電氣性能規(guī)格是不一致的。 12V 電源電壓,而 MCI1489可以將 RS232轉(zhuǎn)換成標(biāo)準(zhǔn)級(jí)的 TTL 電平,它需要電源電壓為 +5V。 MAX232芯片內(nèi)部有一個(gè)電源電壓變換器 ,可以把輸入的 +5V 電源變換成RS232C 輸出電平所需177。 MAX232的引腳 T1IN、 T2IN、 R1OUT、 R2OUT 為接 TTL/CMOS 電平的引腳,引腳 T1OUT、T2OUT、 R1IN、 R2IN 為接 RS232C 電平的引腳。不僅如此,它有許多優(yōu)點(diǎn),比如價(jià)格適中,硬件簡(jiǎn)單等等。當(dāng)單片機(jī)接收通過(guò) PC 電腦發(fā)送的數(shù)據(jù)信號(hào)時(shí),它就會(huì)調(diào)用中斷服務(wù)程序。 A 串口中斷子程序用于接收數(shù)據(jù) 用于接收數(shù)據(jù)的串行端口的中斷子程序,主要是將 PC 微機(jī)接收和發(fā)送的數(shù)據(jù)存儲(chǔ)到分配的內(nèi)存中(不進(jìn)行數(shù)據(jù)處理,以減少中斷的時(shí)間)。定時(shí)器從 0開始計(jì)數(shù)初始值 65535,然后中斷設(shè)定的定時(shí)器0被調(diào)用,在其中 。如果第一個(gè)數(shù)據(jù)是一個(gè)數(shù)字標(biāo)記“ 48”,串行口中斷分程序設(shè)置 ,那么接下來(lái)的五個(gè)字節(jié)的數(shù)據(jù)用于確保步進(jìn)電機(jī)的速度和步驟。當(dāng)通信方案完全由 PC 微機(jī)發(fā)送的數(shù)據(jù)交換,代碼為“ 1”可寫入發(fā)送緩沖區(qū)。 五、 PC 和單片機(jī)通信程序的設(shè)計(jì) 為了方便地實(shí)現(xiàn)系統(tǒng)軟件移植和統(tǒng)一,上位機(jī)程序的設(shè)計(jì)使用作為控制系統(tǒng)編程工具 C++Builder , C++Builder PC 的編程工具。 ( 2)配置串口。微機(jī)具有兩個(gè)串口,一般稱為 COM1和 COM2,以便程序可以自動(dòng)識(shí)別串行端口,利于端 口成功的發(fā)送和接收數(shù)據(jù)和指令。 ( 2)幀格式為 :一位起始位 ,八位數(shù)據(jù)位 ,一位可編程的第九位 (此位為發(fā)送和接收的地址 /數(shù)據(jù)的標(biāo)志位 ),一位停止位。數(shù)據(jù)的通信采用累加和校驗(yàn)的方法 ,即每傳送一組數(shù)據(jù) (個(gè)數(shù)自定 ,設(shè)為 100個(gè) ),校驗(yàn)一次累加和是否正確 ,正確則返回 00H,否則返回 FFH。 ( 7) PC 機(jī)采用 COM2通信。 18 參考文獻(xiàn) [1]樊一支 ,姜文祥 ,陳梨園 .串行通信控制的 C++生成和 :清華大學(xué)出版社 ,2020年 ,第218 [2]茍?zhí)K艾 .C++ .北京 :北京希望電子出版社 ,2020年 ,第 16 [3]劉艷玲 .使用 MAX232進(jìn)行的 MCS51單片機(jī)與 PC 機(jī)之間的的通信 .天津科技 ,1999年 ,第研究所 15( 2) :第 5658 [4]楊國(guó)霞 .采用消息方式處理串口與主程序間的通訊 .雜志 :促進(jìn)科技 ,2020年 ,河北省科學(xué)院卷 .18(4):220223頁(yè) [5]黎步銀 ,江盛林 ,盧重 ,周東祥 .基于 C 語(yǔ)言的串行通信程序設(shè)計(jì) .作者 :Transcluction 技術(shù) ,2020,卷 .(2)第 6566 [6]王志忠 .單片機(jī)和 PC 機(jī)之間的串行通信 .現(xiàn)代電子技術(shù) ,2020,119,第 6870