element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • 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
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs The cost of a float in an ISR
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: jpnbino
  • Date Created: 2 May 2020 7:26 PM Date Created
  • Views 1027 views
  • Likes 5 likes
  • Comments 5 comments
  • c coding
  • code
  • bug
Related
Recommended

The cost of a float in an ISR

jpnbino
jpnbino
2 May 2020

Goal

    Share a time problem when I uncarefully used an unintended float within my ISR.

 

Description

    Searching about debouncing switch routines, I came across this solution while revising a blog I already knew written by Jack Ganssle ( the article ). The technique for me seemed fine to me and then I applied it to a small project I'm doing. I tested the code logically, but not by execution time and that is what showed up later when I had a flickering led instead of a controlled brightness by software. ( note: due to aliasing effect it is not flickering as my eyes see image)

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

    It happened, that I came across this problem when I finished implementing a software PWM to control a LED. Additionally, the problem was not visible when the button was pressed (Low level, and you will know why). So, I had to investigate, and here is what I did (image - passed, image - not passed) :

 

  1. Isolated the PWM code; image
  2. Put all other components except my button routine. image
  3. Put the button module together. image
  4. Commented out ISR debounce code image

 

Bang! Found where! Taking a closer look I was curious about why this else if was jumping to function that converts an integer to float. Specifically, the problem was here:

 

    else if (integrator < MAXIMUM)
    {
        integrator++;
    }

 

 

    when I ran the debug, the code jumped into here:

    image

 

    Then, I realized the definition of the macro MAXIMUM was a float (due to #define DEBOUNCE_TIME be a float)  which led the integrator to be converted to a float in order to perform that else if() :

 

#define DEBOUNCE_TIME 0.3
#define SAMPLE_FREQUENCY 10
#define MAXIMUM (DEBOUNCE_TIME * SAMPLE_FREQUENCY)

 

 

    And that took me some time to realize it because in my head it was so simple, 10 times 0.3 is 3 (and easy integer ). Casting the MAXIMUM to uint8_t would solve the problem, but I wanted to know how much this float in the ISR was costing me and therefore I wanted to measure it.

 

The cost

    Running the simulator within MplabX and using the StopWatch feature to measure from the entring point until the exit point of the debounce ISR, I had:

 

For Button State(Input)Time (with float)Time (cast to uint8_t)
High1,78 ms47 µs
Low876 µs36 µs

 

 

The Setup

    And here is the setup I was using while coding:

 

    • Compiler xc8 v2.10 (FREE)
    • Optimization - 0 (None)
    • PIC12F1840 microcontroller

 

 

 

Be always back to the basics!

 

via GIPHY

  • Sign in to reply

Top Comments

  • shabaz
    shabaz over 5 years ago +2
    Hi jpnbino Nice reminder about keeping an eye on code execution time : ) There was a time the free PIC compiler was deliberately crippled, i.e. would not do certain optimizations, so maybe that made the…
  • genebren
    genebren over 5 years ago in reply to jpnbino +2
    There are a few simple things that you can do to monitor the amount of time each of the interrupt functions use and how frequently they are called. The first and simplest is to use a free GPIO pin and…
  • genebren
    genebren over 5 years ago +1
    Nice troubleshooting. I believe that I too borrowed ideas from this article (being a big fan of Jack Ganssel). Nice that you found your problem. ISR can be real tough to deal with when you are trying to…
Parents
  • genebren
    genebren over 5 years ago

    Nice troubleshooting. I believe that I too borrowed ideas from this article (being a big fan of Jack Ganssel).  Nice that you found your problem.

     

    ISR can be real tough to deal with when you are trying to minimize the amount of time spent in interrupt processing.  You really need to watch the code carefully, no function/subroutine calls, no excessive precision in math, no extra bells and whistles, just the minimum processing and go.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • jpnbino
    jpnbino over 5 years ago in reply to genebren

    Thank you Gene,

     

    the minimum processing and go.

    that is so true, the fundamental of ISR.

    Is there a technic that I can apply to a bigger system that allows me to keep track of them when they are all together? checking if one interrupt is starving?  If that is a paper of your trust or something, I would like to read.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • jpnbino
    jpnbino over 5 years ago in reply to genebren

    Thank you Gene,

     

    the minimum processing and go.

    that is so true, the fundamental of ISR.

    Is there a technic that I can apply to a bigger system that allows me to keep track of them when they are all together? checking if one interrupt is starving?  If that is a paper of your trust or something, I would like to read.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
  • genebren
    genebren over 5 years ago in reply to jpnbino

    There are a few simple things that you can do to monitor the amount of time each of the interrupt functions use and how frequently they are called.

     

    The first and simplest is to use a free GPIO pin and set it upon entry and clear it upon exit.  Then using an oscilloscope you can determine the amount of processing time that is being inside the interrupt.  This approach has revealed issues like you experienced, where a huge time (relative to your expectation) is being seen inside of an interrupt call.

     

    The other approach is to have a free time (free running) and capture the time entering and leaving a section of code.  This is very similar to how profiling software works.  With this approach you need some way of showing the results, like serial communications to expose the data.

     

    Best of luck!

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • 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