avriomacros.txt (1387Bytes)
iomacros.h.txt (2076Bytes)
UPDATED CONTENT BELOW!
Programming AVRs can be difficult for people with little experience in C programming. I think one of the things that is tricky is understanding bitwise operators. Bitwise operators such as |, &, <<, and >> are commonly used for declaring programmable digital IO as either inputs or outputs, setting outputs high or low, reading a digital input, and also setting internal pullup resistors.
This is why I created a header file with macro functions to accomplish these basic input and output tasks, and also because I was tired of copying and pasting the code from old projects, and I also think it looks cleaner. Using the avriomacros.h header file with an avr project is fairly simple and straight forward with AVR Studio:
- copy and paste the header file in the same folder as the c file
- right click the header files folder icon in AVR Studio project tree and click add existing header files (see picture)
- browse and select avriomacros.h
- put #include "avriomacros.h" at the top of the c file
Examples of using avriomacros.h:
with the following input defined:
#define BUTTON PB2
and output defined (note that both reside with register B):
#define LED PB3
to define the button as an input:
in(BUTTON,B);
to define the LED as an output:
out(LED,B);
where the first input is the bit (or pin) and the second input is the register (using register B which is the "B" in "PB2")
to turn on the LED:
on(LED,B);
to set the internal pullup resistor for the button:
pullup(BUTTON,B);
to read the value or state of the button input:
get(BUTTON,B)
The other macro functions are similar, just see the attached header file for a description of each. The file "avriomacros.txt" needs to be renamed to "avriomacros.h" to use- I had to change it due to file extension restrictions when uploading to LMR.
In case anyone is interested in what the dual ## signs do in the macros see the bottom of this post on the C preprocessor, specifically Pasting Tokens. Please also note I'm open to criticism!
UPDATE:
The previous macro functions worked fine but it was not ideal as it required knowing which port a pin is on, which resulted in looking up pin declarations a lot (if say LED was defined as PB3 then I would know to use port B when calling the macro to turn it off or on).
The new and improved iomacros.h header file will keep track of the port bit for you, it basically requires #define'ing your IO as pin,port (separated by a comma).
Here is the sample usage, which is documented in the header file:
#include <avr/io.h>
#include "iomacros.h"
#define LED 3,B // declare LED pin and port
#define BUTTON 2,B // declare BUTTON pin and port
int main (void) {
out(LED); // set LED as an output
in(BUTTON); // set BUTTON as an input
pullup(BUTTON); // activate internal pullup resistor for BUTTON
while(1) { // forever loop
if(get(BUTTON) == 0) // if button is pressed...
on(LED); // turn on LED
else // otherwise
off(LED); // turn off LED
}
}See the link at the top to download iomacros.h
Enjoy!