PIC18F4550 Application Design with Full-Speed ​​USB Interface

Publisher:ByteChaserLatest update time:2011-05-21 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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]

PIC18F4550 Application Design with Full-Speed ​​USB Interface

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.

PIC18F4550 Application Design with Full-Speed ​​USB Interface

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.

PIC18F4550 Application Design with Full-Speed ​​USB Interface

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.

Reference address:PIC18F4550 Application Design with Full-Speed ​​USB Interface

Previous article:Interface Design between Touch Sensor QST108 and PIC16F877A Microcontroller
Next article:Interface Design between USB Flash Drive and PIC Microcontroller

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号