polarity-aware header for pin definitions

OP #6781499
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

Reply

A question of your own? This thread is over 5 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.