IO definitions PIC

Status
Not open for further replies.

jnnewton

Member
I am using the C30 compiler for microchip dsPIC30F4013 and have a header file with the following:

Code:
#define LED     LATBbits.LATB0

I want to now base that definition off of a dip switch, something like (in C):

Code:
TRISBbits.TRISB1 = 1
if (PORTBbits.RB1)
{
     TRISBbits.TRISB0 = 0
     #define LED     LATBbits.LATB0;
}
else
{
     TRISBbits.TRISB0 = 0
     #define SWITCH     PORTBbits.RB0
}

The problem is , i cant get the definitions to work globally if i do it in C and i can't use #if(PORTBbits.RB1 == 1) in preprocessor because, well it is preprocessor and therefore has no idea of the port state yet, and C30 compiler won't let you put a "." in an #if statement. Any Ideas?
 
I want to now base that definition off of a dip switch, something like (in C):
You can not read a switch at run time then go back in time to recompile the code to use that definition.

You either need two code branches or
you can use a single function with calls to functions that check the switch (or a var set from it) and
act accordingly.

Code:
int sVal;

main()
{
    sVal = PORTBbits.RB1;
    ... lots of code...
    doThing1()
   ...more code...
}


// do the conditional part here
void doThing1()
{
     if (sVal)
        ....
     else
        .... ;
}
 
OK, i have this so far (which works). Now i want to be able to use the same definitions in my other .c files without having to copy and paste this code into each of them. Any ideas?
Code:
	TRISA = 0b0000000000000000;
	TRISB = 0b0000000100111100;
	TRISC = 0b0010000000000000;
	TRISD = 0b0000000000000000;
	TRISF = 0b0000000000000000;	
	//Node Definitions
	#define ON 1
	#define LCD 0
	#define NODE PORTBbits.RB3
	if(NODE == LCD)
	{
		#define LED1 LATDbits.LATD2
	}
	else
	{
		#define LED1 LATDbits.LATD8	
	}
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…