bitmask...I think

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.

The addresses or bits will be in the data sheet.

Leon

Here is my example…

#define exampleRegister_IRQ_IOREGISTER PORTB

#define examplePIN_PINMASK 0x01

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…?

It’s a bit mask, not an actual pin!

Leon

Im still lost…So I can put whatever I want here as long as it doesn’t conflict with any other masks?

It depends what you want to mask. The original code probably won’t work if you change it.

Leon

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
}

He might still be confused. The point I was making is that the 0x01 is a bit mask, not a port pin.

Leon

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.

Between the two of us we might have sorted him out. :sunglasses:

Leon

stevo:
Here is my example…

#define exampleRegister_IRQ_IOREGISTER PORTB

#define examplePIN_PINMASK 0x01

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).

76543210   hex     controls IO pin
10000000 = 0x80    7
01000000 = 0x40    6
00100000 = 0x20    5
00010000 = 0x10    4
00001000 = 0x08    3
00000100 = 0x04    2
00000010 = 0x02    1
00000001 = 0x01    0

Wow you guys are good…I was wondering if i could just put

#define IRQ_PIN PORTBbits.RB3

or something to that affect…But it looks like the code would be more stream lined by using the hex values…maybe something i should learn.

#defines are expanded at compile-time, not run-time, by the C preprocessor. It won’t make any difference.

Leon

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.