Design of USB interface virtual oscilloscope based on CH371

Publisher:算法之手Latest update time:2012-04-14 Source: dzscKeywords:CH371 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Introduction

For school teaching experiments and certain specific needs, the analog and digital oscilloscopes currently on the market may not be suitable. They are expensive, large in size, and many professional functions are not practical. Now that computers have become quite popular, using computers and additional data acquisition modules to realize a flexible and convenient virtual oscilloscope can meet most work, learning and development needs, and can achieve quite complex signal processing functions through low-cost hardware and software upgrades. It can realize a flexible configuration of intelligent instrument combinations at a low cost and small size; it can be combined with a portable computer to form a portable inspection and maintenance workstation. At present, there are data acquisition devices for computer parallel port communication, but the application of USB is becoming more and more extensive and in-depth. If the USB function is integrated into it, a higher data transmission rate and more convenient use can be achieved, which better reflects the performance of the virtual instrument.

2. Hardware Design

The data acquisition unit of this virtual oscilloscope consists of the following functional modules: front-end signal conversion module, high-speed analog-to-digital conversion module, high-speed data buffer module, single-chip control module, USB interface module and power module. There are mature solutions for front-end signal conversion and high-speed data acquisition, and they can be designed according to the required indicators, such as sampling rate, range control, sampling depth, etc. Here we mainly discuss the development of the USB interface part.

The development of USB interface has always been a headache. Although many designers yearn for the advantages of USB interface such as high-speed transmission, self-power supply and plug-and-play, it is difficult to overcome the obstacles of protocol firmware development and driver development. In this solution, the author selected a USB bus interface chip that does not require the development of protocol firmware and driver.

The CH371 launched by Nanjing Qinheng Company (www.winchiphead.com) is a universal interface chip for USB bus. It shields all protocols in USB communication by hardware logic and provides end-to-end connection between computer application layer and local controller. Based on CH371, designers do not need to understand any USB protocols, firmware programs and drivers, and can easily upgrade the original parallel and serial port products to USB interface, enjoying the advantages brought by USB interface with lower risk and cost.

When in use, on the local side, CH371 has an 8-bit data bus and read, write, chip select control lines and interrupt output, which can be easily connected to the system bus of controllers such as DSP and MCU just like operating general peripheral chips; on the computer side, the supporting software of CH371 includes universal drivers and application software packages, which provide a simple and easy-to-use operation interface. Communication with the local microcontroller is as simple as reading and writing files in the hard disk. Developers can use high-level languages ​​such as VB, VC, C++Builder for development. In general, when designing USB products based on CH371, it is not necessary to consider USB communication protocols, firmware programs, drivers, configuration processes, and underlying data transmission processes. The work that designers have to do is the same as designing parallel and serial port products, including two things: one is to send data transmission requests from the application layer of the computer and receive responses; the other is to respond when the controller of the USB product is notified of a data transmission request.

At the same time, CH371 not only provides transparent USB protocol, but also provides direct management of I2C devices, direct reading and writing of 16 addresses, as well as reset, watchdog and other functions. The local end can even complete simple control functions without using a single-chip microcomputer. Therefore, it has strong functional adaptability, can meet the needs of different occasions, and effectively reduce system costs.

The SOP28 package product is used in this system, and its pin definition is as follows:

According to different working modes, the interface types of the chip can be the following: passive parallel interface, I2C master interface, master control mode interface. Among them, the passive parallel interface is the most commonly used mode, which provides the connection between the microcontroller and the computer, and CH371 is a passive peripheral of the microcontroller; the I2C master interface is that CH371 directly reads and writes an EEPROM device (such as 24C04, etc.) without passing through the microcontroller under the control of the computer program, and is often used for product information recording of USB peripherals; the master control mode interface does not use the microcontroller, and the computer program directly controls the 8-bit data reading and writing of 16 addresses provided by CH371 or more than a dozen IO lines. This method is mainly used to complete relatively simple external control tasks. These interface modes are not completely opposite and can be flexibly configured as needed. In this virtual oscilloscope, it is used as a peripheral device of the microcontroller, and A3~A0 is used in the master control mode to control four LEDs by the computer.

