Note that there are some explanatory texts on larger screens.

plurals
  1. POC macro for defining symbols
    text
    copied!<p>I'm working with a microcontroller with ANSI C (gcc; not C++) and have a lot of hardware pins to define. I'm looking for a way to make the pin definitions more readable.</p> <p>I think I'd like a macro that would let me define each pin in a single line, like this:</p> <pre><code>PIN(LED_RED, E, 2); PIN(LED_YELLOW, B, 3); PIN(LED_GREEN, A, 4); </code></pre> <p>(this is just a simple example - I've dozens of pins to define). Right now I have ugly looking code like this:</p> <pre><code>#define LED_RED (LATEbits.LATE2) #define LED_RED_TRIS (TRISEbits.TRISE2) #define LED_YELLOW (LATBbits.LATB3) #define LED_YELLOW_TRIS (TRISBbits.TRISB3) #define LED_GREEN (LATAbits.LATA4) #define LED_GREEN_TRIS (TRISAbits.TRISA4) </code></pre> <p>Each pin has 2 symbols; one to read/write the pin, and another to set the I/O direction (the _TRIS definitions). The latch and TRIS definitions come from a header library supplied by the MCU vendor; it's not really practical to avoid using them.</p> <p>I'm fairly sure it is possible to write a macro in C that will define both symbols, but I'm not very good with the # and ## stuff. Here is my half-baked attempt (doesn't work): </p> <pre><code>#define _PIN( id,port,pos) #define ##id (LAT ##port ##bits.LAT ##port ##pos ) #define _TRIS(id,port,pos) #define ##id _TRIS (TRIS ##port ##bits .TRIS ##port ##pos ) #define PIN( id,port,pos) _PIN(id,port,pos) _TRIS(id,port,pos) </code></pre> <p>Is there a way to do this? </p> <p>Or, is there another way to simplify my pin definitions? I'd like to get it down to one line/pin, and to get rid of the duplication of port id (A,B,C, etc.) and bit number (2,3,4, etc.) that I have now; putting them in twice is just asking for trouble.</p> <p>Cheers,</p> <p>--Dave</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload