View previous topic :: View next topic |
Author |
Message |
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
Macro question |
Posted: Fri Oct 22, 2004 3:31 am |
|
|
How can I create a macro for reading a port? _________________ Alex |
|
|
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
|
Posted: Fri Oct 22, 2004 3:37 am |
|
|
Also the input/output port/bit functions do not work in my compiler V3.190
for some reason. _________________ Alex |
|
|
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
|
Posted: Fri Oct 22, 2004 3:44 am |
|
|
In other words, how can I make a macro instead of this function?
BYTE ReadUSBNbus(void)
{
USBN_BUS_DIR = INPUT ; //TRISC = 0xFF
return(USBN_BUS) ; //read PORTC
}
DataByte = ReadUSBNbus(); _________________ Alex |
|
|
Guest
|
|
Posted: Fri Oct 22, 2004 3:51 am |
|
|
I don't know, but I'm curious why you'd want a macro for that, instead of a normal function |
|
|
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
|
Posted: Fri Oct 22, 2004 3:56 am |
|
|
The reason for that is all the similar stuff in the program is defined as macro, I would like to keep the same standard _________________ Alex |
|
|
dvsoft
Joined: 28 Nov 2003 Posts: 46
|
|
Posted: Fri Oct 22, 2004 4:38 am |
|
|
bonjour,
Function
Code: |
BYTE ReadUSBNbus(void)
{
USBN_BUS_DIR = INPUT ; //TRISC = 0xFF
return(USBN_BUS) ; //read PORTC
}
|
Macro
Code: |
#define ReadUSBbus(BUS)\
USBN_BUS_DIR = INPUT ;\
BUS = USBN_BUS
|
Usage
Code: |
byte X;
ReadUSBBus(X);
|
Alain |
|
|
dvsoft
Joined: 28 Nov 2003 Posts: 46
|
|
Posted: Fri Oct 22, 2004 4:41 am |
|
|
Re,
but with compiler CCS you can use
Code: |
#inline
BYTE ReadUSBNbus(void)
{
USBN_BUS_DIR = INPUT ; //TRISC = 0xFF
return(USBN_BUS) ; //read PORTC
}
|
look at the code assembler which is produced
Alain |
|
|
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
|
Posted: Fri Oct 22, 2004 4:43 am |
|
|
dvsoft wrote: | bonjour,
Function
Code: |
BYTE ReadUSBNbus(void)
{
USBN_BUS_DIR = INPUT ; //TRISC = 0xFF
return(USBN_BUS) ; //read PORTC
}
|
Macro
Code: |
#define ReadUSBbus(BUS)\
USBN_BUS_DIR = INPUT ;\
BUS = USBN_BUS
|
Usage
Code: |
byte X;
ReadUSBBus(X);
|
Alain |
So where do you read the port to?
Is the 'BUS' returned from macro writing it this way 'BUS = USBN_BUS' ?
If it does return, so to where? _________________ Alex |
|
|
Ttelmah Guest
|
|
Posted: Fri Oct 22, 2004 5:45 am |
|
|
alexz wrote: | dvsoft wrote: | bonjour,
Function
Code: |
BYTE ReadUSBNbus(void)
{
USBN_BUS_DIR = INPUT ; //TRISC = 0xFF
return(USBN_BUS) ; //read PORTC
}
|
Macro
Code: |
#define ReadUSBbus(BUS)\
USBN_BUS_DIR = INPUT ;\
BUS = USBN_BUS
|
Usage
Code: |
byte X;
ReadUSBBus(X);
|
Alain |
So where do you read the port to?
Is the 'BUS' returned from macro writing it this way 'BUS = USBN_BUS' ?
If it does return, so to where? |
In the example given, he shows a byte variable, called 'X' being declared, and then the macro being used, with this variable passed as it's argument. The macro declaration itself, shows it's argument given a name 'bus' (any suitable name can be used here). The macro 'expansion', then substitutes the passed variable (in this case 'X', for every location where the argument is used.
So the macro expands as:
Code: |
USBN_BUS_DIR = INPUT;
X=USBN_BUS;
|
The ';' on the end of the line, gets added to the end of the macro, making the line complete (in C terms).
If you called the macro with:
Code: |
byte X,Y;
ReadUSBBus(X);
ReadUSBBus(Y);
|
The expansion would be:
Code: |
USBN_BUS_DIR = INPUT;
X=USBN_BUS;
USBN_BUS_DIR = INPUT;
Y=USBN_BUS;
|
You can also do things with macros, that are often 'missed' by a lot of C programmers. For instance:
Code: |
#define ReadUSBbus() (USBN_BUS_DIR = INPUT,\
USBN_BUS)
|
Used like this:
Code: |
Byte X;
X=ReadUSBbus();
|
Creates a macro, that behaves more like a function, returning a value!. What happens, is that the seperate parts inside the round brackets (seperated by ',', rather than the normal C terminator of a ';'), are executed in turn, and the value of the last statement, is 'returned'. Hence in this case, the value of 'USBN_BUS', gets returned. This behaves exactly like the function in your original code, except that it is automatically 'inline'.
Best Wishes |
|
|
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
|
Posted: Fri Oct 22, 2004 6:49 am |
|
|
Thank you very much!
It really helped! _________________ Alex |
|
|
|