基于ATmega8515的USB-CAN適配器的實(shí)現(xiàn)
圖5 EP2 的Rx 中斷子程序流程
2. 2 USB-CAN適配器驅(qū)動(dòng)程序編程
USB-CAN適配器設(shè)備驅(qū)動(dòng)程序采用Win2000DDK編寫(xiě),在VC6.0+ +環(huán)境中編譯. 驅(qū)動(dòng)程序運(yùn)行于內(nèi)核模式,其程序組織主要是按功能進(jìn)行劃分,而不是一種層次結(jié)構(gòu). 即插即用例程和電源管理例程完全按照USB 標(biāo)準(zhǔn)總線驅(qū)動(dòng)程序參考編寫(xiě).一個(gè)驅(qū)動(dòng)程序可以被多個(gè)類(lèi)似的硬件使用,但驅(qū)動(dòng)程序的某些全局初始化操作只能在第一次被裝入時(shí)執(zhí)行一次. 而DriverEntry例程就是用于這個(gè)目的,它是內(nèi)核模式驅(qū)動(dòng)程序主入口點(diǎn). I/O管理器按下面方式調(diào)用該例程:
extern“C”NTSTATUS DriverEntry ( IN PDRIVER OBJ ECT DriverObject , INPUNICODE STRING RegistryPath)
DriverEntry 的第一個(gè)參數(shù)是一個(gè)指針,指向一個(gè)剛被創(chuàng)建的驅(qū)動(dòng)程序?qū)ο?
DriverEntry 的第二個(gè)參數(shù)是設(shè)備服務(wù)鍵的鍵名.
本設(shè)計(jì)中該例程的各個(gè)域設(shè)置如下:
DriverObject - > DriverUnload =
UsbCan- DriverUnload ;
DriverObject - > DriverExtension - > AddDevice =
UsbCan- AddDevice ;
DriverObject - >MajorFunction[ IRP- MJ - CREATE] =
UsbCan- DispatchCreate ;
DriverObject - >MajorFunction[ IRP- MJ - CLOSE] =
UsbCan- DispatchClose ;
DriverObject - >MajorFunction[ IRP- MJ - READ] =
UsbCan- DispatchRead ;
DriverObject - >MajorFunction[ IRP- MJ - WRITE] =
UsbCan- DispatchWrite ;
DriverObject - > MajorFunction[ IRP- MJ - INTERNAL - DEVICECONTROL
] =
UsbCan- DispatchInternalControl ;
DriverObject - >MajorFunction[ IRP- MJ - POWER] =
UsbCan- DispatchPower ;
DriverObject - >MajorFunction[ IRP- MJ - PNP] =
UsbCan- DispatchPnp ;
DriverObject - > MajorFunction [ IRP- MJ - DEVICE- CONTROL ]
=
UsbCan- DispatchControl.
3 USB-CAN適配器的應(yīng)用
本文設(shè)計(jì)的USB-CAN適配器在分布式PLC 控制系統(tǒng)中的應(yīng)用如圖6 所示,筆記本電腦通過(guò)該適配器和PLC控制系統(tǒng)的CAN總線進(jìn)行連接,實(shí)現(xiàn)了一個(gè)筆記本對(duì)多個(gè)PLC進(jìn)行程序下載及對(duì)多個(gè)PLC運(yùn)行狀況進(jìn)行監(jiān)視,特別是在上位機(jī)只進(jìn)行監(jiān)視的情況下,可以進(jìn)行熱插拔而不影響系統(tǒng)的運(yùn)行,給系統(tǒng)調(diào)試帶來(lái)極大的方便. 經(jīng)USB測(cè)試軟件測(cè)試,其最高傳輸速率在40m內(nèi)達(dá)到了1Mb/s.
參考文獻(xiàn):
[1].ATmega8515datasheethttp://www.dzsc.com/datasheet/ATmega8515_.html.
[2].USBN9603datasheethttp://www.dzsc.com/datasheet/USBN9603_6750.html.
[3].SJA1000 datasheethttp://www.dzsc.com/datasheet/SJA1000+_609075.html.
[4].RS-232 datasheethttp://www.dzsc.com/datasheet/RS-232+_584855.html.
[5].RISCdatasheethttp://www.dzsc.com/datasheet/RISC_1189725.html.
[6].AT90S1200 datasheethttp://www.dzsc.com/datasheet/AT90S1200+_143803.html.
[7].EPC1datasheethttp://www.dzsc.com/datasheet/EPC1_300880.html.
[8].EPC2datasheethttp://www.dzsc.com/datasheet/EPC2_2058984.html.
[9].EPC4 datasheethttp://www.dzsc.com/datasheet/EPC4+_2058981.html.
[10].PCA82C200datasheethttp://www.dzsc.com/datasheet/PCA82C200_html.
[11].6N137datasheethttp://www.dzsc.com/datasheet/6N137_91364.html.
[12].PCA82C250datasheethttp://www.dzsc.com/datasheet/PCA82C250_.html.
[13].PLC datasheethttp://www.dzsc.com/datasheet/PLC+_1248813.html.
評(píng)論