Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Defining the pins with names

Status
Not open for further replies.
You can use the #define directive for that.

An example

#define LED LATAbits.LATA1

It can be used for Ports, Constants and even Conditional Macro inclusion using the #ifdef directive.
 
Last edited:
yes you can as wond3rboy has shown you, you can also use #define for things like #define on=1; #define off=0; that way you can have LED=on or whatever you want.. if i was you i would do a search for 3v0's tuts in C it will give you a good place to start,
 
yes you can as wond3rboy has shown you, you can also use #define for things like #define on=1; #define off=0; that way you can have LED=on or whatever you want.. if i was you i would do a search for 3v0's tuts in C it will give you a good place to start,

Actually, I wouldn't do it that way. You would want to define them like:

Code:
#define ON               (1)
#define OFF              (0)

Note that lack of an equals sign or semicolon. The parentheses aren't required, but I'm in the habit of using them for all #defines as it can save you from some problems.

Brad
 
thanks for the tip! out of interest what are the advantages of doing it that way as i am a noob at programming
 
thanks for the tip! out of interest what are the advantages of doing it that way as i am a noob at programming

#defines are direct text substitution. The compiler doesn't parse these values (as it would a C statement), it does the substitution and parses the result of the substitution. If you use

Code:
#define on=1;

Then everywhere you have the text "on=1;", it will be replaced with nothing. The first value after the define is substituted for the second, here the second is blank.

So the difference is that the first way won't work like you would expect and the second will.

Brad
 
Macros can make debugging harder, the more clever you are with the macro the more trouble it can cause.

The less you know about c and coding in general the more trouble it will cause you.
 
Last edited:
You can use the #define directive for that.

An example

#define LED LATAbits.LATA1

It can be used for Ports, Constants and even Conditional Macro inclusion using the #ifdef directive.

But I'd recommend on thing there as best practice...
always add TRIS, LAT, or PORT.

#define LAT_LED LATAbits.LATA1
#define TRIS_LEDTRISAbits.TRISA1

It removes any confusion over whether you're talking about the LAT, PORT, or TRIS part. Otherwise, it's all too easy to mix up PORT and LAT usage. It's also clearer to read, because "LED" alone doesn't tell us a whole lot for sure.
 
Last edited:
LATA refers to the LATCH Register of PORTA and subsequently is the output latch for that port. For the 18Fs, there is no difference between the two. Previously one would need to READ a Port using the PORTx register and WRITE to it using the LATCHx Register. Check the data sheet.
 
Status
Not open for further replies.

Latest threads

Back
Top