Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Run time pin configuration change

Status
Not open for further replies.

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.
 

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
Most Helpful 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;
unsigned char Mask;
 

void OwInit(unsigned char PortAdd,unsigned char Bit){
    Port=(unsigned char *)(int)PortAdd;
    Tris=Port+0x80;
    Mask=1<<Bit;
    *Port&=~Mask;
    *Tris|=Mask;
}

And then the port was accessed by,
Code:
unsigned char OwReset(){
bit temp;
    *Tris&=~Mask;           //pull line low
    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.

Latest threads

New Articles From Microcontroller Tips

Top