I/O Ports (Part 3)

Part 12345

Let’s go one step further with this subject. Now that we have seen how efficient are the various options for setting I/O ports, I suggest that we try to encapsulate these functions and commands in convenient packages, nammely macros!

Macros are managed by the preprocessor at compilation time (Check #define statement here). The correct syntax is:
#define macro_name(arg_1, arg_2, arg_n) (manage_arguments)

Note that macro can either be commands of functions

These are examples: each of them contains a macro and an example of how to use it:

Set a high logical level on PINB5 from PORT B

#define SetHighLevel(port, pin) (port |= (0x01 << pin)) 
SetHighLevel(PORTB, PINB5);

Set a logical low level on PINB5 from PORT B

#define SetLowLevel(port, pin) (port &= ~(0x01 << pin)) 
SetLowLevel(PORTB, PINB5);

Set a logical level on PINB5 from PORT B: either 0 (low) or anything not equal to 0 (high)

#define SetLevel(port, pin, level) (level ? SetHighLevel(port, pin) : SetLowLevel(port, pin))
SetLevel(PORTB, PINB5, 0x00);
SetLevel(PORTB, PINB5, 0xFF);

Read the logical level on PINB5 from PORT B

#define GetLevel(pins, pin) ((pins >> pin) & 0x01) // O=low, 1=high
byte level = GetLevel((PINB, PINB5);

But we all know that these commands and functions are part of the standard commands since ARDUINO 0013, as bitSet, bitClear, bitWrite, bitRead !

Next post on same subject

Leave a Reply

You must be logged in to post a comment.