Yes you can.
I would put the TRIS and GPIO values for each LED into arrays. To turn on LED3 you
Code:
TRISIO=trisioArray[3];
GPIO = gpioArray[3];
Better yet define it as a macro or a function. With a small pic I would use the macro
Code:
#define LED_ON(n) TRISIO=trisioArray[3];GPIO = gpioArray[3]
// to turn on LED2 use
LED_ON(2);
This is code written for the Junebug which has 3 switches and 6 charlieplexed LEDs. It may be of interest.
Code:
// ADVANCED COMPUTER SCIENCE 9/23/2008
// DEMO PROGRAM
//
// Name: read switches
// Purpose: demostrate how to read switches and the
// use of arrays to operate the LEDs
//
//
// settings for configuration memory
#pragma config OSC = INTIO2, WDT = OFF, LVP = OFF
// processor definitions
#include <p18f1320.h>
// tell the compiler these are unsigned 8 bit
// constants that can be stored in flash with
// the program code.
rom const unsigned char lataVal[] =
{0x01, 0x40, 0x40, 0x80, 0x80, 0x01};
rom const unsigned char trisaVal[] =
{~0x41,~0x41,~0xC0,~0xC0,~0x81,~0x81};
void main(void)
{
unsigned char sw;
unsigned char btn1;
unsigned char btn2;
unsigned char btn3;
// crank up the internal clock to 8MHz
OSCCONbits.IRCF0=1;
OSCCONbits.IRCF1=1;
OSCCONbits.IRCF2=1;
ADCON1=0x70; // all digital
TRISB=0x25; // Switches on RB0 RB2 RB5
// PORTB weak pullups for switches
INTCON2bits.RBPU=0;
while(1)
{
// Read from PORTB,
// what would happen if used
//'sw=PORTB&0x25' instead ?
// Use PORTB not LATB with weak pullups.
sw=PORTB;
// deterimine if any was pressed
btn1 = (!(sw & 0x01)); // RB0
btn2 = (!(sw & 0x04)); // RB2
btn3 = (!(sw & 0x20)); // RB5
// turn on correct LED if switch was pressed
if(btn1) // why do we not use "if(btn1==1)" ?
{
LATA=lataVal[0];
TRISA=trisaVal[0];
}
if(btn2)
{
LATA=lataVal[1];
TRISA=trisaVal[1];
}
if(btn3)
{
LATA=lataVal[2];
TRISA=trisaVal[2];
}
}
}