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.

passing a port around.

Status
Not open for further replies.

AtomSoft

Well-Known Member
Ok I want to create a function that takes a port as a argument. How would I do this? Is there a port type?

want to do this for pic18 and pic32 using xc32 and xc8
 
What do you mean by port? There are several regsters linked to the port - TRISx, PORTx, LATx, ODBx etc.

On some PICs they are located at equal offsets from base port address for all ports so you can define structures wth TRIS, PORT etc. members and locate the structures at the base addresses of the ports.

However, on some PICs offsets are different for different ports.
 
Ok to better explain here is an example...

Code:
void setPin (port myPort, int pin)
{
    myPort |= (1<<pin);
}

You can use a pointer, because you need to pass a reference to the sfr location. For example:

Code:
typedef char* port;
 
void setPin (port myPort, int pin)
{
    *myPort |= (1<<pin);
}
 
That would do it. Although, depending on how good the compiler is at optimising there are probably more efficient solutions if that is all you are doing.

Pointers would generally compile to code that uses indirect addressing (which is basically the PIC assembly level equivalent of pointers), but if you are just trying to give your ports friendly names then DEFINE'ing the name to PORTA is more likely to produce code that just writes to a fixed memory address. Pointers are only really necessary if you want to do pointer arithmetic (they also make things neater on full sized computers but on a micro I tend to prefer global variables over passing pointers around, it isn't the "proper" way to do things but when cycles count it is OK to bend the rules sometimes).
 
I know all about defining and my goal is not that simple that was an example.

I plan on creating libraries that can be used more than once. For instance a spi bit bang lib.

with this the pins of spi can be set at runtime to anything the user wants and they can have 1, 2 or more using that one function.

I will create a structure like

bSpi mySpi1;
MySpi1.port = porta;
Myspi1.sck = 2;
Etc...

And all using same function

SpiSend (myspi1, data):
 
Not really interesting but. ...

I like to layout pcbs simple at home. I dont like using a hundred vias. Also I use sd card and rtc and lcd ... while I can simply use one spi line and multiple cs pins it still leaves me with creating a big pcb.

when speed isnt too much of an issue for instance you are making a clock... You have time to read values and wait seconds etc.

A bitbang spi is great. Can make at home pcbs even smaller and single layered.

There are other reasons im sure but those are mine at this point in time..

Also spi isnt the only protocol I will be using this with.
 
I like to layout pcbs simple at home. I dont like using a hundred vias. Also I use sd card and rtc and lcd ... while I can simply use one spi line and multiple cs pins it still leaves me with creating a big pcb.

Many PICs have re-mappable pins. For these, using a module wouldn't restrict you to certain pins. You can even switch a module to a different set of pins on the fly.

Of course, if you want something that works on all PICs, bit-bang is the only way.
 
Yeah my PIC32MX and PIC18F47j53 have remapable and they are awesome but still locked to certain pins... also most will block another important pin like I2C or another SPI port.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top