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 )
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 ( - passed, - not passed) :
- Isolated the PWM code;
- Put all other components except my button routine.
- Put the button module together.
- Commented out ISR debounce code
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:
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) |
---|---|---|
High | 1,78 ms | 47 µs |
Low | 876 µs | 36 µ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!
Top Comments