Four simple button-LED c-programs illustrating the use of macros.
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.
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:
- make -f button_44_b.make
- make -f button_44_b.make program-usbtiny
They also assume that a 20 MHz external clock is on the board, as in the "hello.ftdi.44.echo" 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: "make -f button_44_a.make program-usbtiny-fuses".
References for the basics of AVR programming with c:
- Refer to the microcontroller datasheet for the functions of registers DDRx, PORTx, and PINx (where x=A,B).
- For understanding binary arithmetic and operators that work on bytes and bits
- Many good tutorials on the web.
- A good introductory reference is the book "Make:AVR programming" by Eliot Williams. Available online through Harvard Library, and likely through MIT.
There is an LED on pin PB2, physically pin 5 of the Tiny44.
There is a button on PA5.
The three programs:
1. Here is a simple program and makefile to light an LED in response to a button.
Comments:
- It's simple. You can tell that it is all done by putting numbers into registers and checking values of registers.
- But, to see what is going on, you need to count bits and keep track of numbers. It's easy to make mistakes.
- If you need to change pins, or alter the program for another board arrangement, you have to change numbers in several places.
- It can get very confusing if you are doing anything more complex.
- Program takes 76 bytes in program memory.
2. Here is a clearer way to do it, using macros.
Comments:
- It reads more like spoken language. Once you are comfortable with operations on bits of bytes, it is clearer than the first one.
- 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.
- 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.
- Program takes 72 bytes in program memory.
3. Here's another way to use macros to do the same thing.
Comments:
- Again, it reads like English. It has more levels of definitions and seems more complex.
- 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.
- There is no confusion, for example, if input and output are done on different pins of the same port.
- Program takes 74 bytes in program memory.
4. Here's the same thing, using the Arduino library for Attiny microcontrollers.
Comments:
- The comforting familiarity (to some) of the Arduino commands.
- Under the hood, the same function.
- Program takes 736 bytes in program memory.
- Libraries for ATtiny on Arduino.