Driving LEDs, the Matrix way

Status
Not open for further replies.

millwood

Advanced Member level 3
Joined
Jul 2, 2009
Messages
733
Helped
80
Reputation
164
Reaction score
35
Trophy points
1,308
Activity points
5,088
matrix leds

I thought about ways to drive the most number of LEDs with a given number of pins from a mcu (a PIC in this case).

you can drive two LEDs from one pin - but one of the LEDs has to be on at a given point.

you can drive two LEDs from two pins - you can put both of them off.

with three pins, you can connect two LEDs, reverse biased, between any two pins so you can drive a total of 6 LEDs. However, you have to turn any pin that you are not using into a high impedance mode (input).

so here is a little program that demonstrates that concept. it runs on a 12f675, using three pins to drive six LEDs.

Code:
#include <htc.h>

#define COMBO	6
#define LEDPort	GPIO
unsigned char LEDArray[COMBO][2] = {//first byte for trisio and 2nd byte for port
	{0b100, 0b001},					//pin0//1 output, pin0 high pin1 low
	{0b010, 0b001},					//pin0//2 output, pin0 high pin2 low
	{0b100, 0b010},					//pin0//1 output, pin0 low pin1 high
	{0b001, 0b010},
	{0b010, 0b100},
	{0b001, 0b100}
};

void delay(unsigned long int dly) {
	for (; dly>0; dly--)
	;
}

__CONFIG(MCLRDIS & WDTDIS);

void
main(void)
{
	unsigned char i;
	
	CMCON=0x07;
	ANSEL=0x00;
	
	while (1){
		//TODO Auto-generated main function
		for (i=0; i<COMBO; i++) {
			TRISIO=LEDArray[i][0];
			LEDPort=LEDArray[i][1];
			delay(20000);
		}
	}
}

you can easily expand the program so that it can drive n*(n-1) LEDs using n pins.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…