Professional Documents
Culture Documents
workshop
craft
home
food
play
outside
costumes
Table of Contents
Practical Guide to LEDs 4 - Matrix & Multiplexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 6: Charlieplexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
http://www.instructables.com/id/Practical-Guide-to-LEDs-4-Matrix-Multiplexing/
Sorry for being a day late, all the schematics and code were a lot more effort than estimated and took longer than any of the previous chapters. I hope the outcome is
worth it.
New instructables are scheduled every 2nd and 4th sunday each month. The new Semester is ahead and I won't have the time for more frequent uploads. I may take a
break for a study-headstart, but I'll surely be back on Oct 23rd with the last chapter!
The goal:
A display with 4x8 segments would require 32 I/Os to be driven traditionally. An ATMEGA328P has only 23 I/Os, and you'll need a few for other hardware as well. The
proposed solution will only require 12 I/Os, less than 40% of the original idea.
http://www.instructables.com/id/Practical-Guide-to-LEDs-4-Matrix-Multiplexing/
The idea:
Only one digit will be powered at a time, which is selected by one of 4 I/Os. This allows to share the same 8 I/Os to control the segments of all digits. The microcontroller
needs to cycle trough all digits fast enough that no flickering will be visible.
The circuit:
For this chapter we'll use common anode 7-segment displays as they are more commonly used. A common anode has certain benefits for advanced circuits, but
requires an odd circuit design. If you only have common cathode parts that's ok, too. You need to make a few minor changes to the circuit and code and it'll work as well.
In the last chapter the I/Os sourced current to the anode of the LEDs; the current was flowing out from the pin. A logic "1" corresponded with a lit LED. In this circuit the
cathode of the LED is driven, so the pin must sink the current to turn the LED on. This is done by setting the output to 0V - a logic "0". This is not a huge deal, but the
code needs to account for this.
To provide power to the LEDs P-Channel Mosfets are used. They behave similar to the N-Channel Mosfets used in previous chapter, except they require a negative
Gate-Source voltage VGS to turn on. The two possible cases are:
V = 0V Mosfetoff
1. logic "1" = 5V GS
V = -5V Mosfeton
2. logic "0" = 0V GS
Note that this only works when the signal voltage matches the supply voltage. If the supply voltage for the Mosfets is larger they will not switch off:
V = -7V Mosfeton
1. logic "1" = 5V GS
V = -12V Mosfeton
2. logic "0" = 0V GS
File Downloads
On run of the plan above will be called "phase". After 4 phases each digit has been active once, which equals a complete "cycle". The term "duty cycle" refers to the
relative on-time of a LED; with 4 phases the duty cycle is 1/4.
Activating the outputs:
http://www.instructables.com/id/Practical-Guide-to-LEDs-4-Matrix-Multiplexing/
Before we can run the plan above we need to enable the outputs, which can be easily forgotten. It is important to set the outputs to high beforeenabling them. If the
commands would be executed in reverse order, all segments would be on for a short time.In this case it wouldn't damage the chip, but should be avoided as a good
practice.
PORTB
PORTD
DDRB
DDRD
|=
=
|=
=
0x0F;
0xFF;
0x0F;
0xFF;
//
//
//
//
prepare off-state
prepare off-state
Enable the output
Enable the output
for
for
for
for
Mosfets
segments
the IO PB0 thru PB3
the the whole port
= 0x00;
= (1<<CS01)|(1<<CS00);
= (1<<TOIE0);
The plan laid out above is not suitable for an ISR, it needs to be re-ordered that the wait time is at the beginning or end:
1.
2.
3.
4.
5.
= ~(digit[current_phase]);
& ~(1<<current_phase)|0xF0;
// Update segments
// Turn Mosfet for current digit on
As it does not matter in which order the digits are active, the phase number is counted down to benefit from the faster execution of if(current_phase == 0) instead
of if(current_phase == 3).
The ~ of PORTD = ~(digit[current_phase]); inverts all bits, a "1" in memory matches a active segment. ~(1<<current_phase) results in 7 "1"s and a "0" at
the position of current_phase. The operator &= copies only all "0"s and thus turns only one digit on.
Defining individual symbols:
http://www.instructables.com/id/Practical-Guide-to-LEDs-4-Matrix-Multiplexing/
Somehow we need to tell the program which segments should be on for any desired character. Typing it in manually is not an option for me, so I wrote a include file
(dispCodec.h) to do all the decoding for me. You only need to type in which I/O are tied to which segments and you can use any of the symbols defined below.
The example on the cover image is generated by the code:
digit[3]
digit[2]
digit[1]
digit[0]
=
=
=
=
DP_L;
DP_E;
DP_d;
DP_4;
In this example the display is static, but you can change all characters at any time in the main loop or in additional ISRs. Have fun experimenting!
Image Notes
1. Drive signal of mosfet nr. 1. It is on during the low period and of during the highs.
2. One complete cycle: No two digits are on at the same time. The total cycle duration is about 8ms
3. The width of one of the squares in the background is equal to 2ms
4. Display refresh rate (= FPS)
File Downloads
http://www.instructables.com/id/Practical-Guide-to-LEDs-4-Matrix-Multiplexing/
The ATEMEGA328P on the other hand can only handle 20mA (40mA if you push it) so additional transistors for the segments are required. The updated schematic is
shown above. Resister values were calculated with the formula from last chapter:
R = (5V - 0.7V) * 100 / 0.064A = 6.72k 5.6k
File Downloads
= (1<<TOIE0)|(1<<OCIE0B);
All left to do is to move the "Turn all mosfets off" line into the new ISR:
ISR(TIMER0_COMPB_vect)
{
PORTB
|= 0x0F;
}
http://www.instructables.com/id/Practical-Guide-to-LEDs-4-Matrix-Multiplexing/
Image Notes
1. Compared to Step#2 the on time is much shorter - the brightness of the digit is reduced
2. The display update frequency stays the same as in Step#2
File Downloads
http://www.instructables.com/id/Practical-Guide-to-LEDs-4-Matrix-Multiplexing/
The main.c code is very similar to the previous example. The code to define the outputs as been updated to:
PORTD
DDRD
|= 0x0F;
|= 0x0F;
The driver for the shift registers is enabled by HC595_initialize();, it sets up the SPI module and enables the outputs as defined in the Config folder.
The overflow ISR is change to:
ISR(TIMER0_OVF_vect)
{
// begin next phase
if(current_phase == 0)
current_phase = 3;
else
current_phase--;
HC595_loadData(1, &igit[current_phase]);
HC595_updateDriver();
PORTD
& ~(1<<current_phase);
HC595_loadData(1, &igit[current_phase]); starts the process of sending 1 byte of data. The & is required as the command requires a position of where to find
the data within the memory instead of the actual data itself. Finally HC595_updateDriver(); toggles the STCP pin to output the data after the shifting has been
completed.
Adding more shift registers:
For large dot-matrix displays you can use nearly infinite 74HC595 chained together. However the shifting process takes some time. I don't remember the exact speed of
the code, but it was about 1us at a 16MHz clock and as fast as possible. The code supports sending out multiple bytes at once to improve speed even further. To send
out an array called data with 4 elements you'd type:
data [4] = {0, 1, 2, 3};
HC595_loadData(4, data);
HC595_updateDriver();
Image Notes
1. Connect the DS input of the next 74HC595 here
File Downloads
Step 6: Charlieplexing
No guide about multiplexing is complete without at least mentioning Charlieplexing. Charlieplexing takes advantage of the three states a I/O can have: High (logic "1"),
Low (logic "0") and Hi-Z (input). Compared to normal multiplexing this method can decrease the pin count, but at the same time code and layout complexity increases
significantly. Therefore I do not recommend this for most applications.
Still, there are a few project which are not possible without, such as the miniature LED cube (by HariFun).
So, how does it work exactly?
With Charlieplexing only one LED is driven at a time. To do so one of all I/Os is set to High, one to Low and the remaining to Hi-Z. In the schematic above LED D1 is
driven when PD0 is High, PD1 Low and PD2 Hi-Z. The code must cycle through all I/O combinations for lit LEDs. Obviously each LED will be only on for a short time,
resulting in dim LEDs, especially if many LEDs are driven.
There are no simple solutions to increase the current while maintaining 3-state capabilities. However you can safely use the maximum current per pin of 40mA as each
pin is only on for a short time.
Resistors are calculated as usual, except the the value is split in half. In any configuration two resistors will be in series, so their value adds up to the total, calculated
http://www.instructables.com/id/Practical-Guide-to-LEDs-4-Matrix-Multiplexing/
value. Often the required resistor values will be very small. In such a case it is possible to rely on the internal resistance of the outputs. An ATMEGA328P has a typical
output resistance of 25@5V or 35@3V. The total resistance in series to any LED is therfore 50@5V or 70@3V. The remaining calculations are identical to general
multiplexing.
For now I have to leave you without any example code on this. If things go according to plan (which they usually don't), I have completed a project requiring charlieplexing
by the beginning of 2017 and will share the code along with it.
Related Instructables
Practical Guide
to LEDs 1 - Pick
your LED! by
nqtronix
Practical Guide
to LEDs 2 Essential
Circuits by
nqtronix
Practical Guide
to LEDs 3 Switching &
Dimming by
nqtronix
Complete guide
to LEDs by
Satyam Shree
Mahanta
Solder Fume
Extractor With
Activated
Carbon Filter by
nqtronix
100W LED
flashlight by
vvrtlar
Advertisements
Comments
1 comments
Add Comment
St Stephen says:
Thank you for sharing, thanks for taking time to post, great info!
http://www.instructables.com/id/Practical-Guide-to-LEDs-4-Matrix-Multiplexing/