Keil Logo

C51: VERIFYING LOOK-UP TABLE VALUES


Information in this article applies to:

  • C51 All Versions

QUESTION

I have written the following example lookup table:

#define OSC 1333333

unsigned long testar [] = { OSC / 3, OSC / 4, OSC / 5, OSC / 6 };

and I wish to double-check that the look-up table values are the same as the values I have calculated manually. Is there an easy way to do this?

ANSWER

Yes. If you move the table declaration into a seperate source file then you can add some test code which, when built automatically, checks the values in the look-up table. This can be run in the debugger where it will display any values that are incorrect according to your manual calculations.

For large look-up tables this can easily be extended to use an array of manually calculated values.

You may #define a DEBUG macro that controls whether or not the test code is compiled. For example:

#define DEBUG 1  /*** Undef this for REAL code ***/

#define OSC 1333333

unsigned long testar [] = { OSC / 3, OSC / 4, OSC / 5, OSC / 6 };

#if DEBUG
#include <stdio.h>
#include <reg52.h>

#define assert(expr)   if (expr)                                                \ 
                         {                                                      \ 
                         ;                                                      \ 
                         }                                                      \ 
                       else                                                     \ 
                         {                                                      \ 
                         printf("Assert failed: " #expr " (file %s line %d)\n", \ 
                         __FILE__, (int) __LINE__ );                            \ 
                         }

void main (void)
{
  SCON  = 0x50;
  TMOD |= 0x20;
  TH1   = 221;
  TR1   = 1;
  TI    = 1;

  assert(testar[0] == 444444);
  assert(testar[1] == 335333);
  assert(testar[2] == 266666);
  assert(testar[3] == 222222);

  while (1);
}

#endif

MORE INFORMATION

  • Refer to DEFINE in the Cx51 User's Guide.

Last Reviewed: Tuesday, October 24, 2006


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.