Continue to Site

# Run time pin configuration change

Status
Not open for further replies.

#### haxan

##### New Member
Hi, is it possible to change the pin settings in runtime. Meaning that i declare (define) pins when my program is running and then be able to change them according to init values?

#### MrNobody

##### New Member
I assume that you are referring to CONFIGURATION BITS as in POR, BOR, WDT etc.
No, you can accessed them only during programming and not at runtime.

You can look it up at the datasheet under "SPECIAL FEATURES OF THE CPU" > "Configuration Bits" section.

#### haxan

##### New Member
hmm..

I mean like we #define ports in our code to map them into the hardware. What if we want to map the ports during runtime so for example:

#define LED1 PORTBbits.RB0
#define LED2 PORTBbits.RB1

then somewhere in the code if i want to connect swap their ports, cant i do that.
I know its a stupid question and there are several ways around it but i have made a RS-485 communication library same for Master and Slave and i want to separate their pin connections to hardware while using the command library.

If i define the port in library then i have to use the same port for master and slave (which i do not want).

#### MrNobody

##### New Member
In short, no. #define or any code with # symbol are processed BEFORE it is compiled and converted into hex.

Lets say at the beginning of the code, you write
Code:
#define LED1 PORTBbits.RB0
and at the end of the code, you write
Code:
#define LED1 PORTBbits.RB1
if I am not mistaken, by the time you programed the PIC, LED1 will mean PORTBbits.RB1.

#### haxan

##### New Member
okay thanks. Yeah i know about directives.

#### 3v0

##### Coop Build Coordinator
Forum Supporter
The easy way (and what I sugget) is to put the #define in you main program and include the source code for the lib.

If you only have two sets of definitions, one for master and one for slave you could do the switch programmaticly. Use a static var in the lib to hold the chosen setting and set it using

Code:
#define MASTER 1
#define SLAVE     0
static unsigned char mode
void setMode(unsigned char _mode)
{
mode = _mode;
}
In you low level routines use functions (or macros) like the following to change the logic levels of the pins.

Code:
void setRx(void)
{
if (mode == MASTER)
...
else
...
}

#### Pommie

##### Well-Known Member
You can do it by writing the routine to set the port address. I wrote some One Wire routine where you could define the pin to use. The init was,
Code:
unsigned char *Port;
unsigned char *Tris;

void OwInit(unsigned char PortAdd,unsigned char Bit){
Tris=Port+0x80;
}

And then the port was accessed by,
Code:
unsigned char OwReset(){
bit temp;
delay_10us(75);
*Tris|=Mask;            //release line and make input
delay_us(68);
temp=((*Port&Mask)==0); //line pulled low if device present
delay_10us(48);
return temp;            //return 0 if present
}

Mike.

Status
Not open for further replies.

Replies
13
Views
3K
Replies
2
Views
11K
Replies
7
Views
4K
Replies
11
Views
1K
Replies
6
Views
7K