Output Pin Tutorial
1. Create a new project with a meaningful name, and an initial file. For example you could call your project output_pin, and have an initial file called output_pin.c. Add a main function to your initial file as demonstrated in the previous tutorial.
2. Many useful definitions have been setup to facilitate access to the microcontroller's io pins without knowledge of their particular addresses in a header file called io.h. This header file can be found in the avr folder of the default search path. To make use of its #defines you can include the following line of code at the top of your initial file:
The #include command instructs the pre-compiler to insert the code found in the header file beginning at the current line.
3. Once you have included the io.h header file you can make use of the definitions that it contains. The first step in accessing an io pin on you micontroller is to set the direction of the pin, 0 for input, and 1 for output. By default, all io pins are set to inputs upon reset. One way of setting the direction of port A pin 3 to output is to include the following line of code in your main function before the return command:
DDRA = b00001000;
However there are many other ways to set the value of this pin. For instance consider the following line of code:
DDRA |= 1 << PIN3;
Why might this be a better way in general of setting the direction of port A pin 3? What are the problems that could arise from the previous method?
4. Once you have set the direction of port A pin 3 in the direction register, you can toggle its value from 0 (low) to 1 (high) in subsequent lines of code. Set the output level of this pin in the register PORTA in the same way as you set the direction of the pin in the register DDRA.