PIC microcontroller A/D conversion data storage and serial port efficiency

Publisher:sokakuLatest update time:2020-02-07 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Reference address:PIC microcontroller A/D conversion data storage and serial port efficiency

Previous article:Introduction to the functional modules of PIC microcontroller
Next article:Discussion on low power consumption of PIC microcontroller

Latest Microcontroller Articles
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号