introduction
With the development of USB (Universal Scrial Bus) technology, especially the emergence of high-speed (480 Mbps) USB2.0 protocol, almost all PC peripherals can be ported to USB, so the development space of USB PC peripherals is huge. Even in the near future, USB will completely replace asynchronous serial ports and printer parallel ports. PC manufacturers will no longer produce PCs with asynchronous serial ports and printer parallel ports on the chassis, and there will no longer be so many connections on the chassis.
USB is a fast, bidirectional synchronous transmission, inexpensive and hot-swappable serial interface. Using USB bus technology, various instruments and equipment suitable for scientific research and industrial production are developed to replace the instruments and equipment using serial RS232 or parallel interfaces in traditional computer measurement and control systems, making computer measurement and control systems more efficient, real-time, convenient and flexible.
There are two data acquisition schemes using the USB bus. One is to use an ordinary microcontroller plus a dedicated USB communication chip. This scheme can make full use of the original hardware resources and software knowledge of developers, and the development cost is low, but the system design and debugging are more troublesome, and the electromagnetic compatibility is poor, which easily causes the host to fail to recognize the USB device. The other is to use a microcontroller with USB interface function. The data acquisition system composed of these dedicated chips has simple circuit design, convenient debugging, and good electromagnetic compatibility. However, most of the microcontrollers with USB functions are currently designed specifically for personal computer peripherals or the consumer market, ignoring the needs of embedded engineers. The advent of Microchip's new PIC18F4550 series microcontrollers has added the advantages of full-speed USB to a wide range of embedded applications, and can work normally even in harsh operating environments or when it can only be connected to a personal computer at irregular intervals.
This paper takes the design of grating displacement sensor detection system as the background, and introduces in detail the application design method of PICl8F4550 microcontroller with full-speed USB interface.
1 PIC18F4550 Introduction
PICl8F4550 is the latest 8-bit high-end microcontroller with full-speed USB interface produced by Microchip. The chip is packaged in 40/44 pins. In addition to the unique reduced instruction set (RISC) and Harvard structure of independent data bus and instruction bus of PIC microcontroller, the microcontroller is also equipped with self-programming flash memory and nanowatt energy-saving technology, with an operating frequency of 48 MHz and a data transmission rate of up to 12 Mbps. In addition, it also has Microchip's advanced PMOS electrically erasable unit (PEEC) flash memory technology, which can withstand up to 1 million erase and write times, and the data retention period can exceed 40 years. Therefore, the chip has strong control capabilities and flexible working methods. [page]
The new device's full-speed USB 2.0 interface has 1 KB dual-access RAM, supports up to 32 endpoints (16 pairs in bidirectional mode) and two data transfer rates (i.e., full-speed mode 12 Mbps and low-speed mode 1.5 Mbps), and has four data transfer modes specified by the USB protocol (control transfer mode, interrupt transfer mode, bulk transfer mode, and real-time transfer mode). The interface includes an on-chip transceiver and a parallel stream port, which can directly transmit data to external devices, not only reducing CPU overhead, but also greatly enhancing the system's anti-interference ability and working reliability.
The hardware resources of PIC18F4550 are very rich, including 33 I/Os, I/O ports are port A, B, C, D, E; multiple interrupt sources and 1 interrupt priority selection, 4 timers, 32 KB program flash memory, 256 bytes EEP-ROM data memory, 2 048 bytes data random memory and 8×8 hardware multiplier; integrated 13 channels of 10-bit A/D converter, 2 CCP (compare/capture/PWM) modules, 1 enhanced CCP module and 1 watchdog; sleep mode with power saving function; 1 USART not only supports asynchronous and synchronous serial communication, but also supports LIN bus; 2 analog comparators, master synchronous serial port supporting I2C and SPI communication, programmable undervoltage reset and low voltage detection circuit, etc.
A key feature of the PIC18F4550 microcontroller is that it is equipped with 32 KB of self-programmable enhanced flash memory. This allows designers to upgrade the final application in the field through the USB port. Combined with the new device's range of on-chip peripherals and nanoWatt power management features, it is ideal for a variety of embedded applications, including industrial, medical, automotive, battery-powered and consumer products.
2 Design and Application of PICl8F4550
2.1 Basic working principle and characteristics of grating displacement sensor
The basic working principle of the grating displacement sensor is to use a pair of gratings, one of which is fixed and the other is moving. When they move relative to each other and light passes through them, a fringe signal equivalent to that obtained in an interferometer can be obtained, which is the so-called "Moiré fringe signal". For a pair of metrological gratings, one Moiré fringe moves when one groove is moved relative to each other (modern metrological gratings often use 4 to 250 L/mm). The fringe width is not affected by the wavelength. And the wider the fringe, the more interpolated it can be. Due to the error averaging effect, a very high accuracy can be obtained. Compared with ordinary displacement sensors, it has the following characteristics: [page]
① High precision. The grating displacement sensor is only inferior to the laser interferometer sensor in terms of long-range measurement of length or linear displacement; in terms of circular indexing and angular displacement measurement, the grating sensor has the highest precision.
② Large-range measurement with high resolution. Inductive synchronizers and magnetic grating sensors also have the characteristics of large-range measurement, but their resolution and accuracy are not as good as grating displacement sensors.
③ Dynamic measurement is possible, making it easy to automate measurement and data processing.
④ It has strong anti-interference ability, and its requirements for environmental conditions are not as strict as those of laser interferometer sensors, but it is not as adaptable as inductive synchronizers and magnetic grating sensors. Oil and dust will affect its reliability, and it is mainly suitable for use in laboratories and workshops with good environments.
2.2 PIC18F4550 Hardware Design
The hardware design of the USB interface of PIC18F4550 is relatively simple, while the peripheral devices controlled by the microcontroller belong to the general microcontroller design, and can refer to the design method of the general PIC microcontroller. For the USB interface, Micrachip provides a development kit (Demo board) to help users develop their own products. It provides applications, drivers and firmware to help users familiarize themselves with the working process of USB; at the same time, the development kit can be directly used to expand peripheral devices.
Figure 1 is a hardware circuit diagram of a grating displacement sensor detection system, which mainly realizes data collection, processing, transmission and PWM pulse generation. There are 5 signals input from the grating sensor: ±sin, ±cos and zero window signal zero. After being synthesized by the differential amplifier circuit, they are divided into 3 channels and enter the analog input port of the PIC microcontroller for A/D conversion; at the same time, the sin and cos signals synthesized by the differential amplifier circuit are converted into digital pulse signals after passing through the zero comparator, and then the direction of the grating displacement is identified through the D flip-flop and the "AND" gate circuit. The T0 and T1 ports of the PIC microcontroller receive the digital pulses from the "AND" gate circuit to complete the counting of the grating displacement (calculate the number of complete gratings moved by the grating ruler); the RCl port of the PIC microcontroller outputs a 4 MHz PWM pulse signal as the CP signal of the D flip-flop. The two data lines of the USB interface of the computer are connected to the D+ and D1 ports of the PICl8F4550 respectively, which are used to complete the data communication between the computer and the microcontroller. The computer's USB power supply provides energy for the PIC microprocessor on the one hand, and on the other hand, it is converted into ±12V power through the power module to provide positive and negative power for the op amp circuit. [page]
2.3 MCU software design
The software part of the microcontroller mainly completes the data acquisition of the optical displacement sensor, A/D conversion, calculates the number of positive and reverse Moiré fringes of the grating displacement sensor, provides CP pulses for the digital circuit, and completes USB communication. Figure 2 is the flow chart of the microcontroller software part.
2.4 PIC18F4550 firmware design
Microchip provides a series of USB registers that can be used to complete USB communication. Most USB communications are completed through interrupts. In the USB interrupt service routine, the input/output interface must be implemented, allowing most USB programs to be completed in the background. From the application's point of view, the enumeration process and data communication seem to have no connection.
For the single-chip control program, no manufacturer currently provides tools to automatically generate firmware, so all programs must be compiled manually. Due to the complexity of the USB protocol and considering the needs of the majority of customers, Mictochip provides DEMO programs for different customer groups when launching the PIC18F4550 series chips, which greatly reduces the burden on system developers and shortens the development cycle. The design of this system is completed by making necessary modifications based on the DEMO program provided by Microehip. The specific firmware of this design is mainly composed of the following 8 files.
①main.c: The main program of the system, including two subroutines: InitializeSystem() and USBTasks(void), which mainly complete the initialization of the system and call various other subroutines.
②usb9.c: implements the functions of Chapter 9 of the USB protocol, including enumerating the bus interface and core functions, as well as the USB interrupt service routine. It handles all interrupts generated by USB users. In this program, the enumeration of descriptors and the sleep and reset functions are mainly implemented, mainly including 5 subroutines such as USBCheckStdRequest(void), USBStdGetDscHandler(void) and USBStdFeatureReqHandler(void). [page]
③usbctrltrf.c: mainly implements various functions required for USB control transmission, mainly composed of 8 subroutines such as USBCtrlTrfSetupHandler(void), USBCtr_IEPServieeCompiete(void) and USBPrepareForNextSetupTrf(void).
④usbdrv.e: mainly implements functions related to USB driver, including checking bus status, USB module enable, USB module suspend, remote wake-up and other functions; mainly consists of 12 subroutines such as USBSuspend(void), USBModuleEnable(void), USBSoftDetach(void) and USBDriverService(void).
⑤usbdsc.c: mainly a description of the system, including the manufacturer, product number, etc.
⑥usbgen.c: Configuration of USB device class (here it is configured as general USB class), mainly including initialization endpoint and reading and writing of USB general class, which consists of three subroutines: USBGenlnitEP(void), USBGenRead(byte*buffer, byte len) and USBGenWrite(byte*buffer, byte len).
⑦usbmmap.c: Mainly used to check the match between the endpoint number and the BDT (buffer descriptor table) during USB communication.
⑧user.c: The application program directly facing the user can help the user complete his/her initialization configuration, read/write processing data, process I/O ports, request of equipment, etc. Most functions of the lower computer in this design are realized by this program, such as A/D conversion and data reading, 1MHz PWM waveform generation, etc. It mainly includes six subroutines: UserInit(void), BlinkUSBStatus(void), ServiceRequests(void), ProcessI()(void), ResetTempLog(void) and ReadPOT(void).
2.5 PC Software Programming
To write software on a PC, you can use the development kit provided by Microchip. The kit is an installation program. After installation, you can find USB universal drivers, applications written in Borland C, some firmware codes, and API functions required for writing applications in the installation log. For general applications, the driver can be used directly without recompiling. The following 7 API functions are mainly used in programming: MPUSBGetDLLVersion(), which returns the version number of the DLL; MPUSB-GetDeviceCount(), which returns the number of connected devices; MPUSBOpen(), which returns the handle of the USB device endpoint with the specified pVID_PID and pEP, and returns an endpoint handle; MPUSBRead(), which reads data from the IN endpoint and fills the buffer; MPUSBWrite(), which writes data to an OUT endpoint and uses the pData buffer; MPusBReadInt(), which reads data from the Interrupt IN endpoint and fills the buffer; MPUSBClose(), which closes the handle of an endpoint. Because the MPUSBAPLDLL source program provided by Microchip is written in Borland C, this program cannot be compiled in the VC environment, so it cannot generate the implicit call required. LIB file, so this design uses explicit linking when using DLL.
The software part of the PC is mainly used to realize the USB communication between the upper computer and the lower computer, the subdivision and display of the displacement signal of the grating displacement sensor, etc. There are many ways to realize this function, such as using simple VB, Delphi with strong data processing capabilities, or Borland C and VC widely used at home and abroad. In order to develop the next software and interface with other software of the manufacturer, the software part of the upper computer of this system is implemented using VC++6.0. Figure 3 is the flow chart of the PC software part.
Conclusion
Choosing which chip to design an embedded system requires consideration of system requirements, design difficulty, and cost-effectiveness. Micro-chip provides universal drivers, API functions, DEMO boards, and a series of sample programs for the PICl8F4550 series of microcontrollers. Among them, some firmware endpoints and subroutines for communicating with PCs can be called directly without rewriting, which greatly improves development efficiency and shortens the product development cycle. This will inevitably lead to this series of microcontrollers quickly occupying a part of the USB interface market. It is precisely because of the many advantages of USB and the fact that grating displacement sensors have many advantages that other displacement sensors cannot match, so the application of this design will become more and more extensive.
Previous article:Interface Design between Touch Sensor QST108 and PIC16F877A Microcontroller
Next article:Interface Design between USB Flash Drive and PIC Microcontroller
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Python Machine Learning Basics Tutorial
- Can pure software development be transferred to embedded software development?
- GD32L233C-START Evaluation——02_1. Build development environment and simple debugging
- The Painful Experience of DSP Connecting to Emulator
- C2000 floating point calculation notes - differences between CPU and CLA and error handling techniques
- When Kicad moves a line individually, the line is actually disconnected when moving. How to solve this problem?
- EEWORLD University ---- Theoretical basis of IoT terminal development
- About the serial port problem of MSP430F5529 microcontroller
- Mercury-containing thermometers will be banned from 2026? What do you think is the best alternative?
- Help: AT32F425 pinout information