EmbDev.net

Forum: µC & Digital Electronics polarity-aware header for pin definitions


von Bernd K. (dasohr)


Rate this post
useful
not useful
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

: Edited by User
von Dymo Fond (Guest)


Rate this post
useful
not useful
Hubris is when you develop a specialized solution to a problem almost no 
one has, except confused Arduino users. And then call the header std... 
as if it is a standard library.

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.