For this instructable I will be using the BS2e but any member of the BS2 family should work.
Remove these ads by
Signing UpStep 1Charlieplexing: What, Why, and How
Why use charlieplexing with a Basic Stamp 2?
---Proof of concept: Learn how charlieplexing works and learn something about the BS2. This may be useful to me later using faster 8-pin chips (only 5 of them will be i/o).
---Useful reason: Basically there is none. The BS2 is far too slow to display without noticeable flickering.
What is charlieplexing?
---Charlieplexing is a method of driving a large number of LEDs with a small number of microprocessor i/o pins. I learned about charlieplexing from www.instructables.com and you can too:
Charlieplexing LEDs- The theory
How to drive a lot of LEDs from a few microcontroller pins.
Also at wikipedia: Charlieplexing
How can I drive 20 leds with 5 i/o pins?
---Please read through the three links under "What is charlieplexing?". That explains it better than I ever could. Charlieplexing is different from traditional multiplexing which needs one i/o pin for each row and each column (that would be a total of 9 i/o pins for a 5/4 display).
| « Previous Step | Download PDFView All Steps | Next Step » |








































I used your project to further my understanding of Charlieplexing - thanks for writing it! That being said, I already had a breadboard built with a functioning 20 LED grid when I arrived at your project. I wanted to understand how to efficiently display information on the grid without doing it all manually by hand. I had a bit of trouble initially because your code did not match my hardware design - my LEDs were in a completely different order. I liked what I saw so I decided to work your code over a bit.
Changes:
[1] Added construction notes at the top of the file to aid in breadboard design (for new builders) or re-writing of the code should someone else show up (as I did) with a different layout.
[2] Rewrote the 'SELECT rowC' routine to accomidate the layout I already had working. It also matches the added comments I added to the top of the file.
[3] Added the rest of the English alphabet.
[4] Cleaned up whitespace/formatting a little bit.
Hope that someone finds this useful. Good luck!
-------------------------------------
' {$STAMP BS2}
' {$PBASIC 2.5}
' Circuit Construction Guide: This code assume that you are using a 4-wide x 5-tall
' grid of LEDs, configured as follows:
'
' (01) (02) (03) (04)
' (05) (06) (07) (08)
' (09) (10) (11) (12)
' (13) (14) (15) (16)
' (17) (18) (19) (20)
'
' Pins P0 though P4 are used. The +PIN -> LED -> -PIN assignment is applied in logical order:
'
' LED -> +PIN, -PIN
' 01 -> +P0 , -P1
' 02 -> +P1 , -P0
' 03 -> +P1 , -P2
' 04 -> +P2 , -P1
' 05 -> +P2 , -P3
' 06 -> +P3 , -P2
' 07 -> +P3 , -P4
' 08 -> +P4 , -P3
' 09 -> +P0 , -P2
' 10 -> +P2 , -P0
' 11 -> +P0 , -P3
' 12 -> +P3 , -P0
' 13 -> +P0 , -P4
' 14 -> +P4 , -P0
' 15 -> +P1 , -P3
' 16 -> +P3 , -P1
' 17 -> +P1 , -P4
' 18 -> +P4 , -P1
' 19 -> +P2 , -P4
' 20 -> +P4 , -P2
repC VAR Byte 'Counts character flashes (how long to display each character)
charC VAR Byte 'what character is currently being displayed
rowC VAR Byte 'Counter to track which row is being flashed
display VAR Byte 'Binary to display on the currently scanning row
maxC CON 10 'Maximum character flashes before moving to next character
charsInLib CON 36 'Total characters in the library
'Variables used for reading in each of a five row character
ROW1 VAR Byte
ROW2 VAR Byte
ROW3 VAR Byte
ROW4 VAR Byte
ROW5 VAR Byte
'Variables used to set pin directions
direc1 VAR Byte
direc2 VAR Byte
'Variables used to set pin outputs
outp1 VAR Byte
outp2 VAR Byte
outp3 VAR Byte
outp4 VAR Byte
charC = 0
'Main Program
DO
GOSUB ReadChar
GOSUB LightLED
DIRS = 0
charC = charC + 1 'increment up to the next character
IF charC > charsInLib - 1 THEN
charC = 0
ENDIF
LOOP
'Subroutine to read in character
ReadChar:
READ letters + (charC * 5), ROW1
READ letters + (charC * 5) + 1, ROW2
READ letters + (charC * 5) + 2, ROW3
READ letters + (charC * 5) + 3, ROW4
READ letters + (charC * 5) + 4, ROW5
RETURN
'Subroutine to display character
LightLED:
'In depth explanation:
'5x4 led matrix interpreter functions by row of 4 leds
'Each SELECT is a different column and each case (1-16) corresponds
'to one of the 16 states possible for the column of leds.
'
'Binary should then be read out of memory and handed straight to the
'interpreter for output display.
'
'case 0 %0000
'case 1 %0001
'case 2 %0010
'case 3 %0011
'case 4 %0100
'case 5 %0101
'case 6 %0110
'case 7 %0111
'case 8 %1000
'case 9 %1001
'case 10 %1010
'case 11 %1011
'case 12 %1100
'case 13 %1101
'case 14 %1110
'case 15 %1111
DO
rowC = 1 'Start with row 1
IF repC > maxC THEN 'How long to display the character before returning to main
repC = 0
RETURN
ENDIF
FOR rowC=1 TO 5 'Cycle through each of 5 rows
SELECT rowC
CASE 1
outp1 = %00001
outp2 = %00010
outp3 = %00010
outp4 = %00100
direc1 = %00011
direc2 = %00110
display = ROW1
CASE 2
outp1 = %00100
outp2 = %01000
outp3 = %01000
outp4 = %10000
direc1 = %01100
direc2 = %11000
display = ROW2
CASE 3
outp1 = %00001
outp2 = %00100
outp3 = %00001
outp4 = %01000
direc1 = %00101
direc2 = %01001
display = ROW3
CASE 4
outp1 = %00001
outp2 = %10000
outp3 = %00010
outp4 = %01000
direc1 = %10001
direc2 = %01010
display = ROW4
CASE 5
outp1 = %00010
outp2 = %10000
outp3 = %00100
outp4 = %10000
direc1 = %10010
direc2 = %10100
display = ROW5
ENDSELECT
DIRS = 0
SELECT display
CASE 0
PAUSE 0
CASE 1
OUTS = outp4
DIRS = direc2
PAUSE 1
CASE 2
OUTS = outp3
DIRS = direc2
PAUSE 1
CASE 3
OUTS = outp3
DIRS = direc2
PAUSE 1
OUTS = outp4
PAUSE 1
CASE 4
OUTS = outp2
DIRS = direc1
PAUSE 1
CASE 5
OUTS = outp2
DIRS = direc1
PAUSE 1
DIRS = 0
OUTS = outp4
DIRS = direc2
PAUSE 1
CASE 6
OUTS = outp2
DIRS = direc1
PAUSE 1
DIRS = 0
OUTS = outp3
DIRS = direc2
PAUSE 1
CASE 7
OUTS = outp2
DIRS = direc1
PAUSE 1
DIRS = 0
OUTS = outp3
DIRS = direc2
PAUSE 1
OUTS = outp4
PAUSE 1
CASE 8
OUTS = outp1
DIRS = direc1
PAUSE 1
CASE 9
OUTS = outp1
DIRS = direc1
PAUSE 1
DIRS = 0
OUTS = outp4
DIRS = direc2
PAUSE 1
CASE 10
OUTS = outp1
DIRS = direc1
PAUSE 1
DIRS = 0
OUTS = outp3
DIRS = direc2
PAUSE 1
CASE 11
OUTS = outp1
DIRS = direc1
PAUSE 1
DIRS = 0
OUTS = outp3
DIRS = direc2
PAUSE 1
OUTS = outp4
PAUSE 1
CASE 12
OUTS = outp1
DIRS = direc1
PAUSE 1
OUTS = outp2
PAUSE 1
CASE 13
OUTS = outp1
DIRS = direc1
PAUSE 1
OUTS = outp2
PAUSE 1
DIRS = 0
OUTS = outp4
DIRS = direc2
PAUSE 1
CASE 14
OUTS = outp1
DIRS = direc1
PAUSE 1
OUTS = outp2
PAUSE 1
DIRS = 0
OUTS = outp3
DIRS = direc2
PAUSE 1
CASE 15
OUTS = outp1
DIRS = direc1
PAUSE 1
OUTS = outp2
PAUSE 1
DIRS = 0
OUTS = outp3
DIRS = direc2
PAUSE 1
OUTS = outp4
ENDSELECT
NEXT
repC = repC + 1
LOOP
RETURN
'1
letters DATA %0110,
%0110,
%0110,
%0110,
%0110,
'2
%0110,
%1001,
%0010,
%0100,
%1111,
'3
%0111,
%0001,
%0011,
%0001,
%0111,
'4
%1010,
%1010,
%1111,
%0010,
%0010,
'5
%0111,
%0100,
%0111,
%0001,
%0111,
'6
%0111,
%0100,
%0111,
%0101,
%0111,
'7
%1111,
%1001,
%0010,
%0100,
%0100,
'8
%0110,
%1001,
%0110,
%1001,
%0110,
'9
%0111,
%0101,
%0111,
%0001,
%0111,
'0
%0110,
%1001,
%1001,
%1001,
%0110,
'A
%0110,
%1001,
%1001,
%1111,
%1001,
'B
%1110,
%1001,
%1110,
%1001,
%1110,
'C
%0110,
%1001,
%1000,
%1001,
%0110,
'D
%1110,
%1001,
%1001,
%1001,
%1110,
'E
%1110,
%1000,
%1100,
%1000,
%1110,
'F
%1110,
%1000,
%1100,
%1000,
%1000,
'G
%1111,
%1001,
%1000,
%1011,
%1111,
'H
%1001,
%1001,
%1111,
%1001,
%1001,
'I
%1111,
%0110,
%0110,
%0110,
%1111,
'J
%0001,
%0001,
%0001,
%1001,
%1111,
'K
%1001,
%1010,
%1100,
%1010,
%1001,
'L
%1000,
%1000,
%1000,
%1000,
%1111,
'M
%1001,
%1111,
%1001,
%1001,
%1001,
'N
%1001,
%1101,
%1011,
%1001,
%1001,
'O
%1111,
%1001,
%1001,
%1001,
%1111,
'P
%1111,
%1001,
%1111,
%1000,
%1000,
'Q
%1111,
%1001,
%1001,
%1011,
%1111,
'R
%1111,
%1001,
%1111,
%1010,
%1001,
'S
%1111,
%1000,
%1111,
%0001,
%1111,
'T
%1111,
%0110,
%0110,
%0110,
%0110,
'U
%1001,
%1001,
%1001,
%1001,
%1111,
'V
%1001,
%1001,
%1001,
%1001,
%0110,
'W
%1001,
%1001,
%1001,
%1111,
%0110,
'X
%1001,
%1001,
%0110,
%1001,
%1001,
'Y
%1001,
%1001,
%0110,
%0110,
%0110,
'Z
%1111,
%0010,
%0100,
%1000,
%1111
-------------------------------------
No you don't have to use a bread board its just there for convenience.