ArduinoLibs
|
Manage an array of LED's in a charlieplexed arrangement. More...
#include <Charlieplex.h>
Public Member Functions | |
Charlieplex (const uint8_t *pins, uint8_t numPins) | |
Constructs a new charliexplexing array where the output pins are specified by the numPins entries in pins. More... | |
~Charlieplex () | |
Destroys this charlieplexed array. | |
int | count () const |
Returns the number of LED's in this charlieplexed array based on the number of pins. More... | |
bool | led (int index) const |
Returns the value of the LED at index in the charplexed array; true if lit; false if not lit. More... | |
void | setLed (int index, bool value) |
Sets the value of the LED at index in the charliplexed array. More... | |
uint8_t | pwmLed (int index) const |
Returns the PWM value of the LED at index in the charplexed array; between 0 and 255. More... | |
void | setPwmLed (int index, uint8_t value) |
Sets the PWM value of the LED at index in the charliplexed array; between 0 and 255. More... | |
unsigned long | holdTime () const |
Returns the number of microseconds that each LED should be held on for before moving onto the next in loop(). More... | |
void | setHoldTime (unsigned long us) |
Sets the number of microseconds that each LED should be held on for before moving onto the next in loop() to us. More... | |
void | loop () |
Runs the multiplexing loop, to display the LED states on the charlieplexed array. More... | |
void | refresh () |
Refreshes the charlieplexed array by advancing to the next LED that needs to be lit. More... | |
Manage an array of LED's in a charlieplexed arrangement.
Charlieplexing is a technique for multiplexing large numbers of LED's on a small number of microcontroller output pins. LED's are arranged in complementary pairs; the simplest being for two output pins:
When Pin1 is 1 and Pin2 is 0, LED1 will be lit. When Pin1 is 0 and Pin2 is 1, then LED2 will be lit. The technique extends to 3 pins as follows:
In this case, LED5 is lit when Pin1 is 1, Pin3 is 0, and Pin2 is set to a high-impedance input to "disconnect" it.
Charlieplex presents a simple array of led() values that indicate whether each LED is on, off, or in an intermediate PWM state (if setPwmLed() is used). The application must call loop() or refresh() on a regular basis to ensure that the multiplexed display is kept up to date. The following example drives 6 LED's connected to the output pins D9, D10, and D11:
The following diagram extends the circuit for 4 output pins and 12 LED's:
The following diagram extends the circuit for 5 output pins and 20 LED's:
Circuits for higher numbers of LED's get increasingly complex. For those cases it can be easier to use traditional multiplexing matrix arrangements and shift registers. The DMD class does this for a specific kind of large dot matrix display. Otherwise, use the following pseudocode to determine how to connect the LED's for higher numbers of pins:
Note: while the above circuit diagrams and psuedocode use 1-based numbering for LED's, Charlieplex uses 0-based numbering in the led(), setLed(), pwmLed(), and setPwmLed() functions.
It isn't necessary to wire up all LED's. If you only need 10 LED's, then use the 4-output circuit and omit LED11 and LED12. Charlieplex only drives LED's that are lit; LED's that are unlit or unused will be skipped during the refresh scan. The maximum number of LED's that that can be driven by a specific number of pins is given by the following table:
Number of Pins | Number of LED's |
2 | 2 |
3 | 6 |
4 | 12 |
5 | 20 |
6 | 30 |
7 | 42 |
8 | 56 |
9 | 72 |
10 | 90 |
n | n * (n - 1) |
Definition at line 28 of file Charlieplex.h.
Charlieplex::Charlieplex | ( | const uint8_t * | pins, |
uint8_t | numPins | ||
) |
Constructs a new charliexplexing array where the output pins are specified by the numPins entries in pins.
Note: numPins must be 2 or greater for correct operation.
Definition at line 121 of file Charlieplex.cpp.
|
inline |
Returns the number of LED's in this charlieplexed array based on the number of pins.
Number of Pins | Number of LED's |
2 | 2 |
3 | 6 |
4 | 12 |
5 | 20 |
6 | 30 |
7 | 42 |
8 | 56 |
9 | 72 |
10 | 90 |
n | n * (n - 1) |
Definition at line 34 of file Charlieplex.h.
|
inline |
Returns the number of microseconds that each LED should be held on for before moving onto the next in loop().
The default value is calculated so that all LED's can be refreshed with a rate of at least 200 Hz, which is necessary for handling PWM output on multiple LED's. The less LED's that are lit at once, the faster the display will refresh.
Definition at line 42 of file Charlieplex.h.
|
inline |
Returns the value of the LED at index in the charplexed array; true if lit; false if not lit.
If the LED is displaying a PWM value, then this function will return true for any non-zero PWM value.
Definition at line 36 of file Charlieplex.h.
void Charlieplex::loop | ( | ) |
Runs the multiplexing loop, to display the LED states on the charlieplexed array.
If holdTime() microseconds have elapsed since the last call to loop(), then the current LED is turned off and the next LED that needs to be lit is turned on.
LED's that do not need to be lit are skipped. The total time for a single pass through all lit LED's may be very short if only a few LED's are lit at once. If all LED's are lit, then the total time for a single pass will be count() * holdTime() microseconds.
If the application is using timer interrupts to drive the multiplexing process, then use refresh() instead of loop().
Definition at line 277 of file Charlieplex.cpp.
|
inline |
Returns the PWM value of the LED at index in the charplexed array; between 0 and 255.
Definition at line 39 of file Charlieplex.h.
void Charlieplex::refresh | ( | ) |
Refreshes the charlieplexed array by advancing to the next LED that needs to be lit.
This function is intended to be called from a timer interrupt service routine to advance the multiplexing state without the main application having to explicitly call loop().
Definition at line 296 of file Charlieplex.cpp.
|
inline |
Sets the number of microseconds that each LED should be held on for before moving onto the next in loop() to us.
Definition at line 43 of file Charlieplex.h.
|
inline |
Sets the value of the LED at index in the charliplexed array.
The brightness of the LED will be proportional to the number of LED's that are currently lit, as the holdTime() refresh rate will cause the LED to appear to dim; the more LED's that are lit the less overall time each individual LED is held on. For best results, only a single LED should be lit at once or higher-brightness LED's should be used.
Definition at line 37 of file Charlieplex.h.
|
inline |
Sets the PWM value of the LED at index in the charliplexed array; between 0 and 255.
If this function is used, then it is assumed that the output pins are capable of PWM output.
The PWM-specified brightness of the LED will also be affected to the number of LED's that are currently lit, as the holdTime() refresh rate will cause the LED to appear to dim; the more LED's that are lit the less overall time each individual LED is held on. For best results, only a single LED should be lit at once or higher-brightness LED's should be used.
Definition at line 40 of file Charlieplex.h.