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 support@ccsinfo.com

Creative macro definitions ??

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



Joined: 29 Sep 2006
Posts: 118

View user's profile Send private message

Creative macro definitions ??
PostPosted: Tue Oct 24, 2006 4:08 am     Reply with quote

Hello,

I've created some macro's inspired by the easy build in functions like Make8. The macro's do word and assembly very efficient !
However I'm wondering if I'm doing something 'tricky' or risky..

Any comments? How does CCS define their make8 ?

Regards,
Edwin.

//--- Macro definitions -------------------------------------------------------------------------------------
Typedef union { // This union typedef can be used for easy acces
int8 B[2]; // of bytes in integers
long int W;
} btwd;

#define Make8L(x) ((btwd)(x)).b[0] // return low-byte of word
#define Make8H(x) ((btwd)(x)).b[1] // return hi-byte of word
#define Or16H8(x,y) Make16(Make8H(x)|y, Make8L(x)); // Or-function hi-byte of word-x with byte-y
#define Or16L8(x,y) Make16(Make8H(x), Make8L(x)|y); // Or-function lo-byte of word-x with byte-y


Last edited by Torello on Tue Oct 24, 2006 1:51 pm; edited 1 time in total
Ttelmah
Guest







PostPosted: Tue Oct 24, 2006 4:24 am     Reply with quote

All good C.
Nice to see someone else using the define language constructively. Smile
The only 'caveat', (doesn't apply to the examples given), is to be carefult to always 'bracket round' macros used inside macros, and be very wary about the defintion placements and order, since some odd results can otherwise apply.
The macro expansin, is carried out 'ahead' ofthe main compiler pass, so should always be safe, provided the code you generate with it is 'safe'.

Best Wishes
Torello



Joined: 29 Sep 2006
Posts: 118

View user's profile Send private message

PostPosted: Tue Oct 24, 2006 4:32 am     Reply with quote

Thanks! Laughing
Can one also make/define their own #use definitons ? I want to connect the i/o- pins of my own driver module in the main module by using a #use directive. (would be very nice..)
Ttelmah
Guest







PostPosted: Tue Oct 24, 2006 5:16 am     Reply with quote

I would not do this. '#', is a reserved keyword in itself for the pre-processor, and has effects on so many things, that I _would_ be 'wary' of trying to do anything with this!.
However there is nothing potentially to stop you have a 'use_my_peripheral' setup function, with the macros.
Remember also, that the defines can be 'multi line'. Just put a '\' at the end of a line, and the definition will continue on the next line. Also if the define is bracketted, and the sections are seperated by commas, the value of the last statement is returned. So:
Code:

#define frombuff(buff,in,out,size) (btemp=buff[out],\
   out=(out+1) % (size), \
   btemp)


Reads the value of buff[out], into the variable 'btemp', then increments 'out', and wraps it if it reaches 'size', and then returns the value from 'btemp'. Note the bracketting round 'size'. This is the sort of caveat I was talking about. if 'size', was a mathematical construct in the calling line like '45+8', then if it is not bracketted, the modulus function would just use the '45', and then 8 would be added to the result. By putting a bracket round it, I ensure the addition is performed first.

Best Wishes
Torello



Joined: 29 Sep 2006
Posts: 118

View user's profile Send private message

PostPosted: Tue Oct 24, 2006 6:03 am     Reply with quote

Thanks again ! Laughing Great tips! Shure going to use them.

Best wishes too,
Regards,
Edwin.
tavioman



Joined: 22 Feb 2006
Posts: 65

View user's profile Send private message

Debug
PostPosted: Fri Dec 08, 2006 3:34 am     Reply with quote

Code:
#define _TRACE_ ( x )      {#ifdef DEBUG;  printf( x  );     #endif}
#define _TRACE1_( x, y )   {#ifdef DEBUG;  printf( x, y );   #endif}
#define _TRACE2_( x, y, z ){#ifdef DEBUG;  printf( x, y, z ); #endif}


Usage example:

Code:
_TRACE_("Info");
_TRACE1_("pot = %Ld\n\r", pot);
_TRACE2_("pot = %u, xxx = %Ld", pot, xxx);


If there are any suggestion please tell me?
Torello



Joined: 29 Sep 2006
Posts: 118

View user's profile Send private message

PostPosted: Fri Dec 08, 2006 4:01 am     Reply with quote

Cool idea! Smile Going to use it !
tavioman



Joined: 22 Feb 2006
Posts: 65

View user's profile Send private message

Debug
PostPosted: Fri Dec 08, 2006 6:00 am     Reply with quote

I'm wondering if there is any procedure to make this more simple.
Like _TRACE_("");
No _TRACE1_ or _TRACE2_ .....etc.
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