1 USB通信技術(shù)
USB(Universal Serial Bus)是一種目前通用的串行總線技術(shù),,它是通過PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI" title="PCI">PCI總線和PC的內(nèi)部系統(tǒng)數(shù)據(jù)線連接,實現(xiàn)數(shù)據(jù)的傳送,,具有即插即用的功能,,支持熱拔插,設(shè)備間線纜的長度可達5 m左右,。
USB接口信號線有4條線,,兩條是電源線與地線(Vbus與GND),兩條是以差分方式產(chǎn)生的信號線(D+與D-),,從而實現(xiàn)了主機系統(tǒng)與USB設(shè)備之間的數(shù)據(jù)通信,見圖1,。
圖1 USB通信數(shù)據(jù)流
USB總線121技術(shù)是基于分組交換方式的總線通信,。首先把數(shù)據(jù)分成若干塊,然后在每塊數(shù)據(jù)前添加上同步信號,、包標識,,后面添加上CRC校驗,形成USB封包,。USB總線通信使用一種差分的輸出驅(qū)動器來控制數(shù)據(jù)信號在USB電纜上的發(fā)送,,即通過控制D+和D一線從空閑態(tài)到相反的邏輯電平,,實現(xiàn)源端口的包發(fā)送。包發(fā)送結(jié)束后,,D+和D-上的輸出驅(qū)動器均處于高阻抗狀態(tài),。
2 USB通信系統(tǒng)設(shè)計
2.1 硬件設(shè)計
USB通信接口是連接Pc機和汽車尾氣檢測儀內(nèi)嵌單片機通信的橋梁,目前采用USB接口芯片來實現(xiàn)PC機與單片機AT89C5 1之間的連接是一種比較好的方法,。
由于汽車尾氣檢測系統(tǒng)內(nèi)嵌單片機是采用串口(TXD,、RXD)以RS232方式進行通信的,所以USB芯片與單片機AT89C51之間的連接方式也必須是RS232串口通信方式,。因此,,本文采用PROLIFIC公司推出的可實現(xiàn)USB接口轉(zhuǎn)RS232串口的轉(zhuǎn)換芯片PL一2303HX。根據(jù)汽車尾氣檢測儀USB通信系統(tǒng)設(shè)計要求,,USB通信接口電路設(shè)計如圖2所示,。
圖2 USB通信接口
USB插頭與USB芯片間的連接通過USB—D+、USB—D一,、GND和VO一3.3這4個端點實現(xiàn),,由于USB芯片PL2303HX支持類RS232接口,含有全雙工發(fā)送和接收(RXD,、TXD)串口,,所以,PL2303HX芯片與單片機AT89C51的RS232串口間直接可以用3根線RXD,、TXD,、GND連接,即USB芯片PL2303HX的RXD,、TXD和GND分別與單片機AT89C51的TXD,、RXD和GND連接,雙方都可發(fā)送和接收,。
2.2 軟件設(shè)計
2.2.1 通信方式
本文USB通信系統(tǒng)采用異步半雙工通信方式,,即通信的某一方發(fā)送和接收交替進行。通信的數(shù)據(jù)格式為每幀10位,,包括1位起始位,、8位數(shù)據(jù)位和1位停止位,見圖3,。
圖3 字節(jié)傳輸序列
這里設(shè)置單片機串行口采用T作模式l,,為10位異步通信,片內(nèi)定時器Tl作為波特率發(fā)生器,,波特率為2400bps,;由串行口控制寄存器SCON的工作模式選擇位SM0、SMI,,分別設(shè)為0和l,,從而設(shè)置其串行口為模式l,;當定時器/計數(shù)器Tl控制寄存器TMOD中C/T=0和M1M0=10時,AT89C51定時器/計數(shù)器Tl為定時器模式,,設(shè)置為方式2,。作為波特率發(fā)生器,其波特率表示為:
則:定時器Tl的初值應(yīng)設(shè)置為11Ll=THI=F3H,,另外應(yīng)禁止定時器Tl中斷,,以免因定時器T1溢出而產(chǎn)生不必要的中斷。
2.2.2 通信協(xié)議
進行通信之前,,通信雙方首先要根據(jù)系統(tǒng)的功能要求制訂某種特定的通信協(xié)議,,然后才能編寫相應(yīng)的通信程序。本文通信協(xié)議:
?、偻ㄐ烹p方均使用2 400 bps的速率傳送數(shù)據(jù),,PC機與AT89C51均工作于串口方式l(1位起始位、8位數(shù)據(jù)位,、l位停止位),。
②AT89C51在發(fā)送和接收數(shù)據(jù)時,,采用串口中斷方式,。
③PC機發(fā)出數(shù)據(jù)采集命令(握手信號),,單片機AT89C5l串口收到握手信號后,,立即將AT89C51內(nèi)存存放的檢測數(shù)據(jù)經(jīng)過USB通信接口發(fā)送到PC機,一次數(shù)據(jù)采集完成,。
USB通信程序流程e#
2.2.3 通信程序流程
通信程序涉及到PC機通信程序和AT89C51串口通信中斷程序,,上位機(PC機)發(fā)出數(shù)據(jù)采集命令,下位機(單片機AT89C51)收到握手信號后,,將數(shù)據(jù)傳回PC機,,從而完成一次數(shù)據(jù)采集,圖4和圖5是兩個通信程序的流程圖,。
圖4 PC機通信子流程
圖5 單片機串口中斷子流程
3 結(jié)語
本文設(shè)計的USB通信系統(tǒng)已在汽車尾氣檢測儀數(shù)據(jù)通信中得到了很好的應(yīng)用,。該系統(tǒng)具有采集數(shù)據(jù)準確可靠、使用靈活方便等優(yōu)點,,這種用USB通信技術(shù)取代傳統(tǒng)串口通信的方法切實可行,,有望在其他的智能儀器、儀表設(shè)備中得到借鑒和推廣應(yīng)用,。