The data after A/D conversion of PIC microcontroller usually needs to occupy two 8-bit wide RAM units, but the storage units of PIC microcontroller are limited, so the storage units of the microcontroller cannot be effectively utilized. At the same time, it takes a lot of time to transmit data to the host computer through the serial port.
Therefore, a data packaging method is proposed, which makes the same amount of data occupy less storage space and improves the transmission efficiency.
introduction
In the detection system composed of single-chip microcomputer and PC, usually multiple single-chip microcomputers collect data and send the data after A/D conversion to the host PC through the serial port for data processing. The results of PIC single-chip microcomputer A/D conversion are mostly 8-bit or 10-bit, and 8-bit A/D conversion does not involve this problem. Taking 10-bit data as an example, it takes 2 8-bit bytes to store in the single-chip microcomputer. The lower 8 bits and the upper 2 bits are stored in 2 bytes respectively, but only 2 bits of the 8-bit wide RAM unit used to store high-bit data are valid data.
The storage space of the single-chip microcomputer is limited. Taking the single-chip microcomputer as an example, the RAM data memory is divided into two parts according to function: general registers and special function registers. Excluding the special function registers, there are only 368 8-bit wide RAM units. The amount of data that the single-chip microcomputer can store is very limited. Assuming that the data of each A/D conversion is directly stored, each data will occupy 2 bytes, and only 184 data can be stored in an ideal state; assuming that 100 A/D conversion data need to be stored, it will take up 200 bytes of units. This storage method does not make full use of the storage space of the single-chip microcomputer. At the same time, if the A/D converted data is directly uploaded through the serial port, the serial port will transmit 200 frames of data upward, and the effective transmission rate is relatively low, and the data bits in the data frame are not fully utilized. For this reason, this paper proposes the following method to process the A/D converted data before storing or uploading it.
1 Programming ideas
Taking 10-bit A/D conversion as an example, the result of A/D conversion can be temporarily stored in a[size], and then the data in array a can be taken out one by one, and the lower 8 bits and upper 8 bits can be separated and stored separately, the lower 8 bits can be stored in array b, and the upper 8 bits can be stored in array c. Since only 2 bits of the upper 8 bits are valid data, we can take out the valid data and combine them into a new data storage. After uploading the data, we can decode it in the opposite way to restore the data to its original state. In this way, 3/8 of the space can be saved for 10-bit A/D conversion, and the data that needs to be transmitted to the host computer will be relatively small, and the data transmission time will be only 5/8 of the original. [page]
2 Programs written in C language
This article uses PIC16F877 microcontroller for testing. i, h, j, n, m are defined as int8, a[size] is an array of int16, used to temporarily store the results of A/D conversion; b[number], c[number], d[number] are arrays of int8. The original program is as follows:
voidconvert()
{
h= j;
for ( i = 0 ; i < size ; i + + )
{
b[h+i]= a[i];
c[i] = (a[i]>> 8)&0x03;
j++ ;
}
m=n;
for ( i= 0 ; i< size/4 ; i+ + )
{
d[m+i]= (c[4*i]<< 2) |c[4*i+ 1 ];
d[m+i]= (d[m+i]<< 4) |(c[4*i+ 2 ]<< 2) ;
d[m+i]= d[m+i]|c[4*i+3];
n++ ;
}
}
The program first uses a for loop to split the data after A/D conversion, storing the lower 8 bits in array b and temporarily storing the upper 2 bits in array c. Since the 8-bit binary data in array c are only valid data in the lower 2 bits, the second for loop takes out the 2 bits of valid data from c[i], c[i+1], c[i+2], and c[i+3], and reassembles them into an 8-bit binary number in order from low to high, and puts them into array d to form a new array. In this way, the A/D conversion result is changed from being represented by array a to being represented by array b for the lower 8 bits and array d for the upper 2 bits.
The h, j, n, and m in the program are used to record the location of the last converted data stored in the array. During the next conversion, the data can be connected to the previous data. n and j are first assigned to 0 before convert () is called in the main program.
3 Conclusion
According to the above program, we can combine the 12-bit A/D conversion results in a similar way. Split the 12-bit A/D conversion results into low 8 bits and high 4 bits, and then recombine the 2 high 4 bits into an 8-bit binary number for storage. This can save 1/4 of the storage space and shorten the transmission time for the 12-bit A/D conversion results by 1/4.
The execution time of the program is only 990us, which is very short compared to the data transmission time. In the case of small MCU space, it is worthwhile to exchange the program execution time for the storage space of the MCU. At the same time, it can effectively shorten the data upload time and increase the transmission rate of effective data. However, when the amount of data is small, such as only one A/D conversion data, doing so will waste time and reduce efficiency.
Previous article:How to control a digital potentiometer using an external analog voltage
Next article:Relative Humidity Meter Based on Sensor and PIC Microcontroller
- Popular Resources
- Popular amplifiers
- CVPR 2023 Paper Summary: Efficient and Scalable Vision
- Chip Manufacturing: A Practical Tutorial on Semiconductor Process Technology (Sixth Edition)
- singlechip-implementation-of-levelcrossing-adc-for-ecg-sampling-2332-0796-1000219
- Impact of Power Density Maximization on Efficiency of DC–DC Converter Systems
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
- Inquiry: How can I see the complete reminder?
- Programmable signal generation chip
- [GD32L233C-START Review] IV. Timer PWM Application to Drive RGB Lights
- What should I pay attention to when making PCB samples?
- [Solved] GD32F307RCT6, soft reset cannot be completed during Ethernet initialization (SWR bit is always 1)
- TI - Standard Amplifier Functions in HEV/EV Battery Management Systems
- Follow the beautiful anchor into the Tektronix Laboratory live broadcast room to draw lots and win luxury gifts!
- EEWORLD University Hall----Intelligent Control System
- [RVB2601 creative application development] @fxyc87 RVB2601 unboxing + hello_world
- Body fat scale is broken, pry it open and see