<br> <body> <h1>Four simple button-LED c-programs illustrating the use of macros.</h1> <p>Here are four simple programs in c for the AVR tiny44 that all do the same thing: turn on an LED when a button is pressed. They assume an LED on pin 5 and a button on pin 8 of an attiny44 microcontroller. You can change the programs to reflect your configuration.</p> <p>The makefiles are written assuming that you have a working usb-tiny programmer - add other programmers as needed by referring to other makefiles. Using the AVR crosspack IDE, commands to program are:</p> <ul> <li>make -f button_44_b.make</li> <li>make -f button_44_b.make program-usbtiny</li> </ul> <p>They also assume that a 20 MHz external clock is on the board, as in the &quot;<a href="http://academy.cba.mit.edu/classes/embedded_programming/hello.ftdi.44.png">hello.ftdi.44.echo</a>&quot; board. These button-push programs run fine with the microcontroller set to use the internal clock sources, but for programs where timing matters, you will need to set fuses to use the external clock source. Using the makesfiles here, for example, the command would be: &quot;make -f button_44_a.make program-usbtiny-fuses&quot;.</p> <p>References for the basics of AVR programming with c: </p> <ul> <li>Refer to the microcontroller datasheet for the functions of registers DDRx, PORTx, and PINx (where x=A,B). </li> <li>For understanding binary arithmetic and operators that work on bytes and bits <ul> <li>Many good tutorials on the web.</li> <li>A good introductory reference is the book &quot;Make:AVR programming&quot; by Eliot Williams. Available online through Harvard Library, and likely through MIT.</li> </ul> </li> </ul> <p>&nbsp;</p> <p><img src="tiny44_pinout.png" alt="tiny44 pinout" width="808" height="332" /></p> <p>There is an LED on pin PB2, physically pin 5 of the Tiny44.</p> <p>There is a button on PA5. </p> <h2>The three programs:</h2> <h4>1. Here is a simple program and makefile to light an LED in response to a button. </h4> <ul> <li><a href="button_44_a.c">button_44_a.c</a><a href="simple_Button_44.c"></a></li> <li><a href="button_44_a.make">button_44_a.make</a></li> </ul> <p>Comments: </p> <ul> <li>It's simple. You can tell that it is all done by putting numbers into registers and checking values of registers.</li> <li>But, to see what is going on, you need to count bits and keep track of numbers. It's easy to make mistakes.</li> <li>If you need to change pins, or alter the program for another board arrangement, you have to change numbers in several places. </li> <li>It can get very confusing if you are doing anything more complex.</li> <li>Program takes 76 bytes in program memory.</li> </ul> <p>&nbsp;</p> <h4>2. Here is a clearer way to do it, using macros.</h4> <ul> <li><a href="button_44_b.c">button_44_b.c</a></li> <li><a href="button_44_b.make">button_44_b.make</a></li> </ul> <p>Comments: </p> <ul> <li>It reads more like spoken language. Once you are comfortable with operations on bits of bytes, it is clearer than the first one.</li> <li>If you want to change pins, you still have to change numbers in several places, and remember for more than a second or two where you put things.</li> <li>If you wanted to change input or output to a different port, you would have to make several changes in the main function and keep track of them.</li> <li>Program takes 72 bytes in program memory.</li> </ul> <p>&nbsp;</p> <h4>3. Here's another way to use macros to do the same thing.</h4> <ul> <li><a href="button_44_c.c">button_44_c.c</a></li> <li><a href="button_44_c.make">button_44_c.make</a></li> </ul> <p>Comments: </p> <ul> <li>Again, it reads like English. It has more levels of definitions and seems more complex.</li> <li>But, if you want to change or add pins, the change is done once, in the #define macros. No changes need to be done within the main function.</li> <li>There is no confusion, for example, if input and output are done on different pins of the same port.</li> <li>Program takes 74 bytes in program memory.</li> </ul> <p>&nbsp;</p> <h4>4. Here's the same thing, using the Arduino library for Attiny microcontrollers.</h4> <ul> <li><a href="./button_arduino/button_arduino.ino">button_arduino.ino</a></li> </ul> <p>Comments: </p> <ul> <li>The comforting familiarity (to some) of the Arduino commands.</li> <li>Under the hood, the same function.</li> <li>Program takes 736 bytes in program memory.</li> <li><a href="http://highlowtech.org/?p=1695">Libraries for ATtiny on Arduino.</a></li> </ul> </body>