When working on a microcontroller project in C, we often capture the
mapping of a signal to a physical pin with preprocessor macros, like so
1 | // button is connected to PB5
|
2 | #define BUTTON_PORT PINB
|
3 | #define BUTTON_BIT 5
|
and we use those definitions later in our code
1 | // test if button is pressed
|
2 | if (BUTTON_PORT & (1 << BUTTON_BIT)) { do_something(); }
|
If we later decide that the PCB layout is easier if we connect the
button to PC3 instead of PB5, we just change the two lines above.
But what if we need to change the logic, so the button input is low when
pressed? What if we want to use a pin change interrupt for that signal?
The simple #defines won't do it anymore.
That's what the <stdpins.h> header is for: it allows you to define port
name, pin number and polarity in one place, and then refer to that
signal definition throughout your code, for basic I/O, and for pin
change interrupts.
1 | #define button B,5,ACTIVE_LOW
|
2 | if (IS_TRUE(button)) do_something();
|
3 | PCI_ENABLE(button);
|
4 | PCIEx_ENABLE(button);
|
It can be used with "classic" AVR projects as well as with Arduino
projects.
Source code in https://github.com/requireiot/stdpins