Path: EDN Asia >> Design Ideas >> IC/Board/Systems Design >> Tables simplify microcontroller programming
IC/Board/Systems Design Share print

Tables simplify microcontroller programming

21 Jul 2015  | Abel Raynus

Share this page with your friends

When developing microcontroller firmware, you often need to work with data arrays. Tables make easy work of data arrays, such as those for digital-code transformation, correction for sensor linearity, sophisticated calculations, and multiple output organisation. Table 1 shows how you can organise data in a table. Outputs A, B, and C have values based on the input value, V.


Table 1: Output values versus inpit values.


Table 2: Input and output values.


When using a lookup table, choose the proper microcontroller input and outputs. Assign values for input and outputs data in table 2. These data can consist of constants in binary, hexadecimal, or decimal format or names. For names, you should assign a constant value to each one. For example:


data1 equ $0a

data2 equ $0b

data3 equ $0c

data3 equ $0d


Next, put the data from table 2 in either the beginning or the end of ROM, which makes the data easy to find. For definition of 1B data storage, use pseudo operators FCB or DB. For storage of data comprising 2B, use FDB or DW, as in the following example:


ORG ROM

VxFCB 0T,2T,4T,6T

AxFCB data1,data2,data3,data4

BxFCB $aa,$bb,$cc,$dd

Cx FDB $1122,$3344,$5566,$7788


Note that commas separate the data. Don't place a comma after the last data, or it will be considered as $00.

When working with tables, you should always use indexed addressing mode. It provides access to data using variable addresses. Most microcontrollers have two index registers, X and H. Register X contains the low byte of the conditional address of the operand; H contains the high byte. The algorithm of working with tables is straightforward. After you detect the input value, you should then compare it with the table's input data. The X index determines this value, starting with X=0 and ending with X=N. In this example, N=4. When you find table data equal to the input value, you use the corresponding X as an index to load the output registers with their values. In the case of 2B numbers, you should load the output registers separately, first with a high byte and then with a low one. The figure illustrates this process.


Figure: You can use a look-up table in microcontroller code.


In the listing of assembler code, you can double-check the table content in memory at addresses $F800 through $F813. The listing uses Freescale assembler because most of the appropriate applications employ inexpensive, 8bit microcontrollers from Freescale's HC08 Nitron family. You can, however, use this approach with any type of microcontroller and assembly language.


About the author
Abel Raynus is with Armatron International.


This article is a Design Idea selected for re-publication by the editors. It was first published on April 22, 2010 in EDN.com.




Want to more of this to be delivered to you for FREE?

Subscribe to EDN Asia alerts and receive the latest design ideas and product news in your inbox.

Got to make sure you're not a robot. Please enter the code displayed on the right.

Time to activate your subscription - it's easy!

We have sent an activate request to your registerd e-email. Simply click on the link to activate your subscription.

We're doing this to protect your privacy and ensure you successfully receive your e-mail alerts.


Add New Comment
Visitor (To avoid code verification, simply login or register with us. It is fast and free!)
*Verify code:
Tech Impact

Regional Roundup
Control this smart glass with the blink of an eye
K-Glass 2 detects users' eye movements to point the cursor to recognise computer icons or objects in the Internet, and uses winks for commands. The researchers call this interface the "i-Mouse."

GlobalFoundries extends grants to Singapore students
ARM, Tencent Games team up to improve mobile gaming


News | Products | Design Features | Regional Roundup | Tech Impact