The microcontroller controls CH371 by operating the 16 internal registers. There are two ways to define the addresses of these 16 registers. One is that after power-on reset, the default is the direct address mode, and the four pins A3~A0 are the register addresses. This method is relatively simple, but it requires more IO port resources of the microcontroller; the other is that when CH371 detects the rising edge signal on the ALE pin, it switches to the multiplexed address mode. The register address is controlled by D3~D0 of the data bus, and the jump of ALE is used to determine whether it is a register operation or data reading and writing. In this design, since the large-capacity data buffer needs to use 74373 as an address latch, even if the direct address method is used, it does not need to occupy more microcontroller resources, but in order to save A3~A0 to drive the LED, the multiplexed address method is selected. [page]

According to the system requirements, we designed the hardware as follows.

The USB interface provides data exchange and power supply (after appropriate conversion, it can be used for high-speed AD). CH371 works at a frequency doubling under the 12M crystal oscillator, provides a watchdog reset output function while providing an interface with the microcontroller, and the I2C master control function is not used. A3~A0 can be controlled by the computer to drive 4 LEDs as working display signals. The P1 port group of the microcontroller 89C52 is used to control the data acquisition module, including range conversion control, AD trigger signal, and data acquisition overflow signal; the cache 62256 stores the results of high-speed AD acquisition through low-power CPLD control. The microcontroller reads out the data regularly and transmits it to the computer's data display and analysis software through CH371 to realize the virtual oscilloscope function. The schematic diagram of the microcontroller, CH371 and RAM is as follows:

Figure 3 Schematic diagram of MCU, CH371 and RAM

3. Software Design

MCU software:

The MCU software mainly completes the two functions of data acquisition control and data reporting. The data acquisition control controls the data acquisition module according to the gain, time base, trigger and other parameters set by the computer software. The computer needs to send a small amount of control information to the MCU. The data reporting function requires the MCU to transmit all the data of a collection cycle in the cache to the computer software, which is a large-scale data transmission. We focus on the communication between the MCU and the computer.

The communication between the microcontroller and the computer is achieved by operating the registers of CH371. For details on the definition of its registers, please refer to the data manual.

It should be noted in the application that the MCU receiving buffer provided by CH371 is 8 bytes, and the computer download data exceeding 8 bytes is segmented; and the MCU sending buffer is also 8 bytes, and the upload data exceeding 8 bytes needs to be uploaded multiple times. In addition, the interrupt of CH371 includes upload and reception success, and the specific situation after the interrupt occurs needs to be identified by the transmission status register; at the same time, the interrupt level is monostable, and the interrupt signal jumps after an event occurs, but it only maintains a short pulse before recovery, which is for the purpose of coordinating with processors of different speeds.

