I am currently working on a good macro for setting bits and getting bits from registers. Here is my current progress blog/self/2015/07/11/v2.0-avr-register-macros.html . And this is what it looks like in practice.
// Example blink code for Arduino UNO 16MHz (atmega328p) #define F_CPU 16000000UL // For delay.h to function #include <avr/io.h> // PIN, DDR, PORT defs #include <util/delay.h> //_delay_ms() #include "./AVRAugment_io.h" ##define LED13 B5 // Only need to change this to swap pins #define DDR_13 SET_REG( DDR, LED13, 0xFF ) //IN=0x00, OUT=0xFF #define ON_13 SET_REG( PORT, LED13, 0xFF ) //LOW=0x00, HIGH=0xFF #define OFF_13 SET_REG( PORT, LED13, 0x00 ) //LOW=0x00, HIGH=0xFF int main(void) { DDR_13; ON_13; while(1) { _delay_ms(2000); OFF_13; _delay_ms(2000); ON_13; } }
Some thing I would like to know, since this method can only set one bit at a time for registers that are defined by Pxn (used for DDR, PORT, PIN). Is Pxn and DDR PORT PIN pinout the only one that has any cross relations in terms of "x=letter" "n=number"? (e.g. can I get away with only supporting DDR, PIN, PORT, thus simpler macro. Or should it still remain general?)
Maybe since this is limited to setting one bit at a time, I should rename from "SET_REG" to "SETPIN_REG" to avoid confusion.
----------
Or maybe you have a better approach entirely that allows for directly saying "#define LEDPIN B5" or similar? Also would be nice if the same approach can be used for PWM etc...
*