In the last part we got an atmega168 connected up to an UM232R board so we could program it from our PC. This is a decent step but we’re not really using the full potential of our little MCU yet.

Most instructions on our ATMega168 run in a single clock cycle and the chip arrives running at 1Mhz. Atmel do this so you can program the chip with even the crappiest of programmers. Still – that’s about 1 million instructions per second – which is still incredible if you think about it. Having said that, we can do up to 20x better than this.

Running faster has lots of benefits – writing the flash will finish quicker for one – which for someone very impatient like me is just about the best news.

On the downside, you’ll be consuming more power – but there are ways to mitigate that with the MCU various levels of power-saving (we’ll cover this in another post)

Crystals (but not the kind your girlfriend will like – unless you’re very lucky!)

I happen to have a bunch of 14.7(ish) MHZ crystal oscillators hanging about – the regular 2 pin variety – so i’ll be using these. As an aside, the MCU is very helpful in that it has the circuitry built-in to get the crystal started up and behaving like an oscillator. It means you don’t really need to provide anything else yourself.

Connect the oscillator to pins 9 and 10 of your mcu. Now in theory you should connect 2 caps (one for each pin) of the same value to the oscillator in order to get smooth out any emf noise or stray capacitance but when playing around with something where the timing isn’t critical for very long periods then i tend not to bother with the caps. If you’re implementing something that is very chatty with the UART over longer periods then you might want to consider adding them in.

Take a look at the datasheet for your mcu to check the values for the caps if you decide to go for it.

Lighting The Fuse (bits)

The enabled functions and configuration of the MCU is controlled by something called fuse bits. You can think of them as 4 registers (they are actually 2 but we (and AVRDude) treat them seperately. Lets call these registers Hi, Lo, Extended and Lock. The Hi register deals with features to be enabled or configured. Lo deals with clock sources and config. Extended deals with boot loader config and Lock controls if we can write or verify the MCU.

Be very careful with setting the fuse bits. Setting the Lock bits or some of the Hi bits incorrectly (specifically disabling the Reset pin or the disabling SPI) will mean you can’t write to the MCU again unless you have a high voltage or parallel programmer.

With all this in mind lets get the MCU configured in a way that might be useful. External oscillator with a decent amount of start up delay to get it actually oscillating. Enable SPI programming, leave the watchdog timer on, enable brownout detection. We’ll leave the boot sector bits alone as we’re going to do our programming from scratch and we’ll leave the lock bits well alone. I’m also going to let the EEPROM get erased for any erase cycle.

With that in mind lets set the fuse bits

sudo avrdude -c ftdi -p m168 -B 4800 -P ttyUSB0 -U lfuse:w:0xf7:m -U hfuse:w:0xca

If avrdude is successful you can now disconnect the power. If you haven’t attached the crystal, now is the time. You’ll find that your MCU won’t work without one.

Let’s do an erase of the chip – noting that it’s much faster. Since the clock is faster so is the UART on the chip and hence our bitrate.

sudo avrdude -c ftdi -p m168 -P ttyUSB0 -e

Here’s a handy fuse bit calculator if you want to experiment but really can’t be arsed with the datasheet

http://www.frank-zhao.com/fusecalc/fusecalc.php?chip=atmega168

Now we’re ready to do something real with the chip which will be our subject next Monday (or perhaps earlier if i get a chance).