Reduce space and improve efficiency: PIC microcontroller A/D conversion data packaging

Publisher:RadiantRiverLatest update time:2014-03-19 Source: elecfans 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 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.

Reference address:Reduce space and improve efficiency: PIC microcontroller A/D conversion data packaging

Previous article:How to control a digital potentiometer using an external analog voltage
Next article:Relative Humidity Meter Based on Sensor 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号