0Introduction
In recent years, with the continuous development of integrated circuits and chip manufacturing technology, different USB communication chips have emerged. At present, typical USB communication chips include CH375, PDIUSBD12, etc. These devices are characterized by powerful functions, but high prices, and are difficult to use in the development of medium and low-priced products. The AVR USB technology uses the IO port of the AVR microcontroller with a high-performance 8-bit RISC architecture to simulate the USB communication port, and implements the USB communication protocol by software to simulate the ordinary AVR microcontroller into a USB low-speed device, thereby realizing the communication and control between the AVR microcontroller and the computer. Although this method can only achieve low-speed transmission, it is sufficient for systems with a small amount of commonly used data, and its speed is much higher than the traditional serial port transmission speed, so it has broad application prospects in product development. This article uses AVR USB technology to achieve the measurement and display of temperature.
1 System lower computer software process
The lower computer software design and implementation can be based on the WINAVR programming environment, and the code can be implemented using C language. The AVR microcontroller mainly completes two functions: temperature measurement and data transmission. Temperature measurement mainly samples the voltage signal to determine the discrete value of the temperature. Data transmission is to transmit the measured data to the PC through the USB port simulated by the AVR so that the PC can display and process the data. The main process is shown in Figure 1.
2. Implementation of software functions
2.1 Temperature data collection
ATmega8 contains a 10-bit successive approximation ADC. This ADC can be connected to an 8-channel analog multiplexer to sample 8 single-ended input voltages from port C. The single-ended voltage input is referenced to 0 V (GND). The timing diagram of AD conversion is shown in Figure 2.
For continuous AD data collection, the query method and the interrupt method can be used. The program uses the interrupt method, that is, an interrupt is generated after an AD conversion is completed. When designing, the following function can be used to collect voltage values:
First, define a global variable Value to store the measured temperature value and use the value as the send value of the USB port.
2.2 USB Data Transmission
In order to use ATMEGA8 to simulate "soft USB", the file configuration folder USBDRV of AVRUSB should be placed in the source file directory in WINAVR. In this directory, the files required for programming are listed in Table 1.
This design can reduce the amount of code by only configuring the content of usbconfig.h. Its main parameter configuration is listed in Table 2.
Generally, each USB device has a PID and VID. VID is the manufacturer's code, and PID is the product's code. Each code is a two-byte integer. PID and VID cannot be set arbitrarily. They are assigned by the USB Standard Association (0x16C0, 0x05DC), and in the user configuration file, the principle of low bits first and high bits last must be followed.
2.3 USB data transmission interface function
(1) Initialization function
Before using AVRUSB, you usually need to initialize the USB port by calling the initialization function usbInit(). Before calling this function, considering that the program may jump from other program segments, you usually need to reset the USB port before initialization. Resetting only requires lowering the level of the D- and D+ ports. The following statement can be used to achieve the above function:
DDRD=0x14;PORTD=0x00;
Usually, the function usbInit() can be called after the other parts of the program are initialized, and finally the sei() function is called to enable interrupts. The implementation code in the main.c file is as follows: [page]
(2) Event processing function
The main loop of the user program needs to call the USB event processing function usbPoll() regularly. The USB event processing function usbPoll() will return directly when there is no USB event to be processed, otherwise it will call the internal function to perform the corresponding event processing, and finally pass the data to the user interface function. The usual method is:
The timeout period of a USB communication is 50ms. Therefore, when programming, you should pay attention to other events not taking up too long to prevent the usbPoll() function from being executed in time.
(3) Event interface function
In the user program, you need to write the USB user event interface function to complete USB communication. AVRUSB simplifies the user interface into the following three functions:
The above three functions require user programming and they will complete the data processing of USB communication.
In order to optimize the code, this system only uses the usbFunctionSetup function and shields the usbFunctionWrite and usbFnctionRead functions by setting the corresponding bits in usbconfig.h to 0 or 1 (see Table 2).
The function usbFunctionSetup is responsible for passing USB requests. Its parameters are stored in an 8-byte array (uchar data [8]). The meanings are:
Since the system function only needs to transmit the measured temperature value to the PC, the function only needs to transmit data without identifying different instructions. The following code can be used to implement the above function:
2.4 Configuration of compiled files
In the WINAVR software environment, you also need to configure MakeFile to compile programming files:
After the configuration and programming of the above process, data sampling of the lower computer and data transmission of the USB port can be realized.
3 Conclusion
The temperature measurement and display system based on AVRUSB technology uses integrated soft USB technology, and its hardware circuit design is more compact and stable. It has been proved by use and testing that the system has the characteristics of stable performance and low price. More importantly, this system provides a low-cost USB design solution, and at the same time provides a detailed design process and part of the code of the lower computer, thus providing a better idea for the development of medium- and low-cost USB devices.
Previous article:Design of coal mine sensor based on AVR microcontroller
Next article:Multi-channel hydrological parameter acquisition and wireless transmission system based on ATmega16
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- [Digi-Key Innovation Design Competition] Material Unboxing + ESP32-S3-BOX
- EEWORLD University - Code reuse demonstration using TI SimpleLink? MCU platform
- STM32 RTC registers
- Inductance calculation formula
- [NXP Rapid IoT Review] + Recommendation for NXP Rapid IoT to connect the differential ADC input port of KW64 to the expansion port
- [Allwinner V853 heterogeneous AI intelligent vision development board review] "mkimage command not found" error solution...
- Live broadcast with prizes | Sign up for TI's June Embedded Processing Live Theme Week to get a glimpse of new products in industrial scenarios!
- [MSP430] Practical ADC use, internal temperature measurement use
- STM32F401CC spi flash recognition problem
- [Raspberry Pi 4B Review] + Building an OPENCV Environment