Skip to main content

When to Use a Lookup Table in a Microcontroller

 lookup table MCU

Embedded processing requires performing many different coded functions in an application. To get around all of the compute cycles needed in a modern embedded system, we often try to find ways to optimize algorithms to reduce computational burden. But what if we could eliminate complex calculations and just focus on capturing and comparing measurements? There is a simple way to do this with a small microcontroller and it applies in a variety of situations.

Doing this involves the use of a lookup table in a microcontroller. Look up tables are essentially tables of values against which an input measurement or data point is compared. Through a simple comparison, the system can then supply an output, similar to what you would do with a basic logical comparison. To see how this works, let's take a look at when to use lookup tables and how to create them in an embedded application.

What Are Lookup Tables and How to Use Them

A lookup table is essentially a table of values, which might be hard-coded into an embedded application. Look up tables could also be built on-the-fly algorithmically and then used in other computations.

A simple graphical representation of a lookup table for one input and one output is shown below. When the processor receives an input, the resulting output is determined by matching the input value to a corresponding output value on the lookup table.

lookup table MCU

A simple lookup table is a numerical output on a 7-segment display. This example shows conversion between a binary input (with its hexadecimal conversion) and a numeric output with a lookup table.

This is much more convenient than taking an input, performing some complex math or processing on it, and then giving an output. When creating a lookup table, the developer has already performed all of that work and the application just needs to store the output in memory or display the output to a user. There are some very useful instances where this can be implemented:

  • Taking ADC inputs and providing an output

  • Taking in numerical data from a bit stream in providing an output

  • Taking a precision waveform measurement

  • Applying quantization to some types of input data

The example shown above is the simplest and most desirable type of lookup table to use. It is technically possible to create a lookup table that maps multiple inputs to more than one output using a matrix or tensor formulation. Graphically, these lookup tables are more difficult to construct and visualize, but they essentially function the exact same way as a one-to-one lookup table.

If you're familiar with neural networks, then you will know that a a one layer neural network is basically a multi-dimensional lookup table. If you like, when doing machine learning and training a neural network, the resulting weights in each layer of the network can be treated as values in a lookup table.

Example Lookup Table Usage

The lookup table you create can be hard-coded into your application, or it can be created on-the-fly using values stored in memory. A lookup table creation and comparison example in C is shown below.

This code does the following:

  1. Reads an analog value from analogPin

  2. Compares this value to a set of predefined values in lookupTable

  3. Finds the closest value from the table to the read value

  4. Outputs this value over the serial port

  5. Waits 1 second before repeating Steps 1-4

Depending on your specific requirements (like the resolution of your ADC, the range of input values, etc.), you might need to modify or expand this code. The example is for an Arduino, but your ADC could be external and might instead transfer data over a serial protocol (such as SPI). Finally, for on-the-fly usage, there will need to be a separate function that must create the various entries in the lookup table.

In the example, the lookup table is created with a one-to-one mapping. Because input data is an integer there needs to be some logical conditions on what happens when the input data does not exactly match a value in the input column. Based on the logical comparison result, the output is then returned from this function. It can then be used in some other part of the application.

It’s possible to simulate system performance at all levels with the best set of system analysis tools from Cadence. Only Cadence offers a comprehensive set of circuit, IC, and PCB design tools for any application and any level of complexity. Cadence PCB design products also integrate with a multiphysics field solver for thermal analysis, including verification of thermally sensitive chip and package designs.

Subscribe to our newsletter for the latest updates. If you’re looking to learn more about how Cadence has the solution for you, talk to our team of experts.

Untitled Document