element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Business of Engineering
  • Technologies
  • More
Business of Engineering
Forum If I Had a Hammer
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Business of Engineering to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 34 replies
  • Subscribers 244 subscribers
  • Views 4670 views
  • Users 0 members are here
Related

If I Had a Hammer

jack.chaney56
jack.chaney56 over 8 years ago

I used this as a clever draw to get more readers.  The actual intent is to start a discussion and get input from people for software.  Most of the discussions I have seen on the site are about inventing hardware solutions to popular problems.  I guess I am getting overloaded with how many ways there are to turn on an LED.  A significant amount of computer work is actually in the realm of information technology,  Too often what is lacking are skilled individuals that bring an array of tools to the task. The expression "If the only tool you have is a hammer, all your problems look like nails" is a problem I deal with in my professional life. I am constantly faced with vendors who provide the one pony circus (a million ways to make a state machine).

 

Embedded programming is a discipline that requires the developer to be expert at OS scheduling, database management, hardware interfacing, design architecture, and structured objects, as well as proper coding style, and best solution language. It is a constant process of sharpening the tool set to get the best solution. I have taken to putting some tools into retirement as well. Archiving example code and hint/help files is done periodically so I don't have to keep my COBOL skills current, but, by archiving properly, it is possible with a bit of effort, to blow the dust off a few brain cells, and get back in step with my Fortran lines starting in column 7. (...how old is this guy?)

 

Getting back to where I started with this, I wanted to put a call out to all you embedded PROGRAMMERS for tips and techniques and tricks learned that can become the foundation for tool boxes.

 

Tips like:

  • Thinking like a computer
  • Faster graphics and ray tracing
  • Integer only formulas
  • Weight loss (or wait loss) compression
  • T...I...M...I...N...G...

 

Again, some of these are done with hardware these days, but not all solutions have bottomless bank accounts. Sometimes a one chip 8-bit piece of hardware needs to run an 8 cylinder diesel fueling operation.

  • Sign in to reply
  • Cancel

