Pass Port/Pin Address to Function

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Pass Port/Pin Address to Function

Jump to solution
745 Views
Dustoor
Contributor I

Hello,

 

I am interested in writing a function that takes the address of a port as its argument, so that I can write to that port from within my function. E.g

 

// Definition

void foo(unsigned char *pin) {

   *pin = 1;

   // ...

}

 

// Call

foo(&PORTK_PK1);

 

 

However, the compiler gives me the following error at the function call:

Error: C1833: Cannot take address of this object

 

I am running this code on an MC9S12XS128MAL. Can someone point me in the right direction?

 

Thanks,

Labels (1)
0 Kudos
1 Solution
581 Views
kef
Specialist I

My fault., & is missing. Should be

 

setpin(&PORTK, 1);

clearpin(&PORTK, 1);

 

Also you may need to change function argument from char *ssPort  to unsigned char *ssPort.

 

motorPosition[0] = _ReadEncoder(&PORTK, PORTK_PK0_MASK);

View solution in original post

0 Kudos
6 Replies
581 Views
kef
Specialist I

Pins are not addresseable, but ports are, also pin masks are known. So you may have something like this

 

void setpin(char *port, char mask)

{

   *port |= mask;

}

void clearpin(char *port, char mask)

{

   *port &= ~mask;

}

 

setpin(PORTK, PORTK_PK1_MASK);

clearpin(PORTK, PORTK_PK1_MASK);

 

or

 

void setpin(char *port, char pinno)

{

   *port |= (1<<pinno);

}

void clearpin(char *port, char mask)

{

   *port &= ~(1<<pinno);

}

setpin(PORTK, 1);

clearpin(PORTK, 1);

 

etc.

0 Kudos
581 Views
Dustoor
Contributor I

Thank you. I've tried your suggestion, but I still get the following error:

Type mismatch (expected 'signed char *' , given 'unsigned char')

 

My function prototype is:

short _ReadEncoder(char *ssPort, char ssPinMask);

 

I am calling it as so:

motorPosition[0] = _ReadEncoder(PORTK, PORTK_PK0_MASK);


 

0 Kudos
582 Views
kef
Specialist I

My fault., & is missing. Should be

 

setpin(&PORTK, 1);

clearpin(&PORTK, 1);

 

Also you may need to change function argument from char *ssPort  to unsigned char *ssPort.

 

motorPosition[0] = _ReadEncoder(&PORTK, PORTK_PK0_MASK);

0 Kudos
581 Views
Dustoor
Contributor I

Hi, thanks for your help once again. Placing a '&' and amending the function declaration allowed me to compile with no errors. However, when monitoring the pin using an oscilloscope, I found that it does not change when the function is called. Did we miss something?

 

Function declaration:

short _ReadEncoder(unsigned char *ssPort, char ssPinMask);

 

Function call:

motorPosition[0] = _ReadEncoder(&PORTK, PORTK_PK0_MASK);

 

 

P.S I'm sure the function is actually being called, since I am also calling a legacy function and seeing its result. I've also verified that the pin is initialized and tested it using the legacy function.

0 Kudos
581 Views
kef
Specialist I

It is not clear how is your function defined adn what you expect it to return.

0 Kudos
581 Views
Dustoor
Contributor I

Sorry, I just rechecked my function definition and found that I was setting it twice (rather than clearing first, waiting, then setting it back).

 

Thank you!

0 Kudos