Combine more than one pin to a variable in C18

Micro9900

New Member
Hey guys, I have a quick question. I have 3 pins going to an LED (I've successfully used PWM to fade in and out of green thanks to earlier help ^^ ). Now, I would like to change the color. I'm not going to impose random color shifts just yet, so I would like to have discrete color fade-ins (ex. fade in and out of green, then change color to blue and fade in and out, then red, light blue, etc.)

Note: Compiler is C18 and I am using a PIC18F4550

I understand that to define pin variables you use for example:

Code:
 #define LED1 LATAbits.LATA0 
#define LED2 LATAbits.LATA1 
#define LED3 LATAbits.LATA2


Or you could define a variable for the entire port, for example:

Code:
#define allOfPortAbits LATA

So, my question is how do I define three pins to a variable? So for example (psuedo code):

Code:
#define threePins LATAbits.LATA<0-2>

Would I need to create struct that holds 3 bits like so?

Code:
typedef struct{
	unsigned colorCode : 3;
}myLEDcolor;

myLEDcolor LEDcolor;

And then shift and mask each bit in "LEDcolor.colorCode" and then set it equal to LATA variables (LED1,2,3)? In a nut shell, is there an easy way to create a three bit variable for LAT pins so I can easily update those pins?
 
Last edited:
I don't see much use for a 3 bit variable here but a struct is the way to create one. A common way to set or clear bits within a register is to use a AND/OR mask.

This one sets the 4 bit channel number in the ADC ADCON0 register. The bits in channel are [0..3], the channel bits in ADCON0 are [2..5]
ADCON0 = ((channel << 2) & 0b00111100) | (ADCON0 & 0b11000011);

The masks don't have to be static data, they can be a in a array or a deref'd pointer to a series of different masks.
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…