-3
  • Programming language: C
  • Platform: PIC Microcontroller 8-bit
  • Number of problems: 2

I'm using a 4-digit 7-segment display for showing numbers. I've a few functions that display some letter/digit on the 7-segment like:

zero() // displays 0 on the 7-segment.
one() // displays 1 on the 7-segment.
two() // displays 2 on the 7-segment.
...

Now I've a number (say 1435) to be shown on the 7-segment display. My current algorithm is as follow:

  1. Extract individual digits from the number 1435 (that's separate the digits as 1, 4, 3, 5). sds

    • 1 will be displayed to digit1 of 7-segment.
    • 4 will be displayed to digit2 of 7-segment.
    • 3 will be displayed to digit3 of 7-segment.
    • 5 will be displayed to digit4 of 7-segment.
  2. To display these individual digits, I'm using 'ten' if-else conditions as follow:

    • If the digit to be displayed == 0 -> run the function zero(); else
    • If the digit to be displayed == 1 -> run the function one(); else
    • If the digit to be displayed == 2 -> run the function two();
    • ...
    • ...
    • ...

So this implementation (for the number 1435) to be printed runs several if-else checks.

  • 2 checks for displaying the digit 1
  • 5 checks for displaying the digit 4
  • 4 checks for displaying the digit 3
  • 6 checks for displaying the digit 5
  • 17 total checks that run "periodically and unnecessarily" in the loop() function even if the number is not changed (This is problem number 1).

Problem number 2: This implementation is inefficient as when I need to increment/decrement that number (1435 to 1436 then to 1437 so on..), the number of if-else checks are also changed so the variation in numbers is not smooth. This means that as '0' is the first in if-else checks so it displays quickly. On the other hand '9' is the last in the if-else checks, so it has to undergo ten checks before it gets displayed. This makes implementation much slower as the digit to be displayed grows from 0 towards 9. How can Implement to solve the two problems?

Thanks in advance.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Faraz Ahmad
  • 515
  • 1
  • 6
  • 13

1 Answers1

3

You can use pointers to functions in a table :

typedef void (*func)(); // type for functions
func functions[] = { zero, one, two, three, ... }
functions[3](); // example, will call three()

You have to extract the digit you want and use it as an index in the table....

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • Yup it works, I was already doing this in my code. But the mistake I was making was calling the array as functions[3] instead of functions[3](). Thanks. Can you please confirm it will be efficient than the if-else and switch? Thanks again. – Faraz Ahmad Feb 08 '16 at 21:05
  • Seriously? You need to optimize for nanoseconds? – Jean-Baptiste Yunès Feb 08 '16 at 21:07
  • @FarazAhmad - everyone is saying this is the way to do it? What exactly do you want as confirmation? If you want confirmation then test it yourself. – Hogan Feb 08 '16 at 21:07
  • @Jean-BaptisteYunès: This is a PIC MCU which is not built for such code actually. – too honest for this site Feb 08 '16 at 21:09
  • @Jean-BaptisteYunès - it could matter, this is a micro-controller. If it is a real time system then every nanosecond counts. – Hogan Feb 08 '16 at 21:09
  • @Hogan: Likely every >100ns. The PIC normally runs at few MHz. – too honest for this site Feb 08 '16 at 21:16
  • Just tested the code with this solution (simulation on proteus software). Proteus says, CPU (i.e. microcontroller) is out of memory. When I was using if-else, it ran without this notice but with the problems stated in the question. – Faraz Ahmad Feb 08 '16 at 21:30
  • @Jean-BaptisteYunès, yes I'm implementing the push button increment/decrement so I need to get as fast as possible. – Faraz Ahmad Feb 08 '16 at 21:30