Electronic – Can someone explain to me programming logic behind multiplexing a LED segment display

cgpiomicrocontroller

I am new to programming embedded systems, so any help is appreciated with my question. My task is to program (in C) a timer that counts up on a 7 segment common cathode display such that it will read for example: 000, 001, …, 010, 011, … 110, … 999

I am displaying all 3 digits at the same time via multiplexing with 3 N channel MOSFETs, by quickly switching on and off the gates of the MOSFETs (via GPIO).

What I have so far:

  • I have code that will make a timer for one digit; it displays and increments 0 to 9 but for 1 digit
  • I also have code that will switch on/off GPIO pins (including the MOSFET gates and each segment of the display

What I am missing is how I can put this all together so that will become what I want. The best I have gotten is that one digit will light and count 0 – 9, then it turns off. Then the next digit will light up and count 0 -9, then it turns off and etc.

Any tips? Not exactly looking for entire code write up, just maybe an outline of steps I can take.

Best Answer

The trick here is that you should not treat each digit as a separate entity.

Basically, multiplexing is a way of sharing a small number of IO pins to control a larger number of physical things, in this case LEDs. This is called time-division multiplexing. Multiplexing is just a way of driving N*M devices with N+M IO pins.

What you're missing here is that you don't count for each digit internally. Instead, you have a counter that maintains the overall counter value.

Say the internal counter has a value of 502. First, you illuminate the number 1 digit, with the value 5. Then, you illuminate the number 2 digit, with the value 0, and finally, the number 3 digit with the value 2. There is no incrementing or really any knowledge of what the value 502 even means.

If you want to increment the counter, you have to alter the internal counter number, not anything having to do with the multiplexing system.

For an even more basic implementation, separate the entire concept of numbers from the multiplexing. In this case, you have a array in memory that's just dictates which LEDs in the array are lit. Your multiplexing system just sits there and scans that array, lighting up each part in turn, and nothing else. The code that handles or is even aware that the values in the array represent numbers doesn't know the digits are multiplexed, or even exist. It just knows that this bit means that that LED should be illuminated as it lights up each display segment in turn.

The code that writes the values into the array is what handles determining what ordering of bits means what digits.

Critically, the counting and the multiplexing are separate processes. For multiplexing to be effective in a display system, the multiplexing has to run fast (100+ times a second). However, since the code that modifies the array is separate, it can run much more slowly (e.g. at a rate humans can track, etc...).

Related Topic