CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Macro question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
alexz



Joined: 17 Sep 2004
Posts: 133
Location: UK

View user's profile Send private message

Macro question
PostPosted: Fri Oct 22, 2004 3:31 am     Reply with quote

How can I create a macro for reading a port?
_________________
Alex
alexz



Joined: 17 Sep 2004
Posts: 133
Location: UK

View user's profile Send private message

PostPosted: Fri Oct 22, 2004 3:37 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Oct 22, 2004 3:44 am     Reply with quote

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








PostPosted: Fri Oct 22, 2004 3:51 am     Reply with quote

I don't know, but I'm curious why you'd want a macro for that, instead of a normal function Question
alexz



Joined: 17 Sep 2004
Posts: 133
Location: UK

View user's profile Send private message

PostPosted: Fri Oct 22, 2004 3:56 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Oct 22, 2004 4:38 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Oct 22, 2004 4:41 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Oct 22, 2004 4:43 am     Reply with quote

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







PostPosted: Fri Oct 22, 2004 5:45 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Oct 22, 2004 6:49 am     Reply with quote

Thank you very much!
It really helped!
_________________
Alex
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group