Im new to embedded projects…have only done a few and i noticed places in the code that will say something like
#define portwhatever 0x01
or something to that affect…If I had to guess its a defenition for an address but where is the address coming from? I have been unable to find it anywhere in the microchips documentation.
setting exampleRegister to PORTB makes sense but setting the pinmask does not as the pin the program is using is RB0. Wouldn’t the above statement suggest the pin is RB1…?
The #define directive allows you to substitute one text string for another. It is not a mask of any sort. Using them makes the code more readable and easier to work with.
// Example
#define ON 1
Whenever the assembler/compiler encounters the text “ON”, it substitutes “1”. You, the programmer, won’t be aware of the transformation unless you look at the listing file generated by your tool. You can choose almost anything you want for the text to match, but the substituted text must make sense in the context of the chip you’re working with. For instance, if you define a substitution for the status register of a PIC as #define STATUS 12, that wouldn’t be correct as the STATUS register is register 03.
Your example appears to map a particular I/O port with a specific function. Here is a similar example that defines a number of LED settings:
// PORTA is another example of a #define that was declared in
// another file (#include <pic16f88.h>, for example). The compiler
// vendors provide these include files to make things simpler for
// the user of their tools, much easier to remember "PORTA" than "5"
#define BUSY_LED PORTA.0
#define LOW_BATTERY_LED PORTA.1
#define ON 1
#define OFF 0
// Code that doesn't use any #defines
if (05.0 == 1) {
... do something
}
// Code that uses the #define from the compiler vendor
if (PORTA.0 == 1) {
... do something
}
// Code that uses the #defines in this file
if (BUSY_LED == ON) {
... do something
}
leon_heller:
He might still be confused. The point I was making is that the 0x01 is a bit mask, not a port pin.
I do see your point and you might be right about the OP’s confusion. The example doesn’t make it clear what the intended effect is supposed to be. I thought stepping back a step might provide some clarification.
setting exampleRegister to PORTB makes sense but setting the pinmask does not as the pin the program is using is RB0. Wouldn’t the above statement suggest the pin is RB1…?
This looks very similar to some code I wrote for the 24L01 8) . The *_IOREGISTER #define should be set to whatever the IO port is for the pin you're using to control that function. The *_PIN_MASK #define is set as a mask for the pin you're using, and can be calculated by left shifting a binary '1' to the left by the pin number you're using. This effectively puts a binary '1' in the bit position of the pin that you're using, which is what allows my code to set and clear that pin and only that pin.
To get an idea of what I’m talking about, check out this little ASCII diagram. The hex number (middle column) would be what you would use for the *_PINMASK #define for the IO pin you’re using (far right column).
If you’re using my code, doing that won’t work without modifying my source code. However, if you’re doing that in your own code, it should work just fine.