The first order of business is to run our regulated power supply into our AVR. Even though it's regulated, we're still doing to decouple it. This is important because current is drawn in spikes on the clock edges, and the spikes are even larger concomitant with the number of I/O lines that are switching. This means the spikes can draw several hundred mA and even in short bursts this is significant. We mitigate this by the use of decoupling capacitors that will provide the source for the spiked current draws. This helps ensure a smooth power supply and all the current a hard-working AVR needs.
Despite the critical importance of decoupling, the setup is very simple and straightforward. Here's how we're going to do it. We're going to put two capacitors between the regulated power source and a voltage clamp diode. Along with that, we're going to add an LED with associated resistor as a power indicator. You'll be done in about 30 seconds.
Make your decoupling circuit
Attach your shiny, brand-new regulated power source to your breadboard. I connect via power at the top mounting posts. Check out the voltage regulator in action. You want your decoupling capacitors as close to your Vcc
as possible, so I moved my ATmega328P over a few holes, along with the wires, and brought my decoupling circuit up from the bottom left. Run positive on the left, negative on the right and place a 47uF electrolytic capacitor from positive to ground, followed by a 0.1uF tantalum capacitor, also from positive to ground. After that, place a 1N4002 diode to clamp, noting the direction (it's polarized). Finally, place a "power on" LED with it's requisite 1K Ohm resistor and that's the decoupling circuit. Check out the pictures.
Run a wire from the positive end of the decoupling circuit to pin 7 Vcc
of the AVR and you are DONE! You have a pretty complete, feature rich AVR development environment.
So how about we christen our brand new AVR by configuring it?
Configuring your new AVR
The primary thing we need to configure are the fuse settings to indicate to the AVR that we want to use an external crystal. Along with this, we'll configure a few settings at the same time that specifies clock speed, startup time, and brown-out detection.
Connect your programmer to the ISP header. I use a USBTiny
from Adafruit Industries. Note the way it connects. Make sure you mate the female connector to the header in the correct orientation.
Fire up avrdude and issue the following command:
avrdude -c usbtiny -p atmega328p -F -t
You should be greeted with a prompt as shown below. Click the [i] in the top left corner of the picture and select the original size if you find the picture difficult to read.
Next issue the "part" command to get some device information, followed by the following command:
This reads the lower fuse settings, which we are interested in, as it contains clock settings. Remember that 400-some page datasheet I linked earlier? In it, you'll find the settings for the lower fuse in Table 27-9. The eight bits in it are:
[ CKDIV8 | CKOUT | SUT1 | SUT0 | CKSEL3 | CKSEL2 | CKSEL1 | CKSEL0 ]
Fuse settings are peculiar, as a 1 means unprogrammed and a 0 indicates programmed. CKDIV8 comes programmed from the factory and divides the clock by 8. The internal RC oscillator is programmed by default at 8MHz and with CKDIV8 programmed, that sets the clock speed at 1MHz. We're going to change that.
The whole lower fuse register is one byte and what we are going to do is set:
- CKDIV 1
- CKOUT unchanged
- SUT1 0
- SUT2 1
- CKSEL3 0
- CKSEL2 1
- CKSEL1 1
- CKSEL0 1
The lower fuse comes programmed from the factory with a setting of 0x62. To make our changes we need to convert the hex to binary, then back to hex to make our change. 0x62 in binary is 0110 0010. Based on the changes I mentioned above, we want that register to contain 1101 0111 which is 0xD7 in hex. Here's how that looks:
[ CKDIV8 | CKOUT | SUT1 | SUT0 | CKSEL3 | CKSEL2 | CKSEL1 | CKSEL0 ]0x62 0 1 1 0 0 0 1 00xD7 1 1 0 1 0 1 1 1
Fire avrdude back up and issue: avrdude -c usbtiny -p atmega328p -F -t
At the prompt type
write lfuse 0 0xD7
It should give you a message about writing the fuse. Issue "read lfuse" to read the fuse and verify that it wrote 0xD7. Once complete, type "quit" to leave.
Your AVR is now programmed to run at a blazing 20MHz with a quick startup time and brown-out detection enabled. You're encouraged to read the section on lower fuse settings if any of that is confusing.
At this point, you are able to program your AVR to your hearts content. You can hook up LED's and blink them, read DIP switches, drive motors, write to LCD's, read external memories, and more. But you don't have any way to capture output on a grand scale. That's the next order of business.