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,
已解决! 转到解答。
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);
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.
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);
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);
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.