Top Replies

  • jack.chaney56
    jack.chaney56 over 8 years ago in reply to ntewinkel +4
    ntewinkel , Precisely what I was referring to. Fixed point for calculations saves tons and tons of calculation overhead and saves boatloads of memory. Time for my first contribution to the tools. Fixed…
  • DAB
    DAB over 8 years ago +3
    Hi Jack, I can sympathize. Using high level programming languages hide a lot of detail you need to learn when going into embedded programming. Luckily I learned assembly language programming on an old…
  • rsc
    rsc over 8 years ago +3
    The most important tip I can think of is to document what you do, both hardware and software. When you get to the point you're writing code in several languages on several compilers for different projects…
  • jack.chaney56
    jack.chaney56 over 8 years ago in reply to jack.chaney56

    I did want to add a small note for the selection of the units for angle and ratio.

    Both of these are working on my conceptual approach of "thinking like a computer".

     

    People use degrees or radians for angular measurements, computers don't really know the difference, if the radian is conceptualized as pi * a value for units, then altering the number system slightly affords the ability to make 2*214 instead of 2*pi. Then because a 16 bit integer offers +/- for direction of rotation 215 is used to represent 2pi. The other advantage of this is independent of + or - of angles, location is determined by simply masking off the highest bit. Try it with a few operations. Another added benefit will be presented in a future installment.

     

    For ratio, this is simple because if you multiply by a ratio, you need to round and divide out the remainder in order to get back to a whole number portion. Using a power of 2 to represent 100% means, the divide instruction compiles to a right shift logical. In this case 10 bits. Also, rounding can be easily implemented by adding 512 (b0000000100000000) before the shift.

     

    Because pressure and temperature are on opposite sides of my favorite pV=nRT the fact that both are multiplied by 100 provides self canceling.

     

    Just thought I would explain,

    Jack

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • jack.chaney56
    jack.chaney56 over 8 years ago

    Needed to say something about tables versus formulas. For real time systems, tables always win.

    • Given a non-linear curve where Y=f(x) and x is a measured value, the value at any point can be calculated using the function f(x)
    • For simple linear equations accuracy is high and calculation can be performed simply with a single multiply and add
    • For complex equations, accuracy becomes an issue, and the calculation can become difficult and time consuming.

    image

    • For short ranges, a linear interpolation can be used for a close approximation, but this becomes highly inaccurate outside a specified range

    image

    • Using fundamental calculus, if the range of delta X is distributed evenly, the linear interpolation between any two points more closely approximates any curve
    • If greater accuracy is required the number of sections can be increased

      image

    • By producing a table of these values, the equation becomes simple for any value Xn where Xn is between Xa and Xb
    • The equation becomes:


       ƒ (X)=Ya+(((Yb−Ya)·(Xb−Xn))(Xb−Xa) )

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • jack.chaney56
    jack.chaney56 over 8 years ago in reply to jack.chaney56

    ...lost one of the diagrams

    image

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • DAB
    DAB over 8 years ago in reply to jack.chaney56

    Hi Jack,

     

    I completely agree about tables.

    They are very fast, good enough and you can do approximation between values if you need them

     

    Back when I was programming 8085 processors, I built a single table that enabled me to do sine and cosine very quickly from the same table.

    The time savings was immense, providing answers in a dozen microseconds verses using a math coprocessor that took milliseconds.

     

    When I build an integrated avionics simulator, I used tables for most functions and the run time result was very nice as opposed to trying to replicate all of the functions dynamically.

     

    Tables rule!

     

    If you look inside any real time game, you will find tables everywhere.

     

    DAB

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • Instructorman
    Instructorman over 8 years ago in reply to jack.chaney56

    I once had to calculate humidex in an 8-bit PIC microcontroller using the following formula - without using floating point:

    image

     

    Yup, that got turned into a look up table faster than you can say "Holy Excel spreadsheet, Batman!"

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • jack.chaney56
    jack.chaney56 over 8 years ago in reply to DAB

    DAB , I probably built the exact same table.  I was going to provide it as another installment for tips and tricks.

     

    If you build a 1/4 wave table using the binary radians that I described earlier, where 2pi = 215, bits 14 and 15 get used for determining the quadrant, and bits 0 through 13 get used for the table lookup.

    The other trick to use is cos(n) = sin(n+8192) so everything comes out of the same table lookup.

    if bit 15 == 1, return the negative value

    if bit 14 == 1, use 8192 - n so the table reads backward.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • koudelad
    koudelad over 8 years ago in reply to Instructorman

    One simple, but effective advice could be "learn to use spreadsheets and make graphs from them".

    I used Excel in practically all sensor applications, before implementing the formula to a microcontroller, just to check that the raw output values make sense to what is measured.

     

    • Recently I created a simple altimeter (hobbyist project) using an absolute pressure sensor. The barometric formula looks pretty hard to be calculated by a microcontroller:

    image

    or

    image

     

    However, using Excel, I found out that for a +/- 1000 m of altitude deviation is the pressure change ca. 11 Pa per 1 meter.

    One integer subtraction and an integer division is much simpler, even after considering the computation error.

     

    • Excel is also powerful to draw various graphs, calculate missing values and provide functions' approximations. This is especially useful when working with sensors that need to be calibrated (or were factory calibrated).

    The following graph shows an example of a function providing relative humidity value from  HTS221HTS221 sensor's raw output(using two calibration values It also shows that the sensors I have on my table are linear but the slope is a bit different And last but not least that the higher is the sensors output value the lower is the relative humidity(Please excuse the missing labels it was just a quick graph to demonstrate the point X axis shows relative humidity in rH Y axis shows an 16-bit signed output from the sensor

    image

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • kas.lewis
    kas.lewis over 8 years ago in reply to koudelad

    More information on how you did this would be great. Sounds like a greatly useful skill to have but not something a lot of us do have.

     

    Kas

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • jack.chaney56
    jack.chaney56 over 8 years ago in reply to kas.lewis

    Hi kas.lewis

    The process is part of a table lookup routine that I have in my toolbox. The process treats all tables as an NxM array, where N is the width and M is the height. The linkage to the data is done using a getter function so the data store can be abstracted.

    The function passes X as a parameter of f(x). Because N is the width of the table, there are N elements in the array of equal spacing the max value for X is defined during design. For most cases in my work, I use the ratio element (described earlier) 1024. The delta (spacing between elements) can now be determined as dX = 1024/N.

    The operation now needs two components, Xi which is the index into the table, and Xo witch is the offset from the index. The two values are obtained with the two operators, integer divide, and integer mod (remainder after division).

    Xi = x / dX;

    Xo = x % dX

     

    The getter function is called Z0 = getCal(table, x, y) using parameters of a table accessor, and the x and y indexes the function returns the content of the element (data type is determined by design).

    The operation requires the indexed value and the next value in sequence or x(n) and x(n+1). However, if the table is N units the indexes only go from 0 to N-1, but if the divide operation returns N-1, N, which is the next element, is beyond the end of the array. To account for this, a linear extension is performed:

    Z0 = getCal(table, Xi, y),

    Z1 = Z0 + Z0 - getCal(table, (Xi - 1), y)

    but for most cases it is simply Xi and (Xi + 1)

     

    Last, the interpolation is done as described before, this time with the defined variables  Z0+(((Z1−Z0)·Xo)dX )

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • jack.chaney56
    jack.chaney56 over 8 years ago in reply to Instructorman

    Hi Instructorman

    I like the discussion of the baro sensor being used for altitude. The equations are good, and show step one of the process of developing an application. My intent of this forum was to provide tools and techniques to share with others in problem solving. So if you could, because you mentioned running into some hitches while working with standard packages like Excel, it would be extremely helpful if you would discuss what you did to overcome the issue in developing the embedded solution.

    I can understand how a complex operation with  ex  and complex powers can become involved, especially with smaller processors that don't have floating point hardware. I have to perform pH conversion in many of the functions for our system, so I understand the headaches of dealing with log and anti-log operations.

    So if you would be so kind as to expound on the tricks that got you to a final solution, that would prove helpful to the group.

     

    Thank you,

    Jack

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
<>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube