0

I am trying to implement the Arduino style _BV() macro for a different microcontroller using C programming. When used with a pin number it returns the bit mask associated with the bit.

#define _BV(bit)    (1UL << (bit))

E.g:- _BV(PF0) gives (1 << PF0)

But the problem comes when defining the bitfields. If I start defining each bit position using symbolic constants it will end up repeating a lot of code as shown below

enter image description here

I tried checking the AVR implementation for the same from the following link

avrpins.h

It is implemented in C++. Is there any efficient way of solving this problem in C?

Supreeth
  • 63
  • 5
  • 1
    Note that you should not, in general, create function, variable, tag or macro names that start with an underscore. Part of [C11 §7.1.3 Reserved identifiers](https://port70.net/~nsz/c/c11/n1570.html#7.1.3) says: — _All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use._ — _All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces._ See also [What does double underscore (`__const`) mean in C?](https://stackoverflow.com/a/1449301) – Jonathan Leffler May 11 '19 at 06:47
  • Thanks for the input but I am not trying to implement a custom function here. It is part of AVR Library [link](https://www.microchip.com/webdoc/AVRLibcReferenceManual/FAQ_1faq_use_bv.html) – Supreeth May 11 '19 at 06:54
  • 4
    Frankly, I don't see a problem with what you have. It's easy to read and verify. Not too terribly hard to write (you could write a code generator, but that's probably the same amount of time). So you do it once, bury it in a header file, and never look at it again. That's what header files are for. – user3386109 May 11 '19 at 06:59

0 Answers0