The main function subroutine examples of USB communication between the MCU and the computer are as follows: (Please add the address definition of the corresponding register of CH371 when using it)
file://********** CH371 initialization program******************************
void ch371_init()
{
CH371_INT_SET=0; file://CH371 interrupt data setting register address offset
CH371_LENGTH=0X0F; file://Set the upload data length register to 15, indicating that there is no data to upload temporarily
save_length=0; file://Save the download data length
IT0=1; file://External signal is falling edge trigger
IE0=0; file://Clear interrupt flag
PX0=1; file://Set high priority
EX0=1; file://Enable interrupt
}
file://***********Upload data subroutine********************************
void ch371_upload(unsigned char len) file://Parameter incoming data length
{
unsigned char i;
unsigned char *up_buf;
unsigned char *ch371_buf;
if(len!=0)
{
up_buf=&ret_buffer; file://point to the data address
ch371_buf=&CH371_BUFFER; file://point to the data buffer
for(i=0;i{
*ch371_buf=*up_buf; file://write to the upload data buffer
ch371_buf++; up_buf++;
}
}
CH371_LENGTH=len; file://write the length of this data
}
file://********Interrupt subroutine using interrupt 0 register group 2********
void ch371_inter() interrupt 0 using 2{
unsigned char *down_buf;
unsigned char *ch371_buf;
unsigned char i;
IE0=0; file://clear interrupt flag
if( (CH371_STATUS&0x01) ==0 ){
file://read the status information register and judge the data transmission status
save_length=CH371_LENGTH; file://keep the length of the downloaded data
down_buf=&save_buffer;
ch371_buf=&CH371_BUFFER;
for(i=0;ifile://judge the length of the downloaded data and process it
{
*down_buf=*ch371_buf; file://read one byte of downloaded data
down_buf++; ch371_buf++;
}
}
else CH371_LENGTH=0x0f; file://set the upload data length register to 15
}[page]

Computer software:

The main function of the computer software is to simulate the interface of an oscilloscope, realize data display and analysis functions that meet different needs, and complete the parameter setting of the data acquisition hardware. Basic functions have been realized in this design, and the interface of the virtual oscilloscope is as shown below. After further development, automatic range conversion, in-depth data analysis (such as waveform measurement, spectrum analysis, etc.) can be realized, and even the waveform generator, logic analyzer, sweep frequency analyzer, network analyzer and other functions can be realized in combination with hardware upgrades.

Figure 4 Virtual instrument computer software panel diagram

The development of USB communication on the computer side does not require the understanding of the USB underlying driver, and the chip manufacturer has encapsulated the API functions for functional applications in the form of dynamic link libraries, which developers can call in a variety of high-level languages. The API functions provided by the CH371 dynamic link library mainly include: device management API, data transmission API, interrupt query API, I2C operation API and direct control API.

Among them, the device management API is not needed for general product development; the data transmission API is the most important and commonly used set of functions, which provides a variety of data transmission modes; the interrupt query API is generally used in the case of USB devices using interrupt transmission, which we do not use here; the I2C operation API is used to read and write I2C devices controlled by CH371, which is generally used to store product information, which we do not use here; the direct control API is used for CH371 to directly input and output signals, including settings, input and output. We use this set of APIs to control the LED.
In actual applications, the author uses VB6 as the development platform for computer-side application software. First, the driver and dynamic link library provided by Qinheng Company must be copied to the computer, and then the access entry and function definition entry of the dynamic link library must be added to the VB6 project, that is, the module function module in the provided VB6 example program can be added to your own project. At this time, the communication programming of the USB device is almost the same as accessing files in the local hard disk. The specific difference can be solved by analyzing the syntax of the example program. The following are the relevant programs for the computer to receive and send data.

Option Explicit \' Variable Definition
Dim myReq As DEMO_REQ
Dim mOpen As Integer

Private Sub Form_Load() \' Window loading event
Dim mLength As Long
mIndex = 0
mOpen = CH371OpenDevice(mIndex) \' Open the device when the form is loaded
If mOpen = -1 Then
MsgBox "Unable to open CH371 device" \' Prompt for loading failure
End If

myReq.mCommand = &HFF \' User defined command
myReq.mBuffer(0) = &H11 \' User defined data
myReq.mBuffer(1) = &H22 \' User defined data
mLength = Len(myReq)
CH371WriteData(mIndex, myReq, mLength) \' Send command data via CH371
...
CH371ReadData(mIndex, myReq, mLength) \' Receive response data via CH371
...
End Sub

IV. Conclusion

The virtual oscilloscope with USB interface designed by CH371 has completed the verification of basic functions and is being improved in the later stage. It can also launch a series of products of different levels for various applications. Practice shows that it has good technical performance, powerful functions, small size, easy to use and low price, and is a powerful assistant for electronic enthusiasts and developers.

Keywords:CH371 Reference address:Design of USB interface virtual oscilloscope based on CH371

Previous article:Software Design of Four-channel Multi-parameter Test System Based on LabVIEW
Next article:Design of thermal expansion instrument test system based on virtual instrument

Latest Test Measurement Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号