EETimes

Embedded Systems October 2000 Vol13_11

Issue link: http://dc.ee.ubm-us.com/i/71842

Contents of this Issue

Navigation

Page 43 of 181

You should be free to use whatever units are necessary to get the job done, but also use the names to inform the reader. inventing your own units to avoid con- fusion. For instance, maybe you want to use a pixel as a unit of distance. In that case, don 't call it a millimelel~ call it a pixel. You can then define a well- named constant The ANSI C clockO function is one of many time related functions in the ANSI standard that leaves much to be desired in terms of a name. The timeO function is just as vague, but this at least has the advantage of a long legacy that you can at least assume that programmers will be familiar with it. The clockO function is supposed to tell you how much CPU time your program has consumed, but there are a few gotchas. For one thing the name tells us noth- ing about the units in the return value, and the return type "clock_tO. is equally uninformative. I have seen more than one manual that told me to divide the return by CLK_ TCK to get the time value in seconds, but this is wrong. Again, we have a macro with a bad name. The only thing CLK_ TCK has in common with clockO is that they use the same units (whatever those are). It seems that the unit of clock_t and the definition of CLK_ TCK are implementation defined. Unfortunately some early implementations of ANSI C left out a critical macro- the one which tells you the units, which is more clearly named CLOCKS_PER_SECOND. So what is CLK_ TCK then? CLK_ TCK is supposed to tell you how precise the timer used by clockO is. That is, if CLOCKS_PER_SECOND tells you that the units of clockO are in microseconds, then CLK3CK tells you how many microseconds there are between each clock tick. Obvious isn't it? This gets even more confusing when your implementation incorrectly defines CLK3CK and CLOCKS_PER_SECOND as the same value. This is true of the Cygnus distribution of GCC for Windows (also known as Cygwin B20). Faulty software that uses CLK_ TCK instead of CLOCKS_PER_SECOND will work until a compiler change makes these macros take on different values. Fortunately, hardly anyone uses this function anyway. I can't imagine why not. POSIX improved things with the clock_gettimeO function. This function takes as its parameters a clock ID and a "tv" structure containing two fields-tv_sec and tv nsee. Notice how the structure element names tell you that the time is in seconds + nanoseconds. The clock ID allows for implementation-defined clocks, but every implementation is required to provide a CLOCK_REALTIME clock ID. So this function can accommodate high precision clocks and is a bit more informative than the simpler ANSI functions. abbrevi ation fo r "meter" and "mill i." When in doubt, spell it out. This is especially true if you are going to use some of the less common abbrevi a- tions. For example: • 1/ 1000th of a volt = "mv" or "mV" are okay, "mVolts" or "milliVolts" are even better • 1/ 100th of a volt = "mvx10," "mVx10," "mVoltsx10," or "mill iVoltsx10" are okay but "centiVolts" is would be unusual and "cV" would be cryptic • 1/ 10th of a volt = "mVoltsx100" or "milliVoltsx100" but "deciVolts" would be unusual and "dV" would be cryptic you use These names start to get ugly when derived units th ey' re not li ke "velocity_meters_per_second," but at least Sometimes yo u shou ld conside r 42 OCTOBER 2000 Embedded Systems Programming am biguous. like PI XELS_ TO_ MILLIMETERS that will convert your pixels in to appropriate units. Ano ther example of when you might want to invent your own units is when you need to manipulate raw data from an ADC. Chances a re that the raw data is in arbitrary units , so no SI units apply unless you scale th e data first. In this case just call it somethin g like "adcCoun ts." Any programme r o r reviewer should understand that th ese are raw coun ts that need to be scaled before th ey can be used. Of course, assigning units to every scalar does not make sense. To para- phrase Freud, sometimes an i nt is just an i nt o Examples of th is would include loop counters and bit fl ags. Use good judgement and listen to code l-eview feedback. In the end you should have code th at is easier to understand and fewer bugs. esp John Fusco received his BSEE from Polytechnic University in Brooklyn NY. He has been designing and programming embedded systems f or oveT 10 years. Currently he is with GE Medical Systems where he designs image Teconstruction soft- ware fOT CT scanners. You can e-mail him at fuscoj@execpc.com. References 1. Eckel, Bruce. Thinking in C++, Vol. 1 (2nd Edition). Englewood Cliffs, NJ: Prentice-Hall, 2000. 2. For more information about standard units, consult the National Institute of Standards and Technology: physics.nist. gov/cuu/Units/index.html. Special Publication 330 discusses SI units in detail. 3. For more information about the details behind the loss of the Mars Climate Orbiter, see the official report available from JPL: ftp:!/ftp.hq.nasa.gov/pub/ pao/reports/1999/ Meo Jeport.pdf.

Articles in this issue

Archives of this issue

view archives of EETimes - Embedded Systems October 2000 Vol13_11