Summary of the invention
The object of the present invention is to provide a kind of usb bus adapter based on microcontroller, can be used for the equipment on the CAN bus is monitored, the microprocessor controls data are transmitted between USB socket and CAN bus, solved " bottleneck " problem of communicating by letter between CAN bus and PC, the data transmission credibility height.
For achieving the above object, the present invention adopts following technical proposals:
A kind of USB-CAN bus adapter based on microcontroller, comprise USB socket that connects PC and the CAN bus interface that is connected the CAN bus, it is characterized in that being connected described USB socket and CAN bus interface by a usb bus controller with a CAN bus control unit respectively, realize transfer of data between PC and the CAN bus by a microcontroller.
Above-mentioned microcontroller adopts the microcontroller ATmega 8515 of the RISC framework of Atmel company release.
In the above-mentioned USB-CAN bus adapter based on microcontroller, the power supply that utilizes the USB socket to provide is directly powered to microcontroller ATmega 81515; Between CAN bus control unit and CAN bus interface, adopt photoelectric isolating device to realize being connected, and adopt a DC/DC supply convertor to give photoelectric isolating device and back end device power supply thereof, the electrical Interference between thorough isolated can bus and the PC.
In the above-mentioned USB-CAN bus adapter based on microcontroller, microcontroller connects an indicating circuit: be connected a usb bus indicator light (USB-LED), a CAN bus indicator light (CAN-LED) and the indicator light of communicating by letter (COM-LED) respectively between the PD0 of microcontroller ATmega 8515 pin, PD1 pin and PD4 pin and power supply.
In the above-mentioned USB-CAN bus adapter based on microcontroller, microcontroller connects a keyboard: be connected a switch reset key (JP1) between the PE2 of microcontroller ATmega 8515 pin and RESET pin and power supply.
Above-mentioned CAN bus control unit adopts the independent CAN bus control unit SJA1000 of PHILIPS company.
Above-mentioned usb bus controller adopts device USBN9603 at full speed.
In the above-mentioned USB-CAN bus adapter based on microcontroller, the software of implantation comprises:
(1) the USB-CAN adapter firmware programs among the microcontroller ATmega 8515: adopt the latest edition WinAVR programming of GNUAVR, it can be programmed with C or C Plus Plus to all AtmelAVR, and can move under Win2000;
(2) programming of the USB-CAN adapter drivers in the PC: adopt Win2000DDK to write, in the VC6.0++ environment, compile.
The present invention compared with prior art, have following conspicuous outstanding substantive distinguishing features and remarkable advantage: the present invention has designed the adapter of a kind of usb bus to the CAN bus, adopted microcontroller to be connected CAN bus interface and USB socket with the usb bus controller through the CAN bus control unit respectively, by the transmission of microprocessor controls data between USB socket and CAN bus, solved communication " bottleneck " problem between CAN bus and PC, data transmission bauds can reach the high-transmission efficient 1Mb/s of CAN bus between PC and the CAN bus, and the data transmission credibility height.
Embodiment
A preferred embodiment of the present invention accompanying drawings is as follows: this USB-CAN bus adapter based on microcontroller comprises following hardware and software.
USB-CAN bus adapter hardware:
Shown in Figure 1, this USB-CAN bus adapter based on microcontroller mainly comprises microcontroller 1, usb bus controller 8, CAN bus control unit 3, indicating circuit 2, electric power management circuit.Microcontroller 1 is the hinge of whole system, usb bus controller 8 is full speed usb bus Node Controllers, CAN bus control unit 3 is independent CAN bus control units in the field of industrial automatic control, indicating circuit 2 and electric power management circuit are the system supplymentary circuit, and PC can monitor the equipment of CAN bus 6 by the USB socket.Dividing whole hardware system according to the communication between microcontroller 1, usb bus controller 8 and the CAN bus control unit 3 is 3 parts, is introduced below.
<1〉microcontroller ATmeag8515 telecommunication circuit
ATmeag8515 type microcontroller is the RISC framework, nearly 130 powerful instructions (most of only with a clock cycle execution) are arranged, 32 * 8 general purpose working registers, static fully running, the speed of service can reach 16MIPS under the crystal oscillator of 16MHz, the multiplier in 2 cycles on the sheet, its arithmetic speed is more than 10 times of present 8051 single-chip microcomputers of using always, can significantly reduce the time of Coordination Treatment between usb bus controller 8 and the CAN bus control unit 3 with it, improve the practical communication speed of USB-CAN bus adapter.Microcontroller 1 has non-volatile program and data storage, the online programmable Flash of 8K byte; The EEPROM of 512 bytes, can carry out 100,000 times erasable, the important parameter that is provided with of USB-CAN adapter still can be preserved under power-down conditions; Therefore the SRAM of 512 bytes can select the compiler that the AVR microcontroller is carried out C or C++ programming for use.
As shown in Figure 2, ATmeag8515 can expand the external program memory up to the 64K byte, ALE (being that external data memory address speed enables) pin links to each other with the ALE/AS of SJA1000 and the A0/ALE/SI of USBN9603 respectively, and SJA1000, USBN9603 are respectively 0x7F00H~0x7F7FH, 0xBF00H~0xBFFFH in the space address of ATmeag8515; Multiplexing ATmeag8515PA mouth links to each other with D0~D7 of AD0~AD7, the USBN9603 of SJA1000 respectively as data wire and carries out transfer of data; The ATmeag8515PB mouth acts on SJA1000, USBN9603 as controlling mouth, and wherein PB0, PB1 reset to control mouth, and PB2 is a DMA control mouth, and PB3, PB4 are USBN96033 kind model selection control mouth, as shown in Figure 5; The reading and writing that PD7, PD6 are respectively SJA1000, USBN9603 enable the control mouth, PD2, PD3 are respectively the interruption output enable control mouth of SJA1000, USBN9603, and ATmega8515 carries out condition diagnosing and event handling timely according to PD2, PD3 to SJA1000, USBN9603.
As shown in Figure 2, the operation JP1 key switch ATmeag8515 that resets, ALE is set latchs USBN9603, SJA1000, PC6 is set, the PC7 selection control, PB3 is set, PB4 selects mode of operation, realize that by ATmeag8515 control data are at USBN9603, transmit between the SJA1000, if ATmeag8515 detects and need carry out transfer of data from SJA1000 to USBN9603, and satisfy the condition (with reference to USB-CAN adapter firmware programs) of software setting, PC7 is set effectively selects SJA1000, PD7 effectively enables SJA1000 RD/E mouth, ATmeag8515 utilizes multiplexing PA mouth to read data on AD0~AD7 mouth of SJA1000, PC6 is set then enables to select USBN9603, PD6 effectively enables USBN9603 WR/SK mouth, writes into data from D0~D7 mouth; If need from USBN9603 to SJA1000, carry out transfer of data, condition satisfies, PC6 at first is set effectively selects USBN9603, PD7 effectively enables SJA1000 RD/E mouth, ATmeag8515 utilizes multiplexing PA mouth to read data on D0~D7 mouth of USBN9603, and data are put in the internal RAM, whether detect then SJA1000 satisfies and accepts data, if condition satisfies, PC7 is set enables to select SJA1000, PD6 effectively enables the SJA1000WR mouth, and the data that are temporary among the RAM are write into SJA1000 from D0~D7 mouth.
<2〉usb bus controller USBN9603
USBN9603 type usb bus controller 8 is full speed usb bus Node Controllers, and transmission data baud rate reaches as high as 12Mb/s, and inside is integrated with the adjuster of transceiver, frequency multiplier circuit PLL, 3.3V.
As shown in Figure 3, inner integrated transceiver port is respectively D+, D-, terminal resistance by 18~27 Ω is connected with USB cable, adjuster port V3.3 is connected on the D+ data wire of USB by the pull-up resistor of a 1.5K Ω, can make the USB master controller identify a USB full speed equipment and connect.
Frequency multiplier circuit PLL can reduce EMI, and has the programmable clock output function, and under the power-on reset state, default frequency is output as 4MHz, by microcontroller programming CLKDIV is set, and its output frequency is:
CKout=48MHz/(CLKDIV+1)
Wherein CLKDIV is the integer between 0~15, and when CLKDIV=2, Ckout is output as 16MHz, and the CLKOUT pin links to each other with ATmeag8515 XTAL1, and 16MHz clock source is provided.
USB socket power supply is+5VDC, the maximum electric current that 500mA can be provided, and the ATmeag8515 supply power voltage is at 4.5~5.5VDC, maximum current is 200mA between Vcc and the GND, so the power supply that utilizes USB socket 7 to provide in the circuit design of adapter is directly powered to ATmeag8515, has removed power circuit from.
USBN9603 has 7 end points based on FIFO: an essential two-way control end points 0 (8 byte buffer memory), and three send end points EPC1, EPC3 and EPC5 (each 64 byte buffer memory), and three meet end points EPC12, EPC4 and EPC6 (each 64 byte buffer memory).Make full use of the FIFO of USBN9603, can make ATmega8515 microcontroller and the parallel running of USBN9603 controller, improved the operational efficiency of adapter greatly.
<3〉CAN bus control unit SJA1000
As shown in Figure 4, SJA1000 type CAN bus control unit 3 is independent CAN bus control units in the field of industrial automatic control, it is the continue next-generation of PCA82C200 of Philips Semiconductors company, on the BasicCAN of PCA82C200 pattern basis, increased the PeliCAN pattern that meets CAN 2.0 agreements regulation, so it is compatible with PCA82C200 on electric; Support 11 bit identifier ID and 29 bit identifier ID; Be up under the crystal oscillator of 24MHz, traffic rate reaches 1Mb/s; Send 13 bytes of buffer memory, receive and be cached with 64 bytes, adopt fifo mode, reduced the reception stand-by period, improved practical communication efficient.USB-CAN adapter data transmission and processing block diagram as shown in Figure 4, the data address of CAN bus control unit links to each other with the PA mouth of ATmega8515 for multiplexing mouthful, the INT1 pin of its interruption Enable Pin and ATmega8515 links to each other, and makes ATmega8515 to carry out condition diagnosing and event handling timely to SJA1000; The Serial Data Transfer Mode end links to each other with PCA82C250 type CAN controller 5 interfaces by 6N137 type photoelectric isolating device 4, and the stability of USB-CAN bus adapter and antijamming capability are greatly improved.
The USB-CAN adapter software:
The USB-CAN adapter software comprises two parts: the drive software of USB-CAN adapter in firmware among the ATmega8515 and the PC.
<1〉USB-CAN adapter firmware programs:
The USB-CAN bus adapter adopts the latest edition WinAVR programming of GNU AVR, and it can be programmed with C or C Plus Plus to all Atmel AVR, and can move under WIN2000.
USB-CAN adapter firmware design becomes drives interrupts completely, and when ATmega8515 handled foreground task, the transmission of USB and CAN can be carried out simultaneously on the backstage, improves actual transmissions efficient, is easy to programming and debugging.The Interrupt Process subprogram on backstage by event flag and data buffer zone be set realize and the foreground main program between exchanges data, as shown in Figure 6.When ATmeag8515 detects the status register of CAN controller and learns that receiving buffering CAN_RX_Buffer has data, will pass to data among the transmission buffering area TXFIFO-1 or TXFIFO-2 of free time of USB controller; Equally, when the reception buffering area RXFIFO-1 of USB controller or RXFIFO-2 are full of data, ATmeag8515 will read inner continuous ram space to the data of RXFIFO-1 or RXFIFO-2, when CAN bus control unit transmission buffering area CAN_TX_Buffer is sky, again data are passed to CAN_TX_Buffer from RAM.
Adopt the ping-pong buffer method to improve the actual transmissions digit rate in the USB-CAN adapter firmware design.USBN9603 has 7 end points, corresponding pipeline of each end points when the data in the FIFO transmit or receive usually, but, the end points of two equidirectional transmission data also can utilize a pipeline, can effectively improve the transfer rate of data like this, because when the FIFO of first end points transmission data, microcontroller can carry out data and read or write to the FIFO of second end points, vice versa.USBN9603 is when carrying out bulk transfer, and its actual delivery rate approximately is 512Kb/s, and this is often because the controller of main frame maximum every frame data bag restricted number causes rather than because the transmission performance of USBN9603 own.Other transmission means is 1.5Mb/s as interrupting the high-transmission digit rate of transmission, synchronous transmission, control transmission.After adopting the ping-pong buffer method, the transmission digit rate is doubled, be that the CAN bus control unit of 1Mb/s is not waited for the situation that receives data and send data space thereby make transmission rate, solved " bottleneck " problem in the transfer of data.
In the USB-CAN adapter, end points EP1 (TXFIFO-1) and EP3 (TXFIFO-2) send data to main frame, and EP2 (RXFIFO-1) and EP4 (RXFIFO-2) be from host receiving data, and as shown in Figure 6, the data length among their FIFO all is 64 bytes.When main frame carries out in the enumeration process, when node receives the configuration device request, firmware program just can distribute same end-point addresses to EP1/EP3 and EP2/EP4 respectively.USBN9603 is built-in with a priority list, if same endpoint number is appointed as in a plurality of end points programming, has low number end points (utilizing the TOGGLE sign to distinguish height) meeting with a response and data at first.
Data upload:
When main frame sends an IN token to the USB-CAN adapter, adapter just begins to send data to PC.Be illustrated in figure 7 as the flow chart of data upload.By two sign UPDATE1 and UPDATE2 main program and Tx interruption subroutine are closely linked, have embodied the foreground-background programs design philosophy.When UPDATE1 is clear 0, upload data and finish, when putting 1, begin to transmit the next group data.Fig. 8 is the Tx interruption subroutine flow chart of EP1, and UPDATE1 puts 1 at this interruption subroutine.The operation of UPDATE2 is similar with UPDATE1.
Data down transmission:
When main frame sends an OUT token to the USB-CAN adapter, adapter just begins to receive the data that PC sends, and being temporarily stored among the RAM of ATmega8515, the data that pass to the CAN bus control unit then send buffering area CAN_TX_Buffer.Fig. 9 is the Rx interruption subroutine flow process of EP2, has shown how the data of PC pass to the transmission buffering area of CAN bus.The Rx interruption subroutine flow process of end points EP4 and EP2's is similar.
<2〉USB-CAN adapter drivers programming
USB-CAN adapter device driver adopts Win2000 DDK to write, and compiles in the VC6.0++ environment.Driver runs on kernel mode, and its program organization mainly is to divide by function, rather than a kind of hierarchical structure.Plug and play routine and power management routine are write according to the reference of USB STD bus driver fully.
A driver can be used by a plurality of similar hardware, but some overall initialization operation of driver can only be carried out once when being loaded into for the first time.And the DriverEntry routine is used for this purpose exactly, and it is the kernel-mode driver main entry point.The I/O manager calls this routine by following mode:
extern″C″NTSTATUS?DriverEntry(IN?PDRIVER?OBJECT?DriverObject,INPUNICODE?STRING?RegistryPath)
First parameter of DriverEntry is a pointer, points to a driver object that just has been created.
Second key name that parameter is the device service key of DriverEntry.
Each territory of this routine is provided with as follows among the design:
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_DEVICE_CONTROL]=
UsbCan_DispatchInternalControl;
DriverObject->MajorFunction[IRP_MJ_POWER]=
UsbCan_DispatchPower;
DriverObject->MajorFunction[IRP_MJ_PNP]=
UsbCan_DispatchPnp;
DriverObject->MajorFunction[IRP-MJ_DEVICE_CONTROL]=UsbCan_DispatchControl;
This is based on the application example of the USB-CAN adapter of microcontroller:
The application of USB-CAN adapter in distributed PLC control system as shown in figure 10, notebook computer is connected by the CAN bus of this adapter and PLC control system, realized that a notebook carries out program to a plurality of PLC and downloads and a plurality of PLC operation conditionss are monitored, particularly only carry out under the surveillance scenarios at host computer, can carry out hot plug and do not influence the operation of system, bring great convenience to system debug.Through USB testing software test, its high-transmission digit rate has reached 1Mb/s in 40m.