Microcontroller Selects Minimum / Maximum Value

MC68HC705KJ1

Microcontroller-based systems for measurement, sensor-data processing, or control, sometimes require you to determine a maximum or minimum data value. For example, in an object-detection system, such as a radar or sonar system, the microcontroller receives echo signals from multiple targets and then must select the closest one; in other words, it must determine the minimum distance. Another example is an automatic-tuning system in which the microcontroller acquires data and must determine the maximum or minimum values of the data. A simple way to determine maximum and minimum value involves a microcontroller, IC1, that receives a set of data from N sources (Figure 1). The data should be in 8-bit format. To simplify the process, consider that the data are 1-byte-long integers. In other words, the data cover the range 0 to 255. In most cases, this range produces satisfactory results. The same limitation applies to the number of data sources. If you need more data precision or more data sources, then you can use two or more bytes at the expense of added program complication.

Microcontroller Selects Minimum / Maximum Value

Two approaches exist for the maximum/minimum values. In the first, the microcontroller memory collects the received data and processes the data to determine maximum or minimum values (Figure 1a). In the second approach, the microcontroller processes the data immediately after receiving it (Figure 1b). Listing 1 shows the program for the first approach. Assume that a data array with N=8 exists in the microcontroller's memory. For demonstration purposes, Listing 1 illustrates this part of the routine in lines six to eight and 17 to 21, in which the controller loads the data registers from a predetermined table. In a real situation, you would load the data registers from data sources before calling the program. The process of minimum-value detection is based on a method called "the bubble." The search algorithm starts testing the memory data array from its end. It clears the index register (X) after completion of the program. If you need to detect the maximum instead of the minimum value, you need change only one instruction in line 28.

In the second approach, the microcontroller waits for a data value entering the data register and, upon reception, starts processing the value(Listing 2). This approach needs no memory array. This variant of the program adds two features: First, it simultaneously selects minimum and maximum values of the incoming data and saves them in the DATAmin and DATAmax registers. Second, the routine considers a data value equal to 0 as no data. Thus, the data range is from 01h to ffh. For this project, you can use the inexpensive, one-time-programmable MC68HC705KJ1 from Motorola. Thus, the programs use Motorola's assembly language. However, the algorithms are so straightforward, they're amenable to any language and to practically any microcontroller.Click here to download a zip file containing both listings and other associated software files.

EDN

EMS supplier