Page 336 - Programming Microcontrollers in C
P. 336

Table Look-Up    321

                              Each table entry contains four bytes. The first byte is the inde­
                          pendent variable value, often designated as x. The second byte
                          contains the dependent variable value, usually called y, and the final
                          two bytes contain a floating point format of the slope. The slope
                          must be floating point. The third byte contains the integer position of
                          the slope, and the fourth byte contains the fractional value of the
                          slope. With the binary point placed between the third and fourth bytes
                          of the table entry, we can accomplish some interesting multiply op­
                          erations. Prior to looking at the coding of this problem, let us complete
                          the table and convert the slopes into hexadecimal format.


                                   5
                                  20        5      0x00     0x80
                                  40       15      0x00     0xe0
                                  80       50      0x01     0xc1
                                 120      120      0x02     0x00
                                 180      240

                              Table 6-5: Look-Up Table with Hexadecimal Slopes

                              How does one build a table of this nature in C? Of course, it is an
                          array of structures, or it could be a structure that contains an array of
                          structures. The latter approach would be

                   struct entry
                   {
                   char x;
                   char y;
                   int slope;
                   };


                   struct lut
                   {
                   char entries;
                   struct entry data[5];
                   } _lut = {
                   5,
                   20, 5, 0x0080,
                   40, 15, 0x00e0,
   331   332   333   334   335   336   337   338   339   340   341