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 microcontroller is limited. Taking the PIC16F877 microcontroller 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, and the amount of data that the microcontroller 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 microcontroller. 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.
2Programs 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:Introduction to the functional modules of PIC microcontroller
Next article:Discussion on low power consumption of PIC microcontroller
- 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
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- [ESK32-360 Review] + DHT22 and single bus detection of temperature and humidity
- [TI recommended course] #Zero-based entry: Hand-in-hand teaching you how to quickly develop MSP430? Project#
- [Free Trial] Submit your application here and have a chance to win an Infineon drone~~
- About TL431: Questions about TL431 and TL432
- Texas Instruments uses 20 million hours to make the case for using gallium nitride (GaN)
- Who knows this digital tube?
- Comparison of UWB and its related technologies
- Using FPGA to scan LED large screen.pdf
- Leading with technology, sensing a new future. Download ams Semiconductor's "High-precision Short-distance Measurement Solution" and answer the questions to win prizes!
- Problems with thermistor constant temperature circuit built with op amp