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.
I might be a bit late on this but this is how Ive done it: (XC8 Complier)

Code:
#define _XTAL_FREQ 8000000
#define RA5 (PORTAbits.RA5) //Charge LED
#define RA4 (PORTAbits.RA4) // PFET

I added this part After my #includes

Usage:

Code:
 if (Vbattery >= Vcharge)
  {
  RA4 = 1;
  RA5 = 1;
  }

It seems to work.
 
I might be a bit late on this but this is how Ive done it: (XC8 Complier)

With pre-processor you can go further:

C:
#define LED LATAbits.RA5
#define FET LATAbits.RA4
...
LED = 1;
FET = 1;

However, this doesn't really let you pass it to a function.
 
Accessing a port is simply about accessing the register (memory location) that controls that port. So, all you need to do is "pass around" the memory address of the register.

example:
register address: 0x12

function prototype to accept memory locations:
void writeToMemory(void *address);

pass it to a function (as a pointer) and use it:
(uint8_t*)0x12 //cast memory address to pointer (the compiler now knows that the memory address is for unsigned 8-bit variable at that address)

dereference the pointer so that you can write to that location. Without dereferencing you would just change the address, not the value at that address.
(*((uint8_t*)0x12)) = 0b11111111; // write all ones to the port

If you look at how registers and other ports are defined in your library you will find something like:
#define PORTA (*((uint8_t*)0x12))
 
Last edited:
Accessing a port is simply about accessing the register (memory location) that controls that port. So, all you need to do is "pass around" the memory address of the register.

example:
register address: 0x12

function prototype to accept memory locations:
void writeToMemory(void *address);

pass it to a function (as a pointer) and use it:
(uint8_t*)0x12 //cast memory address to pointer (the compiler now knows that the memory address is for unsigned 8-bit variable at that address)

dereference the pointer so that you can write to that location. Without dereferencing you would just change the address, not the value at that address.
(*((uint8_t*)0x12)) = 0b11111111; // write all ones to the port

If you look at how registers and other ports are defined in your library you will find something like:
#define PORTA (*((uint8_t*)0x12))




OK now for the stupid question!
I get it except #define PORTA (*((uint8_t*)0x12))

What does _t* Mean/do? I get uint8 (unsigned int 8 long) and I get 0x12 is the address in memory
 
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);
}

Hi NorthGuy,

Isn't it a problem to assign int value into Char Pointer?
 
Isn't it a problem to assign int value into Char Pointer?

What would you like it to be?

I think an unsigned int would be a better choice, but int would likely work depending on the address space.
 
*myPort |= (1<<pin);

I think that in this case the compiler does an implicit cast to the correct type.
Notice that the "pin" variable is not the value that is assigned to the port. It is just the number of left shifts applied to the constant 1.

You can do an explicit cast if you want.
*myPort |= ((char)(1<<pin));
 
*myPort |= (1<<pin);

I think that in this case the compiler does an implicit cast to the correct type.
Notice that the "pin" variable is not the value that is assigned to the port. It is just the number of left shifts applied to the constant 1.

You can do an explicit cast if you want.
*myPort |= ((char)(1<<pin));

Hi T,

You're probably right, Pin is just the number of left shifts.

We should keep in mind that Char is 8-bit value.
Therefore if the Port constitutes 32-bit, then myPort should be of type int*, or unsigned int*.


KISS said:
I think an unsigned int would be a better choice, but int would likely work depending on the address space.
Is there really in this case, which has nothing to do with arithmetic operations, whether we choose int or unsigned int?
as long as they're both 32-bit (or any other length).
 
Last edited:
NorthGuy didn't say that. I did.

0-65535 makes more sense than 0-32767 and -1 to 32768 for an address space. Therefor the compiler type casting a pointer to an unsigned int makes a LOT of sense.
 
Status
Not open for further replies.

Latest threads

Back
